如何同时使用android中的SeekBar作为seekBar和progressBar?

发布于 2024-12-09 10:59:30 字数 82 浏览 3 评论 0原文

我打算为我的应用程序编写一个自定义媒体控制器。我计划使用搜索栏来搜索和显示进度。正在努力实现这一点。

有人有这样的实施还是我走错了路?

I intend to write a custom media controller for my app. I was planning to use seekbar to do both seeking and showing progress. Trying to implement this.

Does anyone have such implementation or am I on the wrong path ?

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

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

发布评论

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

评论(2

等风来 2024-12-16 10:59:30

终于我想通了!!

我想出的解决方案是这样的:

首先将搜索栏的最大进度设置为视频的持续时间

videoView.setOnPreparedListener(new OnPreparedListener() {

        @Override
        public void onPrepared(MediaPlayer mp) {

            seekBar.setMax(videoView.getDuration());
            seekBar.postDelayed(onEverySecond, 1000);
        }
    });

此可运行程序将继续更新进度栏:

private Runnable onEverySecond=new Runnable() {

    @Override
    public void run() {

        if(seekBar != null) {
            seekBar.setProgress(videoView.getCurrentPosition());
        }

        if(videoView.isPlaying()) {
            seekBar.postDelayed(onEverySecond, 1000);
        }

    }
};

然后可以使用搜索栏的 setOnSeekBarChangeListener 来获取用户在媒体上寻求。通过使用 fromUser 布尔值,我们可以判断这是由于用户交互还是由于

seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {

            if(fromUser) {
                // this is when actually seekbar has been seeked to a new position
                videoView.seekTo(progress);
            }
        }
    });

Finally I figured it out!!

The solution that I came up with was something like this :

First of all set the max progress for seekbar as the duration of the video

videoView.setOnPreparedListener(new OnPreparedListener() {

        @Override
        public void onPrepared(MediaPlayer mp) {

            seekBar.setMax(videoView.getDuration());
            seekBar.postDelayed(onEverySecond, 1000);
        }
    });

This runnable will keep on updating the progressbar :

private Runnable onEverySecond=new Runnable() {

    @Override
    public void run() {

        if(seekBar != null) {
            seekBar.setProgress(videoView.getCurrentPosition());
        }

        if(videoView.isPlaying()) {
            seekBar.postDelayed(onEverySecond, 1000);
        }

    }
};

And then the setOnSeekBarChangeListener for the seekbar can be used to take the user seeks on the media. By using the fromUser boolean we can make out whether it was a call back due to user's interaction or the

seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {

            if(fromUser) {
                // this is when actually seekbar has been seeked to a new position
                videoView.seekTo(progress);
            }
        }
    });
长途伴 2024-12-16 10:59:30

您设置辅助进度

不,你走在正确的道路上。它非常适合显示下载进度和流(媒体)的当前位置等内容。

You set the seconday progress.

No, you're on the right track. It is very good for displaying stuff like download progress and current location for streams (media).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文