Reputation: 11
Can you tell me how to play mediaPlayer in Android when I press the home button of emulator or phone? I want it so that when it receives a phone call to stop the video and be able to resume video again that point. How should I do this in this code?
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);
}
}
Upvotes: 1
Views: 833
Reputation: 3280
Adding to cyanide's answer, I found the videoView.pause() to not always capture the position of the video - resulting in a black videoView onResume. By adding the videoStopPosition variable this issue is avoided.
//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();
}
Upvotes: 0
Reputation: 3964
I never use VideoView class, but according to documentation, this should work:
@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();
}
Upvotes: 1