开发者

Get current frame from android mediaplayer

开发者 https://www.devze.com 2023-02-14 06:57 出处:网络
Is there a way to retr开发者_开发知识库ieve the current frame played from android MediaPlayer object? No.

Is there a way to retr开发者_开发知识库ieve the current frame played from android MediaPlayer object?


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.


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.


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()
}
0

精彩评论

暂无评论...
验证码 换一张
取 消