简体   繁体   English

当用户返回活动时,Videoview无法启动

[英]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). 问题是视频视图在首次创建活动时再现视频,而不是当用户从其他活动返回时(在我的情况下是MenuSection活动)。 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. MenuSection只是一个显示文本视图的活动,如“Hello world”,所以我没有粘贴它。

Move mVideoView.start(); 移动mVideoView.start(); to onResume() instead of onCreate() as: onResume()而不是onCreate()为:

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

see Managing the Activity Lifecycle onResume() is called from your Activity when Activity is Already Running 请参阅管理活动生命周期 onResume()在活动已经运行时从您的活动中调用

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

Move mVideoView.start(); 移动mVideoView.start(); to onStart() , instead of onCreate() . onStart() ,而不是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(); 我不确定,但您可能还需要移动setVideoURI(); to onStart() as well. onStart()也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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