问题描述
我正在从url播放mp3文件,但是SeekBar在播放歌曲时没有更新.
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.
下面我用来播放和更新SeekBar的代码.
Below code i am using to play and update SeekBar.
我想创建一个seekbar来跟踪媒体播放器的进度,但是效果不佳,正在播放音乐,但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?
我认为我不是没有从正确的位置调用 updateSeekBar()方法...所以尝试了 updateSeekBar()的更改位置,但是没有用...
I think i am not not calling updateSeekBar() method from correct position...so Tried by change position of updateSeekBar() but not worked...
我错过了任何东西吗??请帮助....我是Android的新手...
Am I missing anything...? Please help.... I am new to Android...
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音乐搜索栏不起作用(空闲)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!