如何在按下主页按钮后在Android中播放mediaPlayer,然后接收电话,然后恢复该点的视频?

时间:2014-08-03 06:52:03

标签: java android android-mediaplayer

当我按下模拟器或手机的主页按钮时,你能告诉我如何在Android中播放mediaPlayer吗?我想要它,以便当它接到电话停止视频并能够再次恢复视频时。我应该如何在此代码中执行此操作?

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.MediaController;
import android.widget.VideoView;

public class VideoActivity2 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);  
setContentView(R.layout.activity_video2);

VideoView videoView = (VideoView)this.findViewById(R.id.videoView2);
videoView.setVideoURI(Uri.parse("android.resource://"+
getPackageName() +"/"R.raw.song1));
videoView.setMediaController(new MediaController(this));
videoView.requestFocus();
videoView.start();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.video_activity2, menu);
return true;
}

public void startVideoActivity3(View v) {
Intent intent = new Intent(VideoActivity2.this, VideoActivity3.class);
startActivity(intent);
}
public void startVideoActivity(View v) {
Intent intent = new Intent(VideoActivity2.this, VideoActivity.class);
startActivity(intent);
}
public void startMainActivity(View v) {
Intent intent = new Intent(VideoActivity2.this,    

MainActivity.class);intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
if (getIntent().getBooleanExtra("EXIT", false)) {
 finish();
}
startActivity(intent);
}

}

2 个答案:

答案 0 :(得分:1)

我从不使用VideoView类,但根据文档,这应该可行:

@Override
public void onPause() {
   super.onPause();
    VideoView videoView = (VideoView)findViewById(R.id.videoView2);
    videoView.pause();
}

@Override
public void onResume() {
   super.onResume();
   VideoView videoView = (VideoView)findViewById(R.id.videoView2);
   videoView.resume();
}

答案 1 :(得分:0)

添加到氰化物的答案,我发现videoView.pause()并不总是捕获视频的位置 - 导致黑色videoView onResume。通过添加videoStopPosition变量,可以避免此问题。

//Allows video to resume from same location as the pause
private static int videoStopPosition = 0;

@Override
public void onPause() {
   super.onPause();
   VideoView videoView = (VideoView)findViewById(R.id.videoView2);
   videoStopPosition = videoView.getCurrentPosition();
   videoView.pause();
}

@Override
public void onResume() {
   super.onResume();
   VideoView videoView = (VideoView)findViewById(R.id.videoView2);
   videoView.seekTo(videoStopPosition);
   videoView.resume();
}