Android Music Seekbar is not working (Idle)

我只是一个虾纸丫 提交于 2020-01-18 05:45:28

问题


I am playing mp3 file from url, but SeekBar is not updating while playing song.

When i am trying to move forcefully then song playing from start.

Below code i am using to play and update SeekBar.

I wanted to create a seekBar that track the progress of a mediaplayer but it doesnt work out quite well, the music is playing but the seekbar stay idle. Is there something that I left out?

I think i am not not calling updateSeekBar() method from correct position...so Tried by change position of updateSeekBar() but not worked...

Am I missing anything...? Please help.... I am new to 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);
}

回答1:


try this

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



回答2:


Try

SeekBar.setProgress(0);
SeekBar.setMax(100);

after the mediaPlayer.start();



来源:https://stackoverflow.com/questions/54692981/android-music-seekbar-is-not-working-idle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!