我有一个带有新闻Feed的应用,其中可以有多种内容类型。我正在使用RecyclerView作为列表。
我遇到问题,如果Feed中有视频播放,用户将应用程序发送到后台,然后从最近的列表中重新启动应用程序,请再次按播放,视频大小错误。
我附上了几张图片来说明问题:
这是应用程序启动并正常播放视频的时间
这是在被发送到后台然后从最近的列表中启动之后。 (根据要求手动启动视频)
这是在第二次将应用程序发送到后台并从最近列表重新发送之后。
因此,当应用程序被发送到后台时,onSurfaceTextureDestroyed
被调用。
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
surface.release();
L.i("SURFACE TEXTURE ", "DESTROYED: " + String.valueOf(surface));
if(!mFullScreen) {
if(mMediaPlayer!=null) {
mCurrentPosition = mMediaPlayer.getCurrentPosition();
releaseMediaPlayer();
}
}
所以我在这里打电话给发布媒体播放器。
//Stop Media player and release resources
public void releaseMediaPlayer(){
if(mMediaPlayer!=null){
if(mMediaPlayer.isPlaying()){
mMediaPlayer.stop();
}
mCurrentPosition = mMediaPlayer.getCurrentPosition();
mMediaPlayer.reset();
mMediaPlayer.release();
mMediaPlayer = null;
}
}
我播放视频的方法如下:
public void startVideo(String url) {
mUrl = url;
if (mMediaPlayer != null) {
mMediaPlayer.reset();
mMediaPlayer.release();
mMediaPlayer = new MediaPlayer();
}
else {
mMediaPlayer = new MediaPlayer();
}
try {
L.i(getClass().getName(), mUrl);
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.setOnBufferingUpdateListener(this);
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setOnInfoListener(this);
mMediaPlayer.setOnErrorListener(this);
mMediaPlayer.setSurface(new Surface(mSurface));
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setDataSource(url);
mMediaPlayer.prepareAsync();
}
catch (IOException e) {
e.printStackTrace();
}
}
然后在我的onPrepared
方法
@Override
public void onPrepared(MediaPlayer mp) {
mTextureView.setOnTouchListener(new ControlTouchListener());
mControllerView.setMediaPlayer(this);
mControllerView.setAnchorView(mAnchorView);
L.i(getClass().getSimpleName(), "Video Height: " + String.valueOf(mMediaPlayer.getVideoHeight()) + "Video Width: " + String.valueOf(mMediaPlayer.getVideoWidth()));
mMediaPlayer.getVideoHeight();
if (mMimeType != null && mMimeType.contains("audio")) {
mProgress.setVisibility(View.GONE);
mControllerView.setTimeout(mMediaPlayer.getDuration());
mControllerView.getFullScreenButton().setVisibility(View.GONE);
}
else {
mControllerView.setTimeout(3000);
}
if (!mFullScreen) {
if (mMimeType.contains("video")) {
MediaUtils.handleVideoAspectRatio(mp, mTextureView, mControllerView);
}
else {
mMediaPlayer.start();
mControllerView.updatePausePlay();
}
}
else {
mMediaPlayer.start();
mControllerView.updatePausePlay();
}
}
我还有一个handleVideoAspectRatio方法,以确保视频是否是纵向的:
public static void handleVideoAspectRatio(MediaPlayer mediaPlayer, TextureView textureView, MediaControllerView controllerView) {
int textureViewHeight = textureView.getHeight();
L.i("HANDLE ASPECT RATIO: " , "TEXTURE VIEW HEIGHT: "+String.valueOf(textureViewHeight));
int textureViewWidth = textureView.getWidth();
L.i("HANDLE ASPECT RATIO: " , "TEXTURE VIEW WIDTH: "+String.valueOf(textureViewWidth));
float videoHeight = mediaPlayer.getVideoHeight();
float videoWidth = mediaPlayer.getVideoWidth();
float ratioHeight = textureViewHeight/videoHeight;
float ratioWidth = textureViewWidth/videoWidth;
float aspectRatio = videoWidth/videoHeight;
ViewGroup.LayoutParams textureViewLayoutParams = textureView.getLayoutParams();
if(ratioWidth>ratioHeight){
textureViewLayoutParams.width = (int)(textureViewHeight * aspectRatio);
textureViewLayoutParams.height = textureViewHeight;
}else{
textureViewLayoutParams.width = textureViewWidth;
textureViewLayoutParams.height = (int)(textureViewWidth * aspectRatio);
}
textureView.setLayoutParams(textureViewLayoutParams);
mediaPlayer.start();
controllerView.updatePausePlay();
}
我试图在播放之前用布局参数重置纹理视图的大小,以防回收者视图保留上一个视频中的参数但不起作用
任何人都可以帮忙告诉我为什么会这样吗?