我需要從服務器URL下載mp3文件,然後播放該mp3文件。
基本上主要動機是爲了播放遠程MP3文件,現在有我兩個選擇,要麼我流該文件並播放到媒體播放器,
Android:從網址下載mp3
還是我先下載它,然後播放。我仍然在嘗試第一個,但只有一個成功,所以只考慮第二個選擇。
所以,如果可以提供任何幫助,這將是偉大的我.......
感謝
我需要從服務器URL下載mp3文件,然後播放該mp3文件。
基本上主要動機是爲了播放遠程MP3文件,現在有我兩個選擇,要麼我流該文件並播放到媒體播放器,
Android:從網址下載mp3
還是我先下載它,然後播放。我仍然在嘗試第一個,但只有一個成功,所以只考慮第二個選擇。
所以,如果可以提供任何幫助,這將是偉大的我.......
感謝
MediaPlayer mp=new MediaPlayer();
mp.setDataSource(url_to_mp3);
mp.prepareAsync();
mp.start();
最好使用prepareAsync()
而不是prepare()
。
這是很容易發揮流
MediaPlayer mp=new MediaPlayer();
mp.setDataSource(url_to_mp3);
mp.prepare();
mp.start();
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();
}
}
}
感謝您的迴應,但我得到這個錯誤:11-17 20:06 :36.536:錯誤/ MediaPlayer(310):啓動狀態4調用 11-17 20:06:36.566:錯誤/ MediaPlayer(310):錯誤(-38,0) 11-17 20:06:37.086:錯誤/ MediaPlayer(310):錯誤(-38,0) – viv 2010-11-17 14:37:29
你能提供你想要播放mp3的網址嗎? – Piyush 2010-11-18 04:39:57
ooooooo感謝user472882,我得到它的工作,網址被打破了,我使用的URL形式的教程,但後來我檢查它,它被打破....謝謝............ .. – viv 2010-11-18 05:37:57