我的要求是用动画改变视频播放器内的视频。当一个人点击“更改”按钮时,媒体播放器中的视频将用新的视频进行更改,而旧的视频最终会放大动画。我可以在纹理视图的帮助下实现此功能,但我遇到了一个问题,即更改视频时出现故障。有什么办法可以让它更顺畅吗?
这是我的代码:
public class VideoVieww extends Activity implements TextureView.SurfaceTextureListener {
private TextureView textureView;
private MediaPlayer mMediaPlayer;
private Button mButton;
private Animation zoomAnimation;
private SurfaceTexture mSurfaceTexture;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.texture_layout);
textureView = findViewById(R.id.textureView);
textureView.setSurfaceTextureListener(this);
zoomAnimation = AnimationUtils.loadAnimation(VideoVieww.this, R.anim.zomm_out);
mButton = findViewById(R.id.change);
mButton.setOnClickListener(view -> {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
textureView.startAnimation(zoomAnimation);
try {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(VideoVieww.this, Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.broadchurch));
} catch (IOException e) {
e.printStackTrace();
}
try {
Surface surface = new Surface(mSurfaceTexture);
mMediaPlayer.setSurface(surface);
mMediaPlayer.prepare();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
});
zoomAnimation.setAnimationListener(new Animation.AnimationListener(){
@Override
public void onAnimationStart(Animation arg0) {
}
@Override
public void onAnimationRepeat(Animation arg0) {
}
@Override
public void onAnimationEnd(Animation arg0) {
mMediaPlayer.start();
}
});
}
@Override
public void onBackPressed() {
super.onBackPressed();
mMediaPlayer.release();
mMediaPlayer=null;
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
mSurfaceTexture=surfaceTexture;
Surface surface = new Surface(surfaceTexture);
try {
mMediaPlayer= new MediaPlayer();
mMediaPlayer.setDataSource(VideoVieww.this, Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.sid));
mMediaPlayer.setSurface(surface);
mMediaPlayer.prepare();
mMediaPlayer.start();
// mMediaPlayer.setLooping(true);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
}
任何帮助将不胜感激!!!