我正在使用 VideoView 播放视频。如果我退出应用程序,在返回应用程序时,即在 onResume() 中,它应该从停止的位置播放视频。
问问题
3200 次
2 回答
3
要获得当前进度(在 onPause 中检查):
long progress = mVideoView.getCurrentPosition();
恢复(在 onResume 中):
mVideoView.seekTo(progress);
于 2012-06-05T11:20:41.573 回答
2
在 onPause() 中,保存播放器的当前位置,例如在共享偏好中。在 onResume() 中,检索值,然后使用 MediaPlayer.seekTo() 定位。
http://developer.android.com/reference/android/media/MediaPlayer.html#seekTo(int)
@Override
protected void onPause() {
Log.d(App.TAG, "onPause called");
if(mMediaPlayer==null){
Log.d(App.TAG, "Returning from onPause because the mediaplayer is null");
super.onPause();
return;
}
// the OS is pausing us, see onResume() for resume logic
settings = getSharedPreferences(Dawdle.TAG, MODE_PRIVATE);
SharedPreferences.Editor ed = settings.edit();
mMediaPlayer.pause();
ed.putInt("LAST_POSITION", mMediaPlayer.getCurrentPosition()); // remember where we are
ed.putBoolean("PAUSED", true);
ed.commit();
Log.d(App.TAG, "LAST_POSITION saved:" + mMediaPlayer.getCurrentPosition());
super.onPause();
releaseMediaPlayer();
}
@Override
public void onResume() {
Log.d(App.TAG, "onResume called");
try {
if (mMediaPlayer==null){
setupMediaPlayer();
}
// if we were paused (set in this.onPause) then resume from the last position
settings = getSharedPreferences(Dawdle.TAG, MODE_PRIVATE);
if (settings.getBoolean("PAUSED", false)) {
// resume from the last position
startPosition= settings.getInt("LAST_POSITION", 0);
Log.d(App.TAG,"Seek to last position:" + startPosition);
}
mMediaPlayer.setDataSource(path);
mMediaPlayer.setDisplay(holder);
// this is key, the call will return immediately and notify this when the player is prepared through a callback to onPrepared
// so we do not block on the UI thread - do not call any media playback methods before the onPrepared callback
mMediaPlayer.prepareAsync();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void startVideoPlayback() {
Log.v(App.TAG, "startVideoPlayback at position:" + startPosition);
mMediaPlayer.seekTo(startPosition);
mMediaPlayer.start();
}
于 2012-06-05T11:31:01.373 回答