SeekBar 不更新音乐正在播放但 seekBar 保持空闲

SeekBar is not updating music is playing but the seekBar stay idle

我正在播放 url 中的 mp3 文件,但播放歌曲时 SeekBar 没有更新。

歌曲开始显示有缓冲但不会自动移动

当我试图用力移动时,它也工作正常。

下面是我用来播放和更新 SeekBar 的代码。

我想创建一个 seekBar 来跟踪媒体播放器的进度,但效果不是很好,音乐正在播放,但 seekbar 保持空闲状态。我遗漏了什么吗?

请帮助我,我是 android 的新人。

public class XYZ extends Fragment implements MediaPlayer.OnBufferingUpdateListener,MediaPlayer.OnCompletionListener{
private SeekBar seekBar;
private MediaPlayer mediaPlayer;
private int mediaFileLength;
final Handler handler = new Handler();
private int realtimeLength;
Button b,b3,b4;
private double startTime = 0;
private double finalTime = 0;
public static int oneTimeOnly = 0;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_outside, container,
            false);
    rootView.setBackgroundResource(R.drawable.jotirling);
    seekBar = rootView.findViewById(R.id.seekbar);
    seekBar.setMax(99); // 100% (0~99)
    seekBar.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (mediaPlayer.isPlaying()) {
                SeekBar seekBar = (SeekBar) v;
                int playPosition = (mediaFileLength / 100) * seekBar.getProgress();
                mediaPlayer.seekTo(playPosition);
            }
            return false;
        }
    });


    b3.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v) {
            final ProgressDialog mDialog = new ProgressDialog(getActivity());


            @SuppressLint("StaticFieldLeak") AsyncTask<String, String, String> mp3Play = new AsyncTask<String, String, String>() {

                @Override
                protected void onPreExecute() {
                    mDialog.setMessage("Please wait It will take time according to your network speed...!");
                    mDialog.show();
                    mDialog.setCancelable(false);
                }

                @Override
                protected String doInBackground(String... params) {
                    try {
                        mediaPlayer.setDataSource(params[0]);
                        mediaPlayer.prepare();
                    } catch (Exception ignored) {

                    }
                    return "";
                }

                @Override
                protected void onPostExecute(String s) {
                    mediaFileLength = mediaPlayer.getDuration();
                    realtimeLength = mediaFileLength;
                    if (!mediaPlayer.isPlaying()) {
                        p=1;
                        mediaPlayer.start();
                        Toast.makeText(getActivity(), "Playing sound", Toast.LENGTH_SHORT).show();
                        finalTime = mediaPlayer.getDuration();
                        startTime = mediaPlayer.getCurrentPosition();
                        b3.setBackgroundResource(R.drawable.pp);
                        if (oneTimeOnly == 0) {
                            oneTimeOnly = 1;
                        }
                    } else {
                        p=0;
                        mediaPlayer.pause();
                        Toast.makeText(getActivity(), "Pausing "+
                                "sound",Toast.LENGTH_SHORT).show();
                        b3.setBackgroundResource(R.drawable.p);
                    }

                    updateSeekBar();
                    mDialog.dismiss();
                }
            };

            mp3Play.execute("URL"); // direct link mp3 file

        }
    });

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setOnBufferingUpdateListener(this);
    mediaPlayer.setOnCompletionListener(this);



    return rootView;
}

private void updateSeekBar() {
    seekBar.setProgress((int) (((float) mediaPlayer.getCurrentPosition() / mediaFileLength) * 100));
    if (mediaPlayer.isPlaying()) {
        Runnable updater = new Runnable() {
            @Override
            public void run() {
                updateSeekBar();


            }

        };
    }
}

@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
    seekBar.setSecondaryProgress(percent);
}

}

您应该考虑使用 setOnSeekbarChangeListener()

参考:setOnSeekBarChangeListener()

简单的例子是

seekBar.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) {     
        // TODO Auto-generated method stub      

        if(fromUser){
          //do your things, like updating time durations etc.
        }
    }       
});  

此外,如果您想要工作示例,请查看此 link 代码(我刚刚使用 circularSeekbar 而不是常规的)https://github.com/iamSahdeep/Bop/blob/master/app/src/main/java/com/sahdeepsingh/Bop/Activities/PlayerView.java#L324

更多示例:https://www.javatips.net/api/android.widget.seekbar.onseekbarchangelistener

您正在定义 Runnable updater 但没有调用它。

if (mediaPlayer.isPlaying()) {
    Runnable updater = new Runnable() {
        @Override
        public void run() {
            updateSeekBar();


        }

    };
    seekBar.post(updater)
}