我试图从网址播放mp3音频。但是当网络速度慢时,播放器似乎在缓冲时没有响应。所以我需要在缓冲时添加进度对话框。可能吗。?如果有,任何人都可以帮我做。
public class Player extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
Bundle b = new Bundle();
b = getIntent().getExtras();
final String url = b.getString("audio");
Log.e("URL: ", url);
final MediaPlayer mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
Log.e("preparing: ", url);
mMediaPlayer.setDataSource(url);
mMediaPlayer.prepareAsync(); // might take long! (for buffering, etc)
} catch (IOException e) {
Toast.makeText(this, "mp3 not found", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
//mp3 will be started after completion of preparing...
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer player) {
int max = player.getDuration();
Log.e("MAX", String.valueOf(max));
player.start();
Log.e("start: ", url);
}
});
}
}
答案 0 :(得分:1)
您可以显示搜索栏,例如:
// In your onCreate(), add below at last .......
.
.
.
seekbar.setMax(mMediaPlayer.getDuration());
mMediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent)
{
double ratio = percent / 100.0;
bufferingLevel = (int)(mp.getDuration() * ratio);
seekBar.setSecondaryProgress(bufferingLevel);
}
});
希望这有帮助!