How to detect when VideoView starts playing (Android)?

15,842

Solution 1

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

Solution 2

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

Solution 3

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

Solution 4

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

Share:
15,842

Related videos on Youtube

Lovro Pandžić
Author by

Lovro Pandžić

Updated on June 04, 2022

Comments

  • Lovro Pandžić
    Lovro Pandžić almost 2 years

    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?

  • edoardotognoni
    edoardotognoni almost 10 years
    OMG, please don't go this way! You're polling with a while(true). This is really bad.
  • Tyler Davis
    Tyler Davis over 9 years
    I agree with @edoardotognoni
  • sulu.dev
    sulu.dev over 9 years
    the key point of the answer is not the loop part, but the getCurrentPosition() method. I just wanted to call this function periodically to detect if the video is started or not. you can use different structures for periodic operations if you want. but like i said, it is not the important part of "is my video started" problem
  • sulu.dev
    sulu.dev over 9 years
    and i will be so glad if you state what should i use instead of while(true), please.
  • Ayaz Alifov
    Ayaz Alifov over 8 years
    Thank you, but unfortunately it requres API > 16
  • Daniel Vilas-Boas
    Daniel Vilas-Boas about 5 years
    the link is broken here