视频播放完毕后启动 Activity

发布于 2024-12-02 20:39:21 字数 1302 浏览 1 评论 0原文

在我的 Android 应用程序中,当我正在播放的视频结束时,我试图简单地返回到我的主要活动。我尝试了很多解决方法,但找不到从视频 onCompletionListener 调用 StartActivity 的方法 - 我收到“无法从 Activity 类型对非静态方法 startActivity(Intent) 进行静态引用”错误。

我尝试从 videoView 之前的 Activity 获取上下文,并将其传递给 Intent/startActivity。这允许应用程序编译,但随后我遇到了运行时异常。

这是现在的代码,它出现“无法进行静态引用”错误 - 任何帮助将不胜感激!

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();

}

}

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();

}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瀟灑尐姊 2024-12-09 20:39:22

如果您知道视频的时间,那么您可以尝试:

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();

如果您不知道时间,那么您得到的时间为:

int vtime = vd.getDuration();

然后在线程睡眠时,您只需输入这个整数。

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.

坏尐絯 2024-12-09 20:39:22

如果您从要稍后返回的 Activity 启动了 Video Activity,只需在视频结束时调用 finish() 即可完成任务。

再次启动主 Activity 会创建一个不一定需要的 Activity 堆栈。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文