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?

  • How to detect the swipe left or Right in Android?
  • How set alpha/opacity value to color on xml drawable?
  • Can an Android app install another android app?
  • Trying to get Package Version Information to display in an “About” message
  • How to run my Go code in Android?
  • Android Paint.setTypeface isn't working for italic
  • Related posts:

    Missing api_key/current key with Google Services 3.0.0
    Android - Get the visible area of a view?
    how to call a Method of a class kept under other package
    Activity, AppCompatActivity, FragmentActivity, and ActionBarActivity: When to Use Which?
    Android Treeview
    invalidate() inside of a thread android app
  • Move video file captured by camera using cordova (phonegap) is not refreshing gallery thumbnail
  • Dotted back edges of object
  • How to include 10“ and 7” layouts properly
  • Android: How to Programmatically set the size of a Layout
  • Taking picture from camera without preview
  • Garbage collection not triggered in app when largeHeap is enabled resulting in OOM
  • 7 Solutions collect form web for “How to detect when VideoView starts playing (Android)?”

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

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

    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

    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

    isPlaying() can be called to test whether the MediaPlayer object is in the Started

    Android MediaPlayer.isPlaying()

    Another way to detect if videoview started is using videoView.getCurrentPosition(). getCurrentPosition() returns 0 if streaming not started.

        protected Runnable playingCheck = new Runnable() {
            public void run() {
                while (true) {
                    if (vw.getCurrentPosition() != 0) {
                        // do what you want when playing started
                        break;
                    } else {
                        try {
                            Thread.sleep(250);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        };
    

    Then call:

    new Thread(playingCheck).start();
    

    Please try this :

    final Handler h = new Handler();
                    h.postDelayed( new Runnable() {
                        public void run() {
                                if (videoView.getCurrentPosition() != 0) {
                                    ((ProgressBar) rootView.findViewById(R.id.pgStreaming)).setVisibility(View.GONE);
                                } else {
                                    h.postDelayed(this, 250);
                                }
                        }
                    }, 250);
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.