在Android的TextureView中观看视频后播放视频存在问题

时间:2014-11-04 17:56:08

标签: android video android-mediaplayer playback textureview

使用TextureView内的MediaPlayer播放视频,它可以正常工作。

当视频结束并播放另一个视频时,我会调整TextureView的大小以适应即将到来的视频的宽高比(例如 - 从3:4到9:16等等。)。

问题,在第一个视频结束后,它的最后一帧仍然显示, 比调整TextureView的大小,第一个视频的最后一帧保持一瞬间但现在具有不良的宽高比,第二个视频播放得很好。

显示(尺寸X尺寸)矩形的视频图像:

IMAGE1: 视频1,(3:4):

enter image description here

图像2: 视频1,这里我们介于两个视频之间,视频1的最后一帧,现在调整大小(9:16),但矩形看起来很糟糕。下面的图片会暂时显示:

enter image description here

的Image3: 视频2,(9:16),播放效果不错但在瞬间显示image2之后:

enter image description here

使用的代码没有什么特别之处,以下是它的一些部分:

// To init the TextureView and MediaPlayer:

public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
    m_MediaPlayer = new MediaPlayer();
    m_Surface = new Surface(surfaceTexture);
    m_MediaPlayer.setSurface(m_Surface);
}

m_TextureView.setSurfaceTextureListener(this);

// To Resize:
LayoutParams layoutParams = new FrameLayout.LayoutParams(newTVWidth, newTVHeight);
layoutParams.gravity = Gravity.CENTER;
m_TextureView.setLayoutParams(layoutParams);

// To play:
m_MediaPlayer.reset();
m_MediaPlayer.setDataSource(videoFilePath);
m_MediaPlayer.prepare();
// <--- Here comes a call to "To Resize" which is shown right above this code
m_MediaPlayer.start();

1 个答案:

答案 0 :(得分:2)

你可能有几个选择。

理论上,您可以通过断开媒体播放器,使用GLES清除曲面,然后再次连接媒体播放器来清除Surface。请参阅Grafika PlayMovieSurfaceActivity中的clickPlayStop()clearSurface()。这使用的是MediaCodec,而不是MediaPlayer,因此我不确定它是如何翻译的。

另一种选择是使用TextureView转换而不是自定义框架布局来设置纵横比。由于您的布局没有变化,因此没有失真的帧闪烁。有关示例,请参阅PlayMovieActivity中的adjustAspectRatio()

相关问题