其他分享
首页 > 其他分享> > Android音乐搜索栏无效(空闲)

Android音乐搜索栏无效(空闲)

作者:互联网

我正在从url播放mp3文件,但SeekBar在播放歌曲时没有更新.

当我试图强行移动然后从开始播放歌曲.

我正在使用以下代码来播放和更新SeekBar.

我想创建一个跟踪媒体播放器进度的搜索栏,但它不能很好地工作,音乐正在播放,但搜索栏保持空闲状态.有什么东西我遗漏了吗?

我想我不是从正确的位置调用updateSeekBar()方法…所以尝试更改updateSeekBar()的位置但没有工作…

我错过了什么……?请帮助….我是Android新手…

Activity.java

Play = findViewById(R.id.Play_button);
Play.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            play (url);//Song Url
        }
});

private void play(String url) {

    if (mediaPlayer != null && mediaPlayer.isPlaying()) {
        mediaPlayer.stop();
    }

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try {

        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            public void onCompletion(MediaPlayer mp) {
                try {
                    if (temp == playurl.length) {
                        temp = 0;
                        play(playurl[temp]);
                        t1.setText(mobileArray[temp]);
                    } else {
                        temp = temp + 1;
                        play(playurl[temp]);
                        t1.setText(mobileArray[temp]);

                    }
                }catch(ArrayIndexOutOfBoundsException exception) {
                    temp = 0;
                    play(playurl[temp]);
                    t1.setText(mobileArray[temp]);
                }
            }
        });
        mediaPlayer.setDataSource(url);
    } catch (IllegalArgumentException e) {
        Toast.makeText(getApplicationContext(), "Please Check Your Internet Connection!", Toast.LENGTH_LONG).show();
    } catch (SecurityException e) {
        Toast.makeText(getApplicationContext(), "Please Check Your Internet Connection!", Toast.LENGTH_LONG).show();
    } catch (IllegalStateException e) {
        Toast.makeText(getApplicationContext(), "Please Check Your Internet Connection!", Toast.LENGTH_LONG).show();
    } catch (IOException e) {
        e.printStackTrace();
    }catch (Exception ex){
        String err = (ex.getMessage()==null)?"SD Card failed":ex.getMessage();
        Log.e("sdcard-err2:",err);
    }
    try {
        mediaPlayer.prepare();
    } catch (IllegalStateException e) {
        Toast.makeText(getApplicationContext(), "Please Check Your Internet Connection!", Toast.LENGTH_LONG).show();
    } catch (IOException e) {
        Toast.makeText(getApplicationContext(), "Please Check Your Internet Connection!", Toast.LENGTH_LONG).show();
    }catch (Exception ex){
        String err = (ex.getMessage()==null)?"SD Card failed":ex.getMessage();
        Log.e("sdcard-err2:",err);
    }
    try {
        mediaPlayer.start();
    }catch (Exception ex){
        String err = (ex.getMessage()==null)?"SD Card failed":ex.getMessage();
        Log.e("sdcard-err2:",err);
    }
    finalTime = mediaPlayer.getDuration();
    updateSeekBar();
    startTime = mediaPlayer.getCurrentPosition();
}

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

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

解决方法:

试试这个

mediaFileLength = mediaPlayer.getDuration();
realtimeLength = mediaFileLength;
mediaPlayer.start();
finalTime = mediaPlayer.getDuration();
startTime = mediaPlayer.getCurrentPosition();
updateSeekBar();

标签:android,android-seekbar,android-mediaplayer,android-music-player
来源: https://codeday.me/bug/20190917/1809591.html