Reputation: 695
Hi All I have got a video set as a background in my app. When the app starts the video plays at the main menu and everything works great. Now when I select to go to next activity the video stop and the next activity starts and when the user is then finished with this activity and presses the back button to go to go to the main menu the video should play again, however it doesn't. Hope some one can help me with this. here is my code:
public class MainActivity extends Activity {
VideoView animation;
private MediaController mc;
MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mp = MediaPlayer.create(this, R.raw.leftbanktwo);
mp.setLooping(true);
VideoView animation = (VideoView) findViewById(R.id.imageAnimation);
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/"+R.raw.cartoon);
mc = new MediaController(this);
animation.setMediaController(mc);
animation.requestFocus();
animation.setVideoURI(uri);
animation.start();
}
Upvotes: 0
Views: 505
Reputation: 60244
This is because your onCreate()
method is not called again when user back to first Activity
. If you want it to work like you described put the code which starts video in onResume()
method.
Also, I would recommend to check out Activity Lifecycle.
Upvotes: 2
Reputation: 28484
Try This
@Override
protected void onResume() {
super.onResume();
if(mp!=null){
mp.reset();
mp.start();
}
}
@Override
protected void onPause() {
super.onPause();
if(mp!=null && mp.isPlaying()){
mp.pause();
}
}
Upvotes: 1