Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
388 views
in Technique[技术] by (71.8m points)

android - Event for VideoView playback state or MediaController play/pause

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...