【问题标题】:How to detect action click on MediaController in Alndroid?如何检测Android中MediaController上的动作点击?
【发布时间】:2014-07-25 18:40:25
【问题描述】:

我有一个带有个性化 VideoView 的 Android 应用程序,它允许两个人一起流式传输和同步。所以每个人的暂停/播放动作都会让对方的玩家暂停/播放两个。

所以我需要区分 start()/pause() 函数是来自用户触摸还是来自同步。 所以我正在考虑在用户触摸 MediaController 上的播放/暂停按钮时设置一个布尔值,将设置一个布尔值,但我不知道如何检测 MediaContoller 上的播放按钮是否被触发。

这是我的 VideoView 类代码:

public class MyVideoView extends VideoView {

private VideoActionListener listener;


public MyVideoView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

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

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

public void setOnVideoActionListener(VideoActionListener l){
    listener = l;
}

@Override
public void start(){
    if(listener != null){
        listener.onPlay(this.getCurrentPosition());
        super.start();
    }else{
        super.start();
    }

}

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

@Override
public void seekTo(int msec){       
    if(listener!= null){
        listener.onSeek(msec);
        super.seekTo(msec);
    }else{
        super.seekTo(msec);
    }
}


interface VideoActionListener{
    void onPlay(int msec);
    void onPause(int msec);
    void onSeek(int msec);
}


}

在这里,在我的主要活动中,我使用 VideoView 和 MediaController:

MyVideoView video;
MyController controller = new MyController(mContext);
video = (MyVideoView)rootView.findViewById(R.id.video);
video.setBackgroundColor(Color.TRANSPARENT);
video.setMediaController(controller);

private class MyController extends MediaController {

    public MyController(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public boolean onTouchEvent(MotionEvent event) {
        Toast.makeText(context, "Controller touch event activated",
                Toast.LENGTH_SHORT).show();
        return true;
        // return super.onTouchEvent(event);

    }

    public boolean onTrackballEvent(MotionEvent event) {
        Toast.makeText(context, "Trackball event activated",
                Toast.LENGTH_SHORT).show();
        return super.onTouchEvent(event);
    }

}  

那么,我应该如何检测 MediaController 中的播放按钮何时被按下?

【问题讨论】:

    标签: android touch-event mediacontroller


    【解决方案1】:

    我想你几乎成功了。 只需调用 setOnVideoActionListener。

    MyController controller = new MyController(mContext);
    video = (MyVideoView)rootView.findViewById(R.id.video);
    video.setBackgroundColor(Color.TRANSPARENT);
    video.setMediaController(controller);
    video.setOnVideoActionListener (new VideoActionListener() {
    ...
    }
    );
    

    或者你可以去here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多