Android SeekBar setProgress 导致我的 MediaPlayer 跳过 [英] Android SeekBar setProgress is causing my MediaPlayer to skip

查看:31
本文介绍了Android SeekBar setProgress 导致我的 MediaPlayer 跳过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 SeekBar 来显示 MediaPlayer 类播放的曲目的长度,并在曲目内启用搜索.

I'm trying to use a SeekBar to display both the length of a track played by a MediaPlayer class and to enable seeking within the track.

在轨道内寻找效果很好.但是,在曲目播放时使用 setProgress 更新进度值似乎会导致轻微的跳跃.

Seeking within the track works well. However, updating the progress value using setProgress while the track is playing seems to cause a slight skip.

在 onCreate 方法中,我创建了一个带有循环的线程,用于更新当前轨道的 SeekBar 进度值.当曲目更改时,此循环会重置.

In the onCreate method I create a Thread with a loop which updates the SeekBar's progress value for the current track. This loop resets when the track is changed.

private void createProgressThread() {

    _progressUpdater = new Runnable() {
        @Override
        public void run() {
            //Exitting is set on destroy
            while(!_exitting) {
                _resetProgress = false;
                if(_player.isPlaying()) {
                    try
                    {
                        int current = 0;
                        int total = _player.getDuration();
                        progressBar.setMax(total);
                        progressBar.setIndeterminate(false);

                        while(_player!=null && current<total && !_resetProgress){
                            try {
                                Thread.sleep(1000); //Update once per second
                                current = _player.getCurrentPosition();
                                 //Removing this line, the track plays normally.
                                progressBar.setProgress(current); 
                            } catch (InterruptedException e) {

                            } catch (Exception e){

                            }            
                        }
                    }
                    catch(Exception e)
                    {
                        //Don't want this thread to intefere with the rest of the app.
                    }
                }
            }
        }
    };
    Thread thread = new Thread(_progressUpdater);
    thread.start();
}

理想情况下,我宁愿不使用线程,因为我知道这有缺点.另外请原谅吞咽异常 - 很难继续检查所有 MediaPlayer 状态以响应 UI 事件.然而,我真正的问题是音乐正在跳跃.

Ideally I would rather not use a thread as I understand that this has disadvantages. Also please excuse the exception swallowing - it is difficult to keep checking for all MediaPlayer states in response to UI events. However, my real problem is that the music is skipping.

有人可以建议一种替代方法来更新进度并解释为什么即使使用单独的线程,对 setProgress 的调用也会导致轨道跳过吗?

Could anyone suggest an alternative way to update the progress and explain why the call to setProgress is causing the track to skip even with the use of a seperate thread?

提前致谢.

推荐答案

我认为问题在于,当您调用 setProgress() 时,会触发 onProgressChanged 事件.

I think the problem is that when you call the setProgress(), the onProgressChanged event is fired.

监听器(OnSeekBarChangeListener)有一个方法public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser).在这里,您应该测试侦听器是由用户操作还是从代码触发.在您的情况下, fromUser 变量应为 false.

The listener (OnSeekBarChangeListener) have a method public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser). Here you should test if the listener was fired by a user action or from code. In your case, the fromUser variable should be false.

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    if(fromUser){
           player.seekTo(x);
        }
        else{
         // the event was fired from code and you shouldn't call player.seekTo()
        }
}

这篇关于Android SeekBar setProgress 导致我的 MediaPlayer 跳过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆