I have a VideoView and it is pause when I start an Email intent. When the email intent is done, I want the videoView to continue playing, however, it restarts from the beginning.
#Override
public void onPause() {
Log.d(TAG, "onPause called");
super.onPause();
videoView.pause();
}
#Override
public void onResume() {
super.onResume();
Log.d(TAG, "onResume called");
videoView.start();//resume() doesnt work
}
How can I get the videoView to resume from where it left off.
What about this:
#Override
public void onResume() {
super.onResume();
Log.d(TAG, "onResume called");
videoView.seekTo(stopPosition);
videoView.start(); //Or use resume() if it doesn't work. I'm not sure
}
// This gets called before onPause so pause video here.
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
stopPosition = videoView.getCurrentPosition();
videoView.pause();
outState.putInt("position", stopPosition);
}
Then in your onCreate() call the stopPosition from the bundle and set it globally
#Override
protected void onCreate(Bundle args) {
super.onCreate(args);
if( args != null ) {
stopPosition = args.getInt("position");
}
Using seekTo gives a slight flickering efeect while resuming the video. Better approach would be using pause() and start() methods of MediaPlayer class instead of using methods from VideoView. The start() method of VideoView restarts the video from the beginning, but start() method of MediaPlayer resumes the video if the pause() method had been previously called.
Here is what official android doc says
public void start ()
Added in API level 1 Starts or resumes playback. If playback had
previously been paused, playback will continue from where it was
paused. If playback had been stopped, or never started before,
playback will start at the beginning.
You can get the MediaPlayer associated with a VideoView in OnPreparedListener of VideoView.
public class MainActivity extends Activity {
private MediaPlayer mMediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView videoView = (VideoView) findViewById(R.id.videoView1);
videoView.setVideoPath(path);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mMediaPlayer = mp;
}
});
}
}
Then subsequent pause() and resume() methods can be called on MediaPlayer object itself.
//To pause the video
mMediaPlayer.pause();
//To resume the video
mMediaPlayer.start();
Very simple yet efficient solution. Hope it helps!
Related
I'm trying to play music in all activities in my app but when I switch between mainactivity and another activity music stopped please someone help me without using Service
MainAcivity :
ToggleButton MusicButton;
MediaPlayer mediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.vaporv2);
if(!mediaPlayer.isPlaying()){
mediaPlayer.start();
}
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mediaPlayer.start();
}
});
//code
#Override
protected void onPause() {
super.onPause();
if(mediaPlayer!=null && mediaPlayer.isPlaying()){
mediaPlayer.pause();
}
}
#Override
protected void onResume() {
super.onResume();
if(mediaPlayer!=null && !mediaPlayer.isPlaying()){
if(MusicButton.isChecked()){
mediaPlayer.pause();
}
else
mediaPlayer.start();
}
}
I don't know what I can do in the other activity.
You can pass the current seek position using extras, but the Music may stop while switching between activities.
I suggest that you use service then in each activity onPause event you check if you are switching to another activity in your application or not, if not send some broadcast to the service to stop playing music.
I have been working on playing videos using VideoView in Android.
OnPause of fragment i am pausing the video and storing the seektime
#Override
public void onPause() {
super.onPause();
if (videoview != null && videoview.isPlaying()) {
videoview.pause();
seekTime = videoview.getCurrentPosition();
}
}
OnResume i am trying to resume the video using -
#Override
public void onResume() {
super.onResume();
videoview.seekTo(seekTime);
//vvOnboardingVideos.resume();// this is not working for now - need to investigate
videoview.start();
}
The video plays from the beginning.
Is there anything i am doing wrong. Please suggest.
Try to reverse order of operations :
#Override
public void onResume() {
super.onResume();
videoview.start();
videoview.seekTo(seekTime);
}
try this:
save the video position when onpause() called
// This gets called before onPause
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
stopPosition = videoView.getCurrentPosition();
videoView.pause();
outState.putInt("video_position", stopPosition);
}
Then in your onCreate() load the stopPosition from the bundle
#Override
protected void onCreate(Bundle args) {
super.onCreate(args);
if( args != null ) {
stopPosition = args.getInt("video_position");
}
}
Resume the video using:
#Override
public void onResume() {
super.onResume();
Log.d("Tag", "onResume");
videoView.seekTo(stopPosition);
videoView.start(); //Or use videoView.resume() if it doesn't work.
}
I am creating a game, I want to play a background music for one activity only(For main menu of game), my code shown below, The problem is that the music plays more than one time, I want to play the same music also when activity Resumes.
public class Menu extends Activity {
MediaPlayer mp
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
mp = MediaPlayer.create(Menu.this, R.raw.adalante);
if(!mp.isPlaying()) {
mp.start();
}
public void play(View ButtonClicked) {
mp.stop();
mp.release();
//mp = MediaPlayer.create(Menu.this, R.raw.l);
//mp.start();
goToActivity(Game.class);
}
#Override
public void onResume() {
super.onResume(); // Always call the superclass method first
//coins
coin.setText(data.getString("coin"));
mp = MediaPlayer.create(Menu.this, R.raw.adalante);
if(!mp.isPlaying()) {
mp.start();
}
//mps.release();
}
In your onResume don't initialise MediaPlayer again and again. It creates new instance every time when you come to onResume. So add a check in onResume like this :
#Override
protected void onResume() {
super.onResume();
if (mp==null)
mp=MediaPlayer.create(MainActivity.this,R.raw.adalante);
if (!mp.isPlaying())
mp.start();
}
and additionally add this for prevention to play when activity goes to onPause
#Override
protected void onPause() {
super.onPause();
mp.pause();
}
I have a videoview that plays as a background. How do I pause it when my app looses focus and resume the video when the app has focus again? Here is my code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
VideoView videoView = (VideoView) findViewById(R.id.launcherVideo);
Uri src = Uri.parse("android.resource://com.package/raw/video");
videoView.setVideoURI(src);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setVolume(0, 0);
mp.setLooping(true);
}
});
//videoView.setMediaController(new MediaController(this));
videoView.start();
}
override onPause() method and write code for pause on that method and override onResume() method and write code for resume on that method
I managed to get my video to play with VideoView but after I rotate my device video stops and starts playing from the beginning. I placed my VideoView inside fragment.
I tried this inside my fragment but without success:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public void onPause() {
super.onPause();
if(video.isPlaying()) {
video.pause();
progress = video.getCurrentPosition();
}
}
#Override
public void onResume() {
super.onResume();
if(video.isPlaying() && progress!=0) {
video.seekTo(progress);
video.start();
}
}
How to keep video playing after screen rotates?