0

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

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
MByD
  • 135,866
  • 28
  • 264
  • 277

3 Answers3

2

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.

nvloff
  • 91
  • 3
  • Thanks. Is there another way to get a frame from the played video (while played) using java? Maybe a way to redirect the video outside of the device? – MByD Mar 01 '11 at 14:24
  • No java APIs. Even SurfaceFlinger just creates the surface and passes size and possition of the surface to the renderer. One way to get the video out is to make the android device a video streaming server. Maybe there are some apps for that(i'm guessing VLC) I'm not really sure. Other way is HDMI output. – nvloff Mar 01 '11 at 16:06
  • Thanks for your answer, but I don't want to play content that I already have on the device on the PC, but to catch the frame that is played on the device while my application (which calls MediaPlayer) is running. – MByD Mar 01 '11 at 16:36
1

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.

Matthew
  • 44,826
  • 10
  • 98
  • 87
0

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()
}
Peter
  • 12,274
  • 9
  • 71
  • 86