Android:进度更新 SeekrBar

发布于 2024-12-01 15:39:43 字数 991 浏览 1 评论 0原文

我有一个媒体播放器的搜索栏,我的实现如下。

    //SeekBar setup     
    progress = (SeekBar)findViewById(R.id.seekBarPlayer);
    progress.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            if (fromUser) {
                mediaPlayer.seekTo(progress);
                Log.v(TAG,progress +"Seeking...");
            }

        }
    });

    //seekbar initiated after mediaplayer initiated
    total = mediaPlayer.getDuration();
    progress.setMax(total);

但我的问题是seekBar 不会随着音乐播放而运行。它闲置在一处,如果我寻找,那么音乐就会寻找,但它不会运行或随着音乐一起前进。有人能告诉我我错过了什么吗?

预先感谢您的宝贵时间。

I'm having a seeker bar for media player and I have implemented as below.

    //SeekBar setup     
    progress = (SeekBar)findViewById(R.id.seekBarPlayer);
    progress.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            if (fromUser) {
                mediaPlayer.seekTo(progress);
                Log.v(TAG,progress +"Seeking...");
            }

        }
    });

    //seekbar initiated after mediaplayer initiated
    total = mediaPlayer.getDuration();
    progress.setMax(total);

But my problem is seekBar doesn't run as the music plays. It idles at one place, if I seek then the music seeks but it doesn't run or make progress movment with the music. Can someone tell me what am I missing.

Thanks in advance for your time.

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

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

发布评论

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

评论(2

无边思念无边月 2024-12-08 15:39:43

线程对于这个来说太重了。这是我使用的精简版本。

在您的活动中声明一个处理程序,

private boolean isPaused;
Handler handler = new Handler();

一个可运行的程序

    Runnable onEverySecond = new Runnable() {   

    @Override
    public void run() {
        if(!isPaused){
            progress.setProgress(player.getCurrentPosition());              

            handler.postDelayed(onEverySecond, EVERY_SECOND);
        }
    }
};

声明

@Override
protected void onResume() {
    isPaused = false;
    handler.postDelayed(onEverySecond, EVERY_SECOND);
}

在您的暂停中

@Override
protected void onPause() {  
    isPaused = true;
    handler.removeCallbacks(onEverySecond);
}

a thread is too heavy for this. here is a cut down version of what i use.

declare a handler in your activity

private boolean isPaused;
Handler handler = new Handler();

declare a runnable

    Runnable onEverySecond = new Runnable() {   

    @Override
    public void run() {
        if(!isPaused){
            progress.setProgress(player.getCurrentPosition());              

            handler.postDelayed(onEverySecond, EVERY_SECOND);
        }
    }
};

in your on resume

@Override
protected void onResume() {
    isPaused = false;
    handler.postDelayed(onEverySecond, EVERY_SECOND);
}

in your on pause

@Override
protected void onPause() {  
    isPaused = true;
    handler.removeCallbacks(onEverySecond);
}
荭秂 2024-12-08 15:39:43

这是因为您在媒体播放器运行的同一线程中更新了进度栏。在 UI(或不同)线程中更新进度条。看看这个 android :如何使用进度条?。谢谢。

It is becoz you updated the progress bar in same thread in which media player runs. update your progress bar in UI (or different)thread.Look at this android: how do I use the progress bar?. Thnx.

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