0

我有一个使用 VideoView 播放视频的 Android 应用程序,并且我也在使用媒体控制器。

我的问题是我无法防止媒体控制器在视频播放时消失,并且我还想在视频开始之前使媒体控制器可见,以便用户可以使用媒体控制器最初启动视频。我曾尝试使用 MediaController.show(0),但这并没有解决我的问题。这是我的代码:

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    mVideoView = (VideoView) findViewById(R.id.surface_view);

    mPlay = (ImageButton) findViewById(R.id.play);
    mPause = (ImageButton) findViewById(R.id.pause);
    mReset = (ImageButton) findViewById(R.id.reset);
    mStop = (ImageButton) findViewById(R.id.stop);
    MediaController mc = new MediaController(this);

    mVideoView.setMediaController(mc);

    mPlay.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            playVideo();
        }
    });

    mPause.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
            if (mVideoView != null) {
                mVideoView.pause();
            }
        }
    });

    mReset.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
            if (mVideoView != null) {
                mVideoView.seekTo(0);
            }
        }
    });

    mStop.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
            if (mVideoView != null) {
                current = null;
                mVideoView.stopPlayback();
            }
        }
    });
}

private void playVideo() {
    try {
        final String path = "http://toop.voxelperfect.net/video6.mp4";

        System.out.println("path "+path);
        Log.v(TAG, "path: " + path);
        if (path == null || path.length() == 0) {
            Toast.makeText(SampleActivity.this, "File URL/path is empty",
            Toast.LENGTH_LONG).show();
        } else {
            System.out.println("else ");
            // If the path has not changed, just start the media player
            if (path.equals(current) && mVideoView != null) {
                System.out.println("mVideoView.start() ");

                mVideoView.start();
                mVideoView.requestFocus();
                return;
            }
            current = path;
            current = path;
            MediaController mc = new MediaController(this);
            mVideoView.setMediaController(mc);
            mVideoView.setVideoURI(Uri.parse(path)); 
            mVideoView.start();
            mVideoView.requestFocus();
            mc.show(0);
        }
    } catch (Exception e) {
        Log.e(TAG, "error: " + e.getMessage());
        if (mVideoView != null) {
            mVideoView.stopPlayback();
        }
    }
}
4

1 回答 1

0

我通过创建一个实现 MediaController 并覆盖 hide 方法的嵌套类解决了我的问题。

于 2012-08-01T22:04:56.253 回答