在Android的TextureView中,播放一个视频后再播放另一个视频存在问题

5
使用TextureView内的MediaPlayer播放视频,它能正常工作。
当一个视频结束并且另一个视频应该播放时,我会调整TextureView的大小以适应即将播放的视频的宽高比(例如从3:4到9:16等)。
问题在于,第一个视频结束后,它的最后一帧仍然显示,然后调整TextureView的大小,第一个视频的最后一帧会短暂地保留,但现在宽高比不正确,第二个视频则正常播放。
视频的图像显示为(大小X大小)的矩形:
图片1: 视频1,(3:4):
图片2: 视频1,在两个视频之间,视频1的最后一帧,在调整大小后现在是(9:16),但矩形看起来很糟糕。下面的图片只显示了一瞬间:
图片3: 视频2,(9:16),在显示图片2短暂时间后正常播放。
代码没有什么特别之处,这里是其中的一些部分:
// 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();

将您的Texture View放置在相同的包装布局宽度和高度中,然后使用setLayoutParams()。它能解决问题。我曾经遇到过同样的问题,我是用这种方法解决的。我不知道它是如何工作的,但经过大量研究,对我很有效。如果需要进一步信息,请让我知道。 - user1154390
user1154390,您介意再详细解释一下吗? - Miko Diko
1
<FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextureView android:id="@+id/texture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top" /> </FrameLayout> 在应用变换后,textureView.setLayoutParams(new FrameLayout.LayoutParams(viewWidth, viewHeight)); - user1154390
Miko Diko,你尝试过上面的解决方案吗? - user1154390
显示剩余2条评论
1个回答

2

你有一些选择。

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

另一个选择是使用TextureView转换而不是自定义帧布局来设置您的纵横比。由于您的布局不会更改,因此不会出现扭曲帧的闪烁。请参见PlayMovieActivity中的adjustAspectRatio()以获取示例。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接