Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect when VideoView starts playing (Android)?

Here's the problem, I want to change play button to pause button when the video stream starts playing in videoview but I don't know how to detect that event?

like image 874
Lovro Pandžić Avatar asked Apr 03 '11 12:04

Lovro Pandžić


4 Answers

There is a great article about MediaPlayer in here - http://www.malmstein.com/blog/2014/08/09/how-to-use-a-textureview-to-display-a-video-with-custom-media-player-controls/

You can set infoListener on your VideoView

setOnInfoListener(new MediaPlayer.OnInfoListener() {
    @Override
    public boolean onInfo(MediaPlayer mp, int what, int extra) {

        if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
            // Here the video starts
            return true;
        }
        return false;
    }
like image 177
Nir Hartmann Avatar answered Nov 19 '22 21:11

Nir Hartmann


I ended up using VideoView.setOnPreparedListener. This was enough to cover my problem (play button drawable change to pause)

like image 41
Lovro Pandžić Avatar answered Nov 19 '22 20:11

Lovro Pandžić


accepted answer here is not 100% accurate.

sometimes onprepared is call 3 seconds before first frame is being rendered. i suggest having a callback on that event (MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START)

mMediaPlayer.setOnInfoListener(new MediaPlayer.OnInfoListener() {
    @Override
    public boolean onInfo(MediaPlayer mediaPlayer, int i, int i1) {
        if (i == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START){
            //first frame was bufered - do your stuff here
        }
        return false;
    }
});

see media info documantaion for more callbacks of info/warning: https://developer.android.com/reference/android/media/MediaPlayer.html#MEDIA_INFO_VIDEO_RENDERING_START

like image 10
Wops Avatar answered Nov 19 '22 20:11

Wops


As far as I know, there is no event sent when video start playing in VideoView, but I can think of two options:

  1. Create a version of VideoView by yourself, that sends event in those cases.
  2. Use MediaController (which is default in VideoView)

If you want to follow option one - you can get the source of VideoView from here

like image 1
MByD Avatar answered Nov 19 '22 21:11

MByD