Event for VideoView playback state or MediaController play/pause
Asked Answered
M

2

32

I cant seem to find an event that listens for playback state. I am mostly interested in the play/pause state. I am using MediaController which has a Play/Pause button, but I have a secondary button that also controls Play/Pause. Using my custom button, I can play/pause, but if I play/pause using the MediaController play/pause button, I currently have no way to change the image on my custom play/pause button to either play or pause.

Is there an event that I do not know about so I can do some work during play/pause?

This is a very similar question: How to catch event when click pause/play button on MediaController

Martines answered 28/10, 2011 at 20:29 Comment(0)
M
97

If you're using the MediaController in combination with a VideoView, it should be relatively easy to extend the latter and add your own listener to it.

The custom VideoView would then look something like this in its most basic form:

public class CustomVideoView extends VideoView {

    private PlayPauseListener mListener;

    public CustomVideoView(Context context) {
        super(context);
    }

    public CustomVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomVideoView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setPlayPauseListener(PlayPauseListener listener) {
        mListener = listener;
    }

    @Override
    public void pause() {
        super.pause();
        if (mListener != null) {
            mListener.onPause();
        }
    }

    @Override
    public void start() {
        super.start();
        if (mListener != null) {
            mListener.onPlay();
        }
    }

    public static interface PlayPauseListener {
        void onPlay();
        void onPause();
    }

}

Using it is identical to using a regular VideoView, with the only difference being that we can now hook up our own listener to it.

// Some other code above...
CustomVideoView cVideoView = (CustomVideoView) findViewById(R.id.custom_videoview);
cVideoView.setPlayPauseListener(new CustomVideoView.PlayPauseListener() {

    @Override
    public void onPlay() {
        System.out.println("Play!");
    }

    @Override
    public void onPause() {
        System.out.println("Pause!");
    }
});

cVideoView.setMediaController(new MediaController(this));
cVideoView.setVideoURI(...);
// or
cVideoView.setVideoPath(...);
// Some other code below...

Finally, you may also declare it in your xml layout and inflate it (as shown above) - just make sure your use <package_name>.CustomVideoView. Example:

<mh.so.CustomVideoView android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:id="@+id/custom_videoview" />
Mousey answered 8/11, 2011 at 5:55 Comment(4)
Genius! Thank you sir, that worked perfectly. +50 to you. I can't wait until I know java as well as I know other things like action script.Martines
Glad to have been of help. :) By the way, a call to super is usually done in conjunction with overriding a method. By overriding you're basically redefining the base/super/parent class's method. If you call super in such an overridden method you will retain it's functionality, which is what you want to do if your goal is to add functionality. Sometimes you may want to replace functionality though, in which case you may get the desired outcome by not calling super.Mousey
@Mousey Can we get the buffering state, means the video view working fine but due to poor/slow internet connection it stop in middle as buffering and play again and so on. So how can we analyse that state so that I can set wait/buffer progress bar.Oilla
@Sadiq: Unfortunately VideoView doesn't offer any callbacks to get buffering updates. However, you can try getting a reference to the underlying MediaPlayer instance in the OnPreparedListener callback and setting a OnBufferingUpdateListener to that. Note that this will override the VideoViews listener, but that shouldn't be a big deal as the latter only uses it to keep track of the buffering percentage (but doesn't use it anywhere, except for returning it in getBufferPercentage()).Mousey
O
2

You should be able to set your own MediaController.MediaPlayerControl and override pause and start

Ophthalmia answered 4/11, 2011 at 16:39 Comment(1)
if I @Override the pause() start() methods, how can I do like a dispatchEvent method (dispatchEvent is an actionscript method) that I can listen to on my custom MediaController. Also, I still want all the regular behaviors of pause and start, do I use super in that case? Never really understood superMartines

© 2022 - 2024 — McMap. All rights reserved.