我想创建一个具有暂停和恢复功能的视频视图。因此,为了节省当前时间,我使用共享首选项作为临时存储。但是在调试时它在onPause(),onStop(),onResume(),onStart()上工作得很好。但是当安装在设备中时,视频每次都从一个开头开始。如何解决这个问题?
@Override
protected void onResume() {
super.onResume();
resumeVideo();
}
@Override
protected void onPause() {
super.onPause();
saveCurrentTime();
}
@Override
protected void onStop() {
super.onStop();
saveCurrentTime();
}
@Override
protected void onRestart() {
super.onRestart();
resumeVideo();
}
//......to save current duration in shared preference
public void saveCurrentTime(){
String current_time = String.valueOf(vdoView.getCurrentPosition());
sharedPreference.putValue(this,Constants.SP_NAME,Constants.CURRENT_TIME,current_time);
}
//to resume video from given time
public void resumeVideo(){
String time = sharedPreference.getValue(this, Constants.SP_NAME, Constants.CURRENT_TIME);
if(!sharedPreference.getValue(this,Constants.SP_NAME,Constants.CURRENT_TIME).equals("")) {
int t = Integer.parseInt(time);
vdoView.seekTo(t);
}
vdoView.start();
}
很明显,你的额外处理程序会在意外时间用0覆盖首选项,因为vdoView
已经改变了状态。
删除你的onStop()
和onRestart()
(和onDestroy()
)处理程序。你需要的只是onPause()
和onResume()
。有关详细信息,请参阅the documentation。