我正在开发一款在可扩展屏幕上播放视频的Android应用。 对于容器,我使用的是包含TextureView的片段。
在TextureView中,我使用MediaPlayer对象播放视频。我已经重写了UserVideoFragment类的onPause()和onResume()方法来暂停/恢复视频。
逻辑是,如果缩小视频,那么即使返回到屏幕,视频也应暂停。 如果视频是全屏的,那么视频应该播放。
我面临的问题是当用户回家时,然后返回到按比例缩小模式片段的活动 mediaplayer TextureView显示黑屏。
这是代码
MainActivity.java
private boolean recordingVideo;
private float scaleFactor;
private int marginPixels;
private long animDuration;
private Button btnRecord;
private FragmentManager fragmentManager;
private UserVideoFragment fragmentUserVideo;
private ExpertRecordingFragment fragmentExpertRecording;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
UIUtils.hideActionAndStatusBar(getSupportActionBar(), getWindow());
init();
btnRecord = (Button) findViewById(R.id.btnRecord);
fragmentManager = getFragmentManager();
btnRecord.setOnClickListener(new RecordButtonListener());
}
private void init(){
recordingVideo = false;
scaleFactor = 0.2f;
marginPixels = 32;
animDuration = 250;
}
private class RecordButtonListener implements View.OnClickListener{
@Override
public void onClick(View view) {
fragmentUserVideo = (UserVideoFragment) fragmentManager.findFragmentById(R.id.fragmentUserVideo);
fragmentExpertRecording = (ExpertRecordingFragment) fragmentManager.findFragmentById(R.id.fragmentExpertRecording);
View userVideoView = fragmentUserVideo.getView();
if(!recordingVideo){
animateView(userVideoView, 0f, userVideoView.getHeight(), scaleFactor, marginPixels, marginPixels, animDuration);
}
if(recordingVideo){
animateView(userVideoView, 0f, userVideoView.getHeight(), 1f, 0, 0, animDuration);
}
recordingVideo = !recordingVideo;
fragmentUserVideo.onExpertVideoRecordingStarted(recordingVideo);
fragmentExpertRecording.onExpertVideoRecordingStarted(recordingVideo);
}
}
private void animateView(View view, float pivotX, float pivotY,
float scalingFactor, int marginPixelsX, int marginPixelsY, long duration){
view.setPivotX(pivotX);
view.setPivotY(pivotY);
view.animate()
.scaleX(scalingFactor)
.scaleY(scalingFactor)
.x(marginPixelsX)
.y(-marginPixelsY)
.setDuration(duration);
}
public interface OnExpertVideoRecordingStartedListener{
public void onExpertVideoRecordingStarted(boolean recording);
}
UserVideoFragment.java
private MediaPlayer mediaPlayer;
private TextureView userVideoTextureView;
private boolean mediaSourceSet;
private int mediaPausePosition;
private boolean recording;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_user_video, container, false);;
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnCompletionListener(new MediaPlayerOnCompletionListener());
mediaSourceSet = false;
mediaPausePosition = 0;
recording = false;
userVideoTextureView = (TextureView) view.findViewById(R.id.userVideoTextureView);
userVideoTextureView.setSurfaceTextureListener(new UserVideoTextureViewListener());
return view;
}
@Override
public void onPause(){
super.onPause();
if(mediaPlayer != null && mediaPlayer.isPlaying()){
mediaPlayer.pause();
}
}
@Override
public void onResume(){
super.onResume();
if(mediaPlayer != null){
if(recording) {
mediaPlayer.seekTo(mediaPausePosition);
userVideoTextureView.requestLayout();
}
if(!recording){
mediaPlayer.start();
}
}
}
@Override
public void onDestroy(){
if(mediaPlayer != null){
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
super.onDestroy();
}
private class UserVideoTextureViewListener implements TextureView.SurfaceTextureListener{
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
Surface mediaSurface = new Surface(surfaceTexture);
try{
if(!mediaSourceSet){
//mediaPlayer.setDataSource("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");
mediaPlayer.setDataSource("http://videocdn.bodybuilding.com/video/mp4/62000/62792m.mp4");
mediaPlayer.setSurface(mediaSurface);
mediaPlayer.prepare();
UIUtils.adjustAspectRatio(userVideoTextureView, mediaPlayer.getVideoWidth(), mediaPlayer.getVideoHeight());
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
mediaSourceSet = true;
}
if(mediaSourceSet){
mediaPlayer.setSurface(mediaSurface);
mediaPlayer.prepareAsync();
UIUtils.adjustAspectRatio(userVideoTextureView, mediaPlayer.getVideoWidth(), mediaPlayer.getVideoHeight());
}
}
catch(IOException e){
e.printStackTrace();
}
catch(IllegalStateException e){
e.printStackTrace();
}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
}
}
private class MediaPlayerOnCompletionListener implements MediaPlayer.OnCompletionListener{
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
}
}
public void onExpertVideoRecordingStarted(boolean recording){
if(recording){
if(mediaPlayer != null && mediaPlayer.isPlaying()){
mediaPlayer.pause();
mediaPausePosition = mediaPlayer.getCurrentPosition();
}
}
if(!recording){
if(mediaPlayer != null && !mediaPlayer.isPlaying()){
mediaPlayer.start();
}
}
this.recording = recording;
}