3

我有一个 videoView,我在其中显示视频,并且我想显示默认媒体控制器。出于某种原因,控制器似乎不想出现。

我尝试使用 xml 创建 MediaController,将其设置为始终可见,将其附加到媒体播放器,mMediaController.setMediaPlayer(mVideoView)但似乎没有任何效果。

我正在使用谷歌的经典视频播放器代码,可以在这里找到:http: //developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/media/MediaPlayerDemo_Video.html

会发生什么?侦听器会丢失事件吗?它不附加到实际的视频播放吗?我是否应该在我正在使用的代码中添加其他内容(见下文)?

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_content_video);

    [...]

    mVideoView = (VideoView) findViewById(R.id.surface);
    mainVideoHolder = (LinearLayout) findViewById(R.id.main_video_holder);


    holder = mVideoView.getHolder();
    holder.addCallback(this);

    mMediaController = new MediaController(this);

    mMediaController.show();
}



private void playVideo() {
    doCleanUp();
    try {
        mMediaPlayer = new MediaPlayer();
        Log.d(tag, "surfaceCreated");
        File f = new File(mAssetsPath);
        File[] files = f.listFiles();
        Log.d(tag, "File: " + files[0].toString());

        URI uri = URI.create("file://" + (files[0].toString()));
        File file = new File(uri);
        ParcelFileDescriptor parcel = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);
        mMediaPlayer.setDataSource(parcel.getFileDescriptor());

        mMediaPlayer.setDisplay(holder);

        mMediaPlayer.prepare();

        mMediaPlayer.setOnBufferingUpdateListener(this);
        mMediaPlayer.setOnCompletionListener(this);
        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setOnVideoSizeChangedListener(this);

        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

        mMediaController.setMediaPlayer(mVideoView);

        mVideoView.setMediaController(mMediaController);
    } catch (Exception e) {
        Log.e(tag, "error: " + e.getMessage(), e);
    }
}


public void surfaceCreated(SurfaceHolder holder) {
    Log.d(tag, "surfaceCreated called");
    playVideo();
}

任何想法将不胜感激?

活动中没有应用主题,视频正常播放,没有出现任何错误。只是媒体控件没有出现!

谢谢。

4

4 回答 4

1

在尽我所能之后,以下代码对我有用!

        mVideoView = (VideoView) findViewById(R.id.video);

        mMediaController = new MediaController(this) {
            //for not hiding
            @Override
            public void hide() {}

            //for 'back' key action
            @Override
            public boolean dispatchKeyEvent(KeyEvent event) {
                if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                    Activity a = (Activity)getContext();
                    a.finish();
                }
                return true;
            }
        };

        mMediaController.setAnchorView(mVideoView);
        mMediaController.setMediaPlayer(mVideoView);
        mVideoView.setMediaController(mMediaController);
        mMediaController.requestFocus();

        //only this showed the controller for me!!
        mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mVideoView.start();
                mMediaController.show(900000000);
            }
        });

        //finish after playing
        mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                finish();
            }
        });
于 2014-08-21T19:23:52.947 回答
0

您在哪里将控制器添加到布局中?还要确保容器足够大。在添加视频控制器之前,给它一个背景并查看它是否在屏幕上显示良好。

于 2012-02-01T17:00:59.790 回答
0
this work for me:

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        getWindow().setFormat(PixelFormat.UNKNOWN);

        mVideoView = (VideoView)findViewById(R.id.videoview);   
        mc = new MediaController(this){
            @Override
            public void hide() {} //dont hide control
        };

        new MyDelay2().execute("");
    }

    private class MyDelay2 extends AsyncTask<String,Void,String>{

        @Override
        protected String doInBackground(String... params) {                 
            Uri uri2 = Uri.parse("android.resource://com.blah/"+R.raw.cool);
            mVideoView.setVideoURI(uri2);
            return null;
        }


        @Override
        protected void onPostExecute(String result) {   
            dialog.dismiss();
            mc.setAnchorView(mVideoView);
            mc.setMediaPlayer(mVideoView);
            mc.setEnabled(true);
            mVideoView.setMediaController(mc);      

            mc.show();
        }       
    }
于 2014-07-18T14:08:41.757 回答
0

我不确定这个答案有点晚了,但也许其他人有这个问题。doku 说:

使用这个类的方法是以编程方式实例化它。MediaController 将创建一组默认控件并将它们放在浮动在应用程序上方的窗口中。具体来说,控件将浮动在使用 setAnchorView() 指定的视图上方。如果闲置三秒钟,窗口将消失,并在用户触摸锚点视图时重新出现。

所以你可以这样做(在这种情况下,MediaPlayer 是一个 VideoView:

VideoView videoView = (VideoView) findViewById(R.id.videoView);
videoView.setVideoPath(uri);
MediaController controller = new MediaController(this);
controller.setAnchorView(videoView);
controller.setMediaPlayer(videoView);
videoView.setMediaController(controller);
videoView.start();

这对我有用。

于 2012-12-11T17:54:30.510 回答