gpt4 book ai didi

android - 使用 videoview 按下主页按钮后应用程序重新启动状态

转载 作者:太空狗 更新时间:2023-10-29 13:10:13 24 4
gpt4 key购买 nike

我有一个视频播放器,当我启动它并按下启动器图标并再次重新启动应用程序时,视频会从原来的位置开始播放。当我暂停视频并按下启动器图标并再次重新启动应用程序时。视频不在暂停状态。它再次从最后一个位置开始。提前致谢

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();
}
}

最佳答案

请尝试创建一个状态 bool 值来检查视频是否是手动暂停。

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));
}
}

你必须像这样手动处理你以前的状态。

希望这对你有帮助...

关于android - 使用 videoview 按下主页按钮后应用程序重新启动状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42037558/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com