Android的搜索栏setProgress导致我的MediaPlayer跳过我的、跳过、Android、setProgress

2023-09-05 00:19:19 作者:cat lovers(爱猫者)

我试图用一个搜索栏来显示由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方法创建一个线程与环路更新搜索栏的进步价值,为当前曲目。当轨道改变这种循环复位。

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

理想我宁愿不使用一个线程,因为我明白,这有缺点。也请原谅异常吞咽 - 这是很难保持响应UI事件检查所有的MediaPlayer的状态。但是,我真正的问题是,音乐跳绳。

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)有一个方法 公共无效onProgressChanged(搜索栏搜索栏,INT进步,布尔FROMUSER)。在这里,你应该测试,如果听者被解雇由用户操作或code。 在你的情况下,FROMUSER变量应该是假的。

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