MByD
MByD

Reputation: 137442

Get current frame from android mediaplayer

Is there a way to retrieve the current frame played from android MediaPlayer object?

Upvotes: 0

Views: 4326

Answers (3)

Peter
Peter

Reputation: 13515

I assume you mean the frame bitmap, not the index of the frame.

If so, you can render it to a TextureView, hide the TextureView, and then call TextureView.getBitmap().

Here's some Kotlin code to that effect (Kotlin is a language that will be invented 5 years in your future to replace Java):

class VideoBitmapGetter(
    val textureView: TextureView,
    val videoSize: Size = EXPECTED_VIDEO_SIZE,  // Size of video image to get
){

    fun startVideoPlayback() {
        val mediaPlayer = MediaPlayer().apply { setDataSource(<INSERT DATA SOURCE HERE>); prepare() },
        mediaPlayer.isLooping = true
        textureView.layoutParams = RelativeLayout.LayoutParams(videoSize.width, videoSize.height)
        textureView.alpha = 0f  // Hides textureview without stopping playback
        textureView.surfaceTextureListener = (object : TextureView.SurfaceTextureListener {
            override fun onSurfaceTextureAvailable(surface: SurfaceTexture, width: Int, height: Int) {
                val surface = Surface(textureView.surfaceTexture)
                try {
                    mediaPlayer.setSurface(surface)
                    mediaPlayer.start()
                } catch (e: IOException) {
                    e.printStackTrace()
                }
            }
            override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture, width: Int, height: Int) {}
            override fun onSurfaceTextureDestroyed(surface: SurfaceTexture) = true
            override fun onSurfaceTextureUpdated(surface: SurfaceTexture) {}

        })
    }

    fun getLatestImage(): Bitmap? = textureView.getBitmap()
}

Upvotes: 0

Matthew
Matthew

Reputation: 44919

This is a pretty heavy undertaking, involving Android NDK development using something like ffmpeg and glbuffer. You could also bypass OpenGL and just write the ffmpeg-decoded frame to a bitmap in memory.

For getting video thumbnails, there is always ThumbnailUtils.

Upvotes: 1

nvloff
nvloff

Reputation: 91

No. The media player just setups a pipeline that is handled by lower level components. And depending on the hardware platform and the decoder setup the rendering is done in different places.

Upvotes: 2

Related Questions