Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - changing/replaying video in videoView

App I'm developing contains many short (1-2 sec) videos.

The videos are displayed in one activity. User can either replay video (possibly while video is beeing played) or change actual video.

Part of code changing video:

String videoPath = getVideoPath();
videoView.setVideoPath(videoPath);
videoView.start();

Those 3 lines already causes app to load new video and play it.

Problem starts after video is completed. From this point loading new video causes many problems (Like sometimes for half a movie only sound is played while screen is black blank). There are similar problems with replaying video (which I end up with calling 3 lanes from above).

It seems like android after completing movie releases resources or something like this (and that's why I am settings same path, when I want to replay video).

Ideally I would want video to simply pause and seekTo to beggining of movie after finished playing (but I cannot do this in OnCompletedListener, since it already changed state to stopped...).

Can I somehow achieve this? (By this I mean -> after completed video pauses and seekTo to beginning)

I already tried all combinations of pausing vidoes, suspending them, setting OnPreparedListener, setting OnCompletedListener.

Thx!

like image 307
Ajk Avatar asked Dec 20 '12 15:12

Ajk


2 Answers

Try something like

mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer arg0) {
         mVideoView.start();

    }
});


mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    public void onCompletion(MediaPlayer mp) {
            mp.reset();
            mVideoView.setVideoPath(file.getAbsolutePath());
            mVideoView.start();
    }
});
like image 92
Wamasa Avatar answered Nov 12 '22 22:11

Wamasa


stop the playback, change video path, start video

videoView.stopPlayback();
videoView.setVideoPath(newVideoPath);
videoView.start();
like image 34
gderaco Avatar answered Nov 12 '22 21:11

gderaco