如何在安卓中实现视频视图的暂停和恢复功能?



我想创建一个具有暂停和恢复功能的视频视图。因此,为了节省当前时间,我使用共享首选项作为临时存储。但是在调试时,它在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()。有关详细信息,请参阅文档。

最新更新