android SeekBar未更新音乐正在播放,但seekBar保持空闲

gojuced7  于 2022-12-16  发布在  Android
关注(0)|答案(3)|浏览(143)

我正在播放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);
}

}
fivyi3re

fivyi3re1#

您应该考虑使用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.
        }
    }       
});

另外,如果你想工作的例子,看看这个链接的代码(我刚刚使用了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

2w3kk1z5

2w3kk1z52#

您可以为此创建使用定时器

seekBar.setMax(mediaPlayer.getDuration());

    new Timer().scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            seekBar.setProgress(mediaPlayer.getCurrentPosition());
        }
    },0,1000);
o7jaxewo

o7jaxewo3#

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

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

        }

    };
    seekBar.post(updater)
}

相关问题