1

I'm starting a project and I want to reproduce a video in the main activity when the app is executed, when the user presses the video it goes to another activity. If the user press the back button, he is going to the main screen again and reproduces the video from the beginning. The video is located in the raw directory.

The problem is that the videoview is reproducing the video when the activity is first created but not when the user goes back to it from the other activity (In my case the MenuSection activity). The code is really simple but i will paste it anyway:

 public class MainActivity extends Activity {
   private VideoView mVideoView;
   LinearLayout menuSection;
   @Override

   public void onCreate(Bundle icicle) {
      super.onCreate(icicle);
      setContentView(R.layout.main);
      mVideoView = (VideoView) findViewById(R.id.surface_view);
      mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() +"/"+R.raw.documentariesandyou));
      mVideoView.requestFocus();
      mVideoView.setMediaController(null); //i dont want the controls of the videoview.
      mVideoView.start();
      menuSection = (LinearLayout) findViewById(R.id.menuSection);
      menuSection.setOnClickListener(new menuSectionListener());

   }

class menuSectionListener implements OnClickListener {
    public void onClick(View v) {
        Intent staticActivityIntent = new Intent(MainActivity.this, MenuSection.class);
        startActivity(staticActivityIntent);        
    }
}

}

The MenuSection is just an activity that shows a textview like "Hello world", so I'm not pasting it.

AdolfoFermo
  • 107
  • 1
  • 8

3 Answers3

2

Move mVideoView.start(); to onResume() instead of onCreate() as:

@Override
    protected void onResume() {
        super.onResume();
        mVideoView.start();
    }

see Managing the Activity Lifecycle onResume() is called from your Activity when Activity is Already Running

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • onStart() or onResume() would both work. But I have found that using onResume() will lead to the video being restarted sometimes when it is unintended at least in my own applications with videos. – FoamyGuy Jul 04 '12 at 19:47
  • Thanks, the question was really easy. But I'm kind of new with android and I'm getting used to all the processes of the activities and stuff. Thanks a lot! – AdolfoFermo Jul 04 '12 at 20:06
1

call video.pause() on onPause() overrided method of your activity and call video.resume() on onResume() method of your activity.

Aqif Hamid
  • 3,511
  • 4
  • 25
  • 38
0

Move mVideoView.start(); to onStart(), instead of onCreate().

See Activity Lifecycle in the developer docs for more info on how the lifecycle of Activities work.

I am not certain, but you may also need to move the setVideoURI(); to onStart() as well.

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
  • how onStart() help in this case? – ρяσѕρєя K Jul 04 '12 at 19:38
  • @imrankhan because onCreate() only gets called if the Activity is not already in memory. onStart() gets called just before the activity is shown on the screen, which in this case is when the user is navigating back to it from somewhere else. – FoamyGuy Jul 04 '12 at 19:45