事件VideoView回放状态或的MediaController播放/暂停 [英] Event for VideoView playback state or MediaController play/pause

查看:316
本文介绍了事件VideoView回放状态或的MediaController播放/暂停的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法找到一个侦听回放状态的事件。我最感兴趣的是播放/暂停状态。我使用的MediaController 具有播放/暂停按钮,但我有一个辅助按钮也可以控制播放/暂停。使用我的自定义按钮,我能播放/暂停,但如果我播放/暂停使用的MediaController 播放/暂停按钮,我现在有没有办法改变的形象在我的自定义播放/暂停按钮来播放或暂停。

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?

这是一个非常类似的问题:<一href="http://stackoverflow.com/questions/7868191/how-to-catch-event-when-click-pause-play-button-on-mediacontroller">How捉事件时点击暂停/上的MediaController 播放按钮

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

推荐答案

如果您使用的是的MediaController 与组合 VideoView ,它应该是比较容易扩展,后者并添加自己的听众吧。

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.

自定义VideoView然后将看起来像这样在其最基本的形式:

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

}

使用它等同于使用常规 VideoView ,唯一的区别是,我们现在可以连接我们自己的监听器吧。

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...

最后,你也可以在你的XML布局声明它和它充气(如上图所示) - 只要确保你使用&LT;程序包&GT; .CustomVideoView 。例如:

<mh.so.CustomVideoView android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:id="@+id/custom_videoview" />

这篇关于事件VideoView回放状态或的MediaController播放/暂停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆