我需要从服务器URL下载一个mp3文件,然后播放该mp3文件
基本上主要的动机是播放一个远程mp3文件,现在我有两个选项,我可以将该文件流传输到媒体播放器上,
或者我首先下载然后播放它。我仍在尝试第一个,但取得了一个成功,所以只考虑第二个选择。
所以,如果能提供任何帮助,对我来说会很棒.......
感谢
答案 0 :(得分:3)
public class Mp3player extends Activity {
private Button buttonPlayStop;
private MediaPlayer mediaPlayer;
private SeekBar seekBar;
private final Handler handler = new Handler();
static final String AUDIO_PATH =
"http://sound18.mp3slash.net/indian/abcd/[Songs.PK]%20ABCD%20-%2002%20-%20Bezubaan.mp3";
// Here i override onCreate method.
//
// setContentView() method set the layout that you will see then
// the application will starts
//
// initViews() method i create to init views components.
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
initViews();
}
// This method set the setOnClickListener and method for it (buttonClick())
private void initViews() {
buttonPlayStop = (Button) findViewById(R.id.ButtonPlayStop);
buttonPlayStop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
buttonClick();
}
});
try {
playAudio(AUDIO_PATH);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
seekBar = (SeekBar) findViewById(R.id.SeekBar01);
seekBar.setMax(mediaPlayer.getDuration());
seekBar.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
seekChange(v);
return false;
}
});
}
private void playAudio(String url) throws Exception
{
killMediaPlayer();
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(url);
mediaPlayer.prepare();
// mediaPlayer.start();
}
private void killMediaPlayer() {
if(mediaPlayer!=null) {
try {
mediaPlayer.release();
}
catch(Exception e) {
e.printStackTrace();
}
}
}
public void startPlayProgressUpdater() {
seekBar.setProgress(mediaPlayer.getCurrentPosition());
if (mediaPlayer.isPlaying()) {
Runnable notification = new Runnable() {
public void run() {
startPlayProgressUpdater();
}
};
handler.postDelayed(notification, 1000);
} else {
mediaPlayer.pause();
buttonPlayStop.setText(getString(R.string.play_str));
seekBar.setProgress(0);
}
}
// This is event handler thumb moving event
private void seekChange(View v) {
if (mediaPlayer.isPlaying()) {
SeekBar sb = (SeekBar) v;
mediaPlayer.seekTo(sb.getProgress());
}
}
// This is event handler for buttonClick event
private void buttonClick() {
if (buttonPlayStop.getText() == getString(R.string.play_str)) {
buttonPlayStop.setText(getString(R.string.pause_str));
try {
mediaPlayer.start();
startPlayProgressUpdater();
} catch (IllegalStateException e) {
mediaPlayer.pause();
}
} else {
buttonPlayStop.setText(getString(R.string.play_str));
mediaPlayer.pause();
}
}
}
答案 1 :(得分:1)
播放流非常容易
MediaPlayer mp=new MediaPlayer();
mp.setDataSource(url_to_mp3);
mp.prepare();
mp.start();
答案 2 :(得分:1)
MediaPlayer mp=new MediaPlayer();
mp.setDataSource(url_to_mp3);
mp.prepareAsync();
mp.start();
最好使用prepareAsync()
而不是prepare()
。