Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect when videoview stops playing

I have this code for playing a video on a VideoView

myVideoView = (VideoView) findViewById(R.id.VideoView);
myVideoView.setVideoURI(Uri.parse(path));
myVideoView.requestFocus();
myVideoView.start();

When video is finish playing I get message :" Sorry, this video can't be played". I want to cache the moment when video is finish playing. How can I do this?

Here is my code :

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.video);

        Bundle extras = getIntent().getExtras();
        path = extras.getString("path");
        idPlaylist = extras.getString("idPlaylist");
        timer = extras.getString("timer");

        myVideoView = (VideoView) findViewById(R.id.VideoView);
        myVideoView.setVideoURI(Uri.parse(path));
        myVideoView.requestFocus();
        myVideoView.start();

        myVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            public void onCompletion(MediaPlayer mp) {
                Intent i = new Intent(getBaseContext(),
                        com.PlayerOrange.ViewPlaylist.class);
                i.putExtra("id", idPlaylist);
                i.putExtra("timer", timer);
                startActivity(i);
                finish();           
                }
          });

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        System.out.println("Playing "+myVideoView.isPlaying());
        if ((keyCode == KeyEvent.KEYCODE_BACK) && (myVideoView.isPlaying()==true)) {
                //nothing happens
        } else {
            Intent i = new Intent(getBaseContext(),
                    com.PlayerOrange.ViewPlaylist.class);
            i.putExtra("id", idPlaylist);
            i.putExtra("timer", timer);
            startActivity(i);
            finish();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }
like image 671
Gabrielle Avatar asked Nov 25 '11 08:11

Gabrielle


1 Answers

Have you tried?

myVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

    @Override
    public void onCompletion(MediaPlayer vmp) {
        Intent i = new Intent(getBaseContext(),
                    com.PlayerOrange.ViewPlaylist.class);
            i.putExtra("id", idPlaylist);
            i.putExtra("timer", timer);
            startActivity(i);
            currentActivityName.finish();           
    }
});  

Also make sure your myVideoView not to have as static. If yes, then remove static..

More info here

Look at Starting Activity when video is finished playing

Android – Video/Music player sample

Playing Video

OnCompletion video notification on MediaController and VideView usage?

Android - Listener (or handler) for video finish

like image 160
user370305 Avatar answered Nov 03 '22 00:11

user370305