Android - 媒体控制器不显示

发布于 2025-01-01 06:10:41 字数 2288 浏览 1 评论 0 原文

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

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

我正在使用 Google 的经典视频播放器代码,可以在这里找到: 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();
}

任何想法将不胜感激?

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

谢谢。

I have a videoView where I show a video and I want to show the default media controllers. For some reason the controllers don't seem to want to show up.

I have tried creating the MediaController with xml, setting it to be always visible, attaching it to the media player with mMediaController.setMediaPlayer(mVideoView) but nothing seems to work.

I am using the classic video player code from Google which can be found here: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/media/MediaPlayerDemo_Video.html

What can be happening ? Does the listener lose the event ? Is it not attached to the actual video playing ? Should I add something else to the code that I am using (see below) ?

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

Any ideas would be greatly appreciated ?

There is no theme applied at the activity and the video plays normally without giving any error. It's just that the media controlos don't show up !

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

两仪 2025-01-08 06:10:41

在尝试了所有我能做的之后,以下代码对我有用!

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

After trying all that I could, the following code worked for me!

        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();
            }
        });
乖乖兔^ω^ 2025-01-08 06:10:41

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

Where do you add your controller to a layout ? Also be sure the container is large enough. Give it a background and look if it displays well on screen before adding the video controller.

亚希 2025-01-08 06:10:41

我不确定这个答案有点晚了,但也许其他人也有这个问题。 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();

这对我有用。

I'm not sure this answer is a little late but maybe someone else has that problem. The doku says the fellowing:

The way to use this class is to instantiate it programatically. The MediaController will create a default set of controls and put them in a window floating above your application. Specifically, the controls will float above the view specified with setAnchorView(). The window will disappear if left idle for three seconds and reappear when the user touches the anchor view.

So you can do it like this (the MediaPlayer is a VideoView in this case:

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

This worked for me.

耶耶耶 2025-01-08 06:10:41
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();
        }       
    }
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();
        }       
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文