Ajk
Ajk

Reputation: 520

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!

Upvotes: 7

Views: 17595

Answers (5)

Carbon Anik
Carbon Anik

Reputation: 11

Do this.

videoView.setOnPreparedListener { mp ->
 mp.isLooping = true
)

Upvotes: 0

Martin Sch
Martin Sch

Reputation: 64

You can do something like this:

videoView.setOnCompletionListener(MediaPlayer.OnCompletionListener { mp ->
            mp.seekTo(0);//go to second 0
            mp.start()// start again
        })

I discover in this link: https://developer.android.com/reference/android/media/MediaPlayer

Upvotes: 0

gderaco
gderaco

Reputation: 2362

stop the playback, change video path, start video

videoView.stopPlayback();
videoView.setVideoPath(newVideoPath);
videoView.start();

Upvotes: 2

Ajk
Ajk

Reputation: 520

I refactored my code in a following way: Everytime I need new video I reload whole activity. It works most of times, but now instead of video blackness at the beginning of playing I sometimes get "Cannot open media file" error.

Has it something to do with android resource managment? I release mediaPlayer in onCompletionListener.

Has anyone had such problem with playing many videos from external storage?

Upvotes: 0

Joao Guilherme
Joao Guilherme

Reputation: 387

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

Upvotes: 10

Related Questions