简体   繁体   中英

Videoview not starting when the user comes back to activity

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.

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

onPause() video.pause()上调用video.pause()覆盖你的活动方法,并在你活动的onResume()方法上调用video.resume()

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM