如何使 MediaController 在整个视频播放过程中可见

发布于 2024-11-05 18:40:13 字数 122 浏览 6 评论 0原文

我在我的应用程序中使用 MediaController。但不幸的是 MediaController 在 3 秒后消失了。但我希望我的 MediaController 在我的视频完全播放之前可见。

如何实现这一目标。

I am using MediaController in my app. But unfortunately the MediaController disappears after a time period of 3 secs. But I want my MediaController to be visible until my video plays fully.

How to achieve this.

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

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

发布评论

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

评论(4

ゃ懵逼小萝莉 2024-11-12 18:40:13

默认情况下,MediaController 在 3 秒后隐藏自身。为了使其在整个视频播放过程中可见,我们必须重写 MediaController 的 hide() 。我已经给出了下面的代码片段。

final MediaController mc = new MediaController(this);
video.setMediaController(new MediaController(this) {
    @Override
    public void hide()
    {
       mc.show();
    }

    }); 

video.setMediaController(mc);

By default the MediaController hides itself after 3 secs of time. In order to make it visible throughout our video playback we will have to override the hide() of MediaController. I have given the code snippet below.

final MediaController mc = new MediaController(this);
video.setMediaController(new MediaController(this) {
    @Override
    public void hide()
    {
       mc.show();
    }

    }); 

video.setMediaController(mc);
烟酒忠诚 2024-11-12 18:40:13

为了停止隐藏 MediaController,我们可以通过扩展基类来创建一个新的 Mediacontroller。然后我们可以通过简单地覆盖它来禁用 hide 方法。为了获得实际的隐藏功能,我们可以获取基类中的 hide() 方法。我们可以使用它在播放完成后隐藏媒体控制器。
以下是 MediaController 的代码:

public class MediaController_2 extends MediaController{
public MediaController_2(Context context) {
    super(context);
}
public void hide() {
}
public void hidecontroller()    {
    super.hide();
}
}

现在,即使播放完成后,媒体控制器也不会隐藏。为了在完成播放后隐藏控制器,我们可以使用 OnCompletionListener。

        MediaController_2 mediaController = new MediaController_2(getActivity());
        mediaPlayer.prepare();   
        mediaPlayer.start();
        mediaController.show(0);
        mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
                mediaController.hidecontroller();
            }
        });

For stop hiding the MediaController we can make a new Mediacontroller by extending the base class. Then we can disable the hide method by simply overriding it. For getting the actual hide functionality, we can fetch the hide() method in base class. We can hide the Mediacontroller after playback is completed using that.
Here is the code for MediaController:

public class MediaController_2 extends MediaController{
public MediaController_2(Context context) {
    super(context);
}
public void hide() {
}
public void hidecontroller()    {
    super.hide();
}
}

Now the mediacontroller won't be hiding even after the completion of the playback. For hiding the controllers after completing playback we can use OnCompletionListener.

        MediaController_2 mediaController = new MediaController_2(getActivity());
        mediaPlayer.prepare();   
        mediaPlayer.start();
        mediaController.show(0);
        mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
                mediaController.hidecontroller();
            }
        });
还如梦归 2024-11-12 18:40:13

更简洁

首先,在 show() 方法中使用 int timeout 作为参数对我没有任何作用。

其次,这段简洁的代码强制控件在第一次触摸后留在屏幕上:

// Assumes you have a VideoView in your layout called 'video_preview'.
VideoView videoPreview = (VideoView) findViewById(R.id.video_preview);

MediaController mediaController = new MediaController(this) {                                                        
    @Override
    public void hide() {}      // Prevent hiding of controls.
};

videoPreview.setMediaController(mediaController);

More Succinct

First off, using an int timeout as a parameter in the show() method did nothing for me.

Secondly, this succinct piece of code forced the controls to stay on the screen after the first touch:

// Assumes you have a VideoView in your layout called 'video_preview'.
VideoView videoPreview = (VideoView) findViewById(R.id.video_preview);

MediaController mediaController = new MediaController(this) {                                                        
    @Override
    public void hide() {}      // Prevent hiding of controls.
};

videoPreview.setMediaController(mediaController);
纸短情长 2024-11-12 18:40:13
VideoView videoPlayer;
MediaController mediaController;

videoPlayer = view.findViewById(R.id.videoPlayer);
Uri uri = Uri.parse(URL);
videoPlayer.setVideoURI(uri);
mediaController = new MediaController(getContext()) {
    @Override
    public void hide() {}      // on hide do nothing
};
videoPlayer.setMediaController(mediaController);
videoPlayer.requestFocus();
videoPlayer.start();
VideoView videoPlayer;
MediaController mediaController;

videoPlayer = view.findViewById(R.id.videoPlayer);
Uri uri = Uri.parse(URL);
videoPlayer.setVideoURI(uri);
mediaController = new MediaController(getContext()) {
    @Override
    public void hide() {}      // on hide do nothing
};
videoPlayer.setMediaController(mediaController);
videoPlayer.requestFocus();
videoPlayer.start();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文