Vlad  Parker
Vlad Parker

Reputation: 49

How to start activity after VideoView end

I have Activity , in this activity i have video . After this video i wish run another activity . I try make stupid things in java :) but it doesnt work . Help me pls . My clear java code without my stupid ideas :) I hope you help me :)

package com.mem.good;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.VideoView;

public class MemActivity extends Activity {


        @Override

        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.main);

        VideoView videoView = (VideoView) findViewById(R.id.videoview);
        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(videoView);
        Uri video = Uri.parse("/sdcard/memapp/load.mp4");
        videoView.setVideoURI(video);
        videoView.start();
            }


        }

Upvotes: 2

Views: 6672

Answers (4)

Zar E Ahmer
Zar E Ahmer

Reputation: 34360

Simply add mediaplayer.setLooping(true) if you want to repeat this video. it will start repeating itself.

mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mp.setLooping(true);//for repeating the same Video
                    mVideoView.start();
                }
            });

Upvotes: -1

Danny
Danny

Reputation: 1

This one has a delay in case the video is still loading and not playing

new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Thread t = new Thread(){
                    public void run(){

                        while (video.isPlaying() == true)
                        {  
                            try{
                                sleep(1000);
                            }
                            catch(Exception e){};


                        }
                        Intent in = new Intent(getApplicationContext(),
                                Main_Activity.class);
                        startActivity(in);
                    }
                };
                t.start();

            }
        }, 2000);

Upvotes: 0

litterbugkid
litterbugkid

Reputation: 3666

You could maybe create a Thread which keeps checking whether videoView.isPlaying(). When it returns False, you could open up your new activity.

Thread t = new Thread(){
    public void run(){

        while (videoView.isPlaying() == True)
        {  
            try{
                sleep(1000);
            }
            catch(Exception e){};


        }
        Intent h = new Intent(this, new.class); 
        startActivity(h);
    }
};
t.start();

But I haven't used VideoView before so I don't know what other features it has!

Upvotes: 1

devunwired
devunwired

Reputation: 63293

This is what the MediaPlayer.OnCompletionListener is for. Checking if the video is playing won't tell you if the user paused it for some reason before it's finished.

Simply call setOnCompletionListener() on your VideoView and register a callback. The callback will be called both in the case where the video completes successfully, and if playback terminates due to an error. So...

videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    public void onCompletion(MediaPlayer mp) {
        //Start a new activity or do whatever else you want here
    }
});

You might also check the VideoView Documentation for other useful methods.

HTH.

Upvotes: 9

Related Questions