onResume()之后不显示MediaController

时间:2013-03-31 05:57:43

标签: android

这是我对视频播放器的活动:

public class myActivity extends Activity
         implements MediaPlayer.OnPreparedListener, 
                    MediaController.MediaPlayerControl {
....
}

并且在其onPause方法中,我不会发布MediaPlayer而我只是pause

@Override
protected void onPause() {
    super.onPause();
    lastPlaybackInfo.IsPlaying = mMediaPlayer.isPlaying();
    mMediaPlayer.pause();
    lastPlaybackInfo.SeekPosition = mMediaPlayer.getCurrentPosition();
}

并在onResume

@Override
protected void onResume() {
    super.onResume();
        if (mMediaPlayer != null) {
            if (lastPlaybackInfo.IsPlaying) {
                mMediaPlayer.start();
            } else {
                mMediaPlayer.seekTo(lastPlaybackInfo.SeekPosition);
            }
            mMediaController.show();
        }
}

和onTouchEvent:

@Override
public boolean onTouchEvent(MotionEvent event) {
            mMediaController.show();
            return true;
}

我有两个问题:

  1. 第一个问题是,一旦应用恢复,与MediaController绑定的MediaPlayer只会显示一次(onResume中的来电),并且之后,onTouchEvent中的来电不显示MediaController

  2. 第二个问题是,如果在应用暂停之前暂停MediaPlayer,则在恢复应用后,绑定到SurfaceView的{​​{1}}为黑色并且不显示最后一个渲染帧。

1 个答案:

答案 0 :(得分:0)

事实上,我发现如果我们在onResume内部运行代码会延迟让布局自行刷新,这两个问题都会得到解决。

以下是:

protected void onResume() {
    super.onResume();
    try {
        if (IsMediaReady && IsSubtitleReady) {
            if (mMediaPlayer != null) {

                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (lastPlaybackInfo.IsPlaying) {
                            mMediaPlayer.start();
                        } else {
                            mMediaPlayer.seekTo(lastPlaybackInfo.SeekPosition);
                        }
                        mMediaController.show();
                    }
                }, 100);

            }
        }
    } catch (NullPointerException e) {
        Log.e(TAG, "Exception in resume");
    }
}