向MediaController按钮添加其他方法

时间:2012-07-19 09:35:42

标签: android

我有一个播放视频的VideoView,我打电话

videoPlayer.setMediaController(new MediaController(this));

显示媒体控制器按钮(播放,暂停,前进,后退)。 问题是当我点击MediaController的播放或暂停按钮时如何添加其他方法?

这是我的班级:

public class VideoPlayer extends Activity implements OnCompletionListener {
    private String filename;

    private VideoView videoPlayer;
    private MediaPlayer mediaplayer;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.video_player);

        System.gc();
        Intent i = getIntent();
        Bundle extras = i.getExtras();
        filename = extras.getString("video_url");

        System.out.println(filename);

        videoPlayer = (VideoView) findViewById(R.id.videoPlayer);
        videoPlayer.setOnCompletionListener(this);
        videoPlayer.setKeepScreenOn(true);
        videoPlayer.setVideoPath(filename);
        videoPlayer.setMediaController(new MediaController(this));
        videoPlayer.requestFocus();
        videoPlayer.start();

        mediaplayer = new MediaPlayer();
        mediaplayer = MediaPlayer.create(
                this,
                Uri.parse("android.resource://" + getPackageName() + "/"
                        + R.raw.alo));
        mediaplayer.start();

    }

    /** This callback will be invoked when the file is finished playing */
    @Override
    public void onCompletion(MediaPlayer mp) {
        // Statements to be executed when the video finishes.
        this.finish();
    }

    /** Use screen touches to toggle the video between playing and paused. */
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            if (videoPlayer.isPlaying()) {
                videoPlayer.pause();
                mediaplayer.pause();
            } else {
                videoPlayer.start();
                mediaplayer.start();
            }
            return true;
        } else {
            return false;
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }   
}

1 个答案:

答案 0 :(得分:0)

我不确定你想要实现的目标。 您可以使用MediaController扩展一个类并实现您自己的一些方法。基本的OO原则。

private class MyMediaController extends MediaController {

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // Your logic here
        return super.onTouchEvent(event);
    }
}