-2
我打通過URL音樂運作良好,但在時間上得到getDuration只返回0。Android的媒體播放器getDuration只返回0
我的代碼:
String AudioURL = link;
mediaplayer = new MediaPlayer();
mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaplayer.setDataSource(AudioURL);
mediaplayer.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mediaplayer.start();
mediaplayer.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
Log.e("error", "total duration: " + mp.getDuration());
musica.progre.setProgress(0);
Log.e("MusicPP",""+mp.getDuration());
musica.progre.setMax(mp.getDuration());
Toast toast= Toast.makeText(musica.context,
"duration: "+mp.getDuration(), Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
int duration = buscaMusica.mediaplayer.getDuration();
musica.totalTempo.setText(musica.getTimeString(duration));
}
});
mediaplayer.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
musica.progre.setSecondaryProgress(mediaplayer.getCurrentPosition());
}
});