我有一个视频播放器,当我启动它并按启动器图标并再次重新启动应用程序时,视频开始播放它的位置。当我暂停视频并按启动器图标并再次重新启动应用程序。视频不在暂停状态。它再次从最后一个位置开始。提前谢谢
public class VideoFullscreenActivity extends Activity {
private static boolean startFromZero = false;
VideoView videoView;
private static boolean isplaying = false;
ProgressDialog pDialog;
SharedPreferences.Editor editor;
SharedPreferences prefs;
int restoredPosition;
public static boolean isStartFromZero() {
return startFromZero;
}
public static void setStartFromZero(boolean startFromZero) {
VideoFullscreenActivity.startFromZero = startFromZero;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_fullscreen);
videoView = (VideoView) findViewById(R.id.videoview);
editor = getSharedPreferences("VIDEO_POSITION", MODE_PRIVATE).edit();
prefs = getSharedPreferences("VIDEO_POSITION", MODE_PRIVATE);
if (startFromZero) {
editor.putInt("CURRENT_POSITION", 0);//stopPosition is an int
editor.commit();
} else {
setStartFromZero(false);
}
restoredPosition = prefs.getInt("CURRENT_POSITION", 0);
if (Utilities.checkInternetConnection(this)) {
Intent intent = getIntent();
pDialog = new ProgressDialog(this);
// Set progressbar message
pDialog.setMessage("Buffering...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
// Show progressbar
pDialog.show();
MediaController mediaController = new
MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
if(intent.getStringExtra(Constants.URL)!= null)
{
Uri videoUri = Uri.parse(intent.getStringExtra(Constants.URL));
videoView.setVideoURI(videoUri);
} else if (intent.getStringExtra("VideoUrl") != null)
{
Map<String, String> headers = new HashMap();
headers.put(APP_KEY, APPKEY_VALUE);
headers.put(SESSION_ID, SharedInstance.getInstance().getLoginDetails().getSessionId());
Uri uri = Uri.parse(intent.getStringExtra("VideoUrl"));
Method setVideoURIMethod = null;
try {
setVideoURIMethod = videoView.getClass().getMethod("setVideoURI", Uri.class, Map.class);
setVideoURIMethod.invoke(videoView, uri,headers);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
//videoView.setVideoURI(uri,headers);
}
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
pDialog.dismiss();
videoView.start();
}
});
if (prefs != null) {
restoredPosition = prefs.getInt("CURRENT_POSITION", 0); //0 is the default value.
}
videoView.seekTo(restoredPosition);
videoView.start();
Log.i("positionPlayback", "onCreate " + restoredPosition);
} else {
Utilities.showAlertLoginDialog(this, "", getResources().getString(R.string.internet_unavailable_msg));
}
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
onBackPressed();
}
});
//---------------------------------------------------------------//
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
this.onPrepared(mediaPlayer);
}
});
}
@Override
public void onPause() {
super.onPause();
Log.i("positionPlayback", "onPause " + restoredPosition);
if (videoView != null && videoView.isPlaying()) {
videoView.pause();
}
restoredPosition = videoView.getCurrentPosition();
editor.putInt("CURRENT_POSITION", restoredPosition);//stopPosition is an int
Utilities.hideKeybord(VideoFullscreenActivity.this);
}
@Override
public void onResume() {
super.onResume();
checkForCrashes();
startFromZero = false;
if (Utilities.checkInternetConnection(this)) {
Log.i("positionPlayback", "onResume " + restoredPosition);
pDialog.show();
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
pDialog.dismiss();
restoredPosition = prefs.getInt("CURRENT_POSITION", 0);
videoView.seekTo(restoredPosition);
videoView.start();
}
});
} else {
Utilities.showAlertLoginDialog(this, "", getResources().getString(R.string.internet_unavailable_msg));
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
private void checkForCrashes() {
CrashManager.register(this, BuildConfig.hockeyAppId);
}
@Override
protected void onStart() {
super.onStart();
if (prefs != null) {
SharedPreferences prefs = getSharedPreferences("VIDEO_POSITION", MODE_PRIVATE);
restoredPosition = prefs.getInt("CURRENT_POSITION", 0); //0 is the default value.
}
videoView.seekTo(restoredPosition);
videoView.start();
Log.i("positionPlayback", "onStart " + restoredPosition);
}
@Override
protected void onStop() {
super.onStop();
Log.i("positionPlayback", "onStop 1 " + restoredPosition);
if (videoView != null && videoView.isPlaying()) {
videoView.pause();
}
editor.putInt("CURRENT_POSITION", restoredPosition);//stopPosition is an int
editor.commit();
Log.i("positionPlayback", "onStop " + restoredPosition);
}
@Override
protected void onDestroy() {
editor.putInt("CURRENT_POSITION", restoredPosition);
editor.commit();
Log.i("positionPlayback", "onDestroy " + restoredPosition);
super.onDestroy();
}
@Override
protected void onRestart() {
super.onRestart();
SharedPreferences prefs = getSharedPreferences("VIDEO_POSITION", MODE_PRIVATE);
restoredPosition = prefs.getInt("CURRENT_POSITION", 0);
Log.i("positionPlayback", "onRestart " + restoredPosition);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
@Override
public void onBackPressed() {
editor.putInt("CURRENT_POSITION", 0);
editor.commit();
finish();
}
}
答案 0 :(得分:1)
在您添加值后,您需要提交<div class="topics">
<ul>
<%= @topics.each do |topic|>
<li> <%= topic.title %> </li>
</ul>
</div>
@ editor.commit();
!
答案 1 :(得分:1)
请尝试制作一个状态布尔值,以检查视频是否为手动暂停。
boolean manual_pause = false;
onPause中的检查视频状态是否会影响manual_pause。
@Override
public void onPause() {
super.onPause();
Log.i("positionPlayback", "onPause " + restoredPosition);
if (videoView != null && videoView.isPlaying()) {
videoView.pause();
manual_pause = false;
} else if(videoView != null){
manual_pause = true;
}
restoredPosition = videoView.getCurrentPosition();
editor.putInt("CURRENT_POSITION", restoredPosition);//stopPosition is an int
Utilities.hideKeybord(VideoFullscreenActivity.this);
}
然后在onResume()中检查此状态以设置之前的视频状态。
@Override
public void onResume() {
super.onResume();
checkForCrashes();
startFromZero = false;
if (Utilities.checkInternetConnection(this)) {
Log.i("positionPlayback", "onResume " + restoredPosition);
pDialog.show();
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
pDialog.dismiss();
restoredPosition = prefs.getInt("CURRENT_POSITION", 0);
videoView.seekTo(restoredPosition);
if(manual_pause) {
videoView.pause();
manual_pause = false;
}else{
videoView.start();
}
}
});
} else {
Utilities.showAlertLoginDialog(this, "", getResources().getString(R.string.internet_unavailable_msg));
}
}
你必须像这样manualy处理你以前的状态。
希望这会对你有帮助......