Starting Activity when video is finished playing

六眼飞鱼酱① 提交于 2020-01-04 11:11:13

问题


In my Android app, I am trying to simply go back to my main Activity once a video that I am playing ends. I have tried many workarounds, but I can't find a way to call StartActivity from the video onCompletionListener - I am getting the "cannot make a static reference to the non-static method startActivity(Intent) from the type Activity" error.

I tried getting a context from the Activity that preceded the videoView, and passing that to the intent/startActivity. That allowed the app to compile, but then I got a runtime exception.

Here is the code as it stands now, which gets the "cannot make a static reference" error - any help would be appreciated!

public class Videoscreen extends Activity{

public static VideoView myVideoView;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.videoplay);
    myVideoView = (VideoView) findViewById(R.id.main_videoview);
    System.out.println("playing video oncreate");
    playVideo();
}
public static void playVideo(){

    // video finish listener
    myVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer vmp) {
            Intent intent = new Intent();
            intent.setClass(Videoscreen.this, Game.class);
            Videoscreen.startActivity(intent);
        }
    }); 

    String low_word = SpellingView.get_low_word();
    Uri bubblesUri = Uri.parse("android.resource://org.lalloinc.ilovetrucks/raw/"+ low_word + "_vid");
    myVideoView.setVideoURI(bubblesUri);
    myVideoView.start();

}

}


回答1:


If you know the time of the video then you try:

String uri1 = "android.resource://" + getPackageName() + "/" + R.raw.race3;
        vd.setVideoURI(Uri.parse(uri1)); 
        vd.start();

new Thread() {
             public void run() {
                     try{

                             sleep(50000);
                     } catch (Exception e) {

                     }
                   Intent intent = new Intent(Video.this, Another.class);
                   startActivity(intent);
                   finish();
             }
     }.start();

if you don't know the time then you get the time as:

int vtime = vd.getDuration();

And then at thread sleep you just put this integer.




回答2:


If you started the Video Activity from the Activity you would like to go back to later, just calling finish() at the end of the video will do the job.

Starting the main Activity again creates a not necessarily wanted stack of activities.



来源:https://stackoverflow.com/questions/7315279/starting-activity-when-video-is-finished-playing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!