我有兩個活動:流媒體在線音頻 - 播放第一個音頻僅
- MediaPlayer_Activity(顯示像播放,暫停,停止,下一控制....)
- MediaPlayerPlayList_Acitivty(顯示所有可用的歌曲)
我通過加載的所有歌曲URL流一上線音頻文件中的ArrayList
稱爲SongsList
和播放一個由一個或用戶可以從MediaPlayerPlayList_Acitivty
(不同的活動,顯示所有的選擇歌曲)並播放任何歌曲... MediaPlayerPlayList_Acitivty
傳遞正確的songIndex,並通過調試代碼進行確認。
問題:它只播放第一首是songIndex(0)的歌曲,而不管我從MediaPlayerPlayList_Acitivty
中選擇哪首歌曲,它會播放第一首歌曲。
一旦我從MediaPlayerPlayList_Acitivty
中選擇了一首新歌曲,我就會得到我在下面粘貼的錯誤信息,同時粘貼了我用於此媒體播放器的核心代碼。
我花了很多時間調試代碼,但沒有效果。
的logcat:
03-18 07:15:52.338: E/MediaPlayer(998): error (1, -1004)
03-18 07:15:52.338: W/System.err(998): java.io.IOException: Prepare failed.: status=0x1
03-18 07:15:52.338: W/System.err(998): at android.media.MediaPlayer.prepare(Native Method)
03-18 07:15:52.338: W/System.err(998): at net.website.player.MediaPlayer_Activity.playSong(MediaPlayer_Activity.java:341)
03-18 07:15:52.338: W/System.err(998): at net.website.player.MediaPlayer_Activity.onActivityResult(MediaPlayer_Activity.java:325)
03-18 07:15:52.338: W/System.err(998): at android.app.Activity.dispatchActivityResult(Activity.java:3908)
03-18 07:15:52.338: W/System.err(998): at android.app.ActivityThread.deliverResults(ActivityThread.java:2528)
03-18 07:15:52.338: W/System.err(998): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2574)
03-18 07:15:52.338: W/System.err(998): at android.app.ActivityThread.access$2000(ActivityThread.java:117)
03-18 07:15:52.338: W/System.err(998): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961)
03-18 07:15:52.348: W/System.err(998): at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 07:15:52.348: W/System.err(998): at android.os.Looper.loop(Looper.java:123)
03-18 07:15:52.348: W/System.err(998): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-18 07:15:52.348: W/System.err(998): at java.lang.reflect.Method.invokeNative(Native Method)
03-18 07:15:52.348: W/System.err(998): at java.lang.reflect.Method.invoke(Method.java:507)
03-18 07:15:52.348: W/System.err(998): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-18 07:15:52.348: W/System.err(998): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-18 07:15:52.348: W/System.err(998): at dalvik.system.NativeStart.main(Native Method)
03-18 07:15:52.348: E/MediaPlayer(998): Attempt to call getDuration without a valid mediaplayer
03-18 07:15:52.348: E/MediaPlayer(998): error (-38, 0)
03-18 07:15:52.458: E/MediaPlayer(998): Error (-38,0)
03-18 07:15:52.458: D/onCompletion(998): 2
03-18 07:15:52.458: D/playSong(998): 5
03-18 07:15:53.058: E/MediaPlayer(998): error (1, -1004)
03-18 07:15:53.058: W/System.err(998): java.io.IOException: Prepare failed.: status=0x1
03-18 07:15:53.058: W/System.err(998): at android.media.MediaPlayer.prepare(Native Method)
03-18 07:15:53.058: W/System.err(998): at net.website.player.MediaPlayer_Activity.playSong(MediaPlayer_Activity.java:341)
03-18 07:15:53.058: W/System.err(998): at net.website.player.MediaPlayer_Activity.onCompletion(MediaPlayer_Activity.java:449)
03-18 07:15:53.058: W/System.err(998): at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:1344)
03-18 07:15:53.058: W/System.err(998): at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 07:15:53.058: W/System.err(998): at android.os.Looper.loop(Looper.java:123)
03-18 07:15:53.058: W/System.err(998): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-18 07:15:53.058: W/System.err(998): at java.lang.reflect.Method.invokeNative(Native Method)
03-18 07:15:53.058: W/System.err(998): at java.lang.reflect.Method.invoke(Method.java:507)
03-18 07:15:53.058: W/System.err(998): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-18 07:15:53.058: W/System.err(998): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-18 07:15:53.068: W/System.err(998): at dalvik.system.NativeStart.main(Native Method)
03-18 07:15:53.218: E/MediaPlayer(998): Attempt to call getDuration without a valid mediaplayer
03-18 07:15:53.218: E/MediaPlayer(998): error (-38, 0)
03-18 07:15:53.218: E/MediaPlayer(998): Error (-38,0)
03-18 07:15:53.218: D/onCompletion(998): 3
03-18 07:15:53.228: D/playSong(998): 5
03-18 07:15:53.828: E/MediaPlayer(998): error (1, -1004)
03-18 07:15:53.828: W/System.err(998): java.io.IOException: Prepare failed.: status=0x1
03-18 07:15:53.828: W/System.err(998): at android.media.MediaPlayer.prepare(Native Method)
03-18 07:15:53.828: W/System.err(998): at net.website.player.MediaPlayer_Activity.playSong(MediaPlayer_Activity.java:341)
03-18 07:15:53.828: W/System.err(998): at net.website.player.MediaPlayer_Activity.onCompletion(MediaPlayer_Activity.java:449)
03-18 07:15:53.828: W/System.err(998): at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:1344)
03-18 07:15:53.828: W/System.err(998): at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 07:15:53.828: W/System.err(998): at android.os.Looper.loop(Looper.java:123)
03-18 07:15:53.828: W/System.err(998): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-18 07:15:53.828: W/System.err(998): at java.lang.reflect.Method.invokeNative(Native Method)
03-18 07:15:53.828: W/System.err(998): at java.lang.reflect.Method.invoke(Method.java:507)
03-18 07:15:53.828: W/System.err(998): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-18 07:15:53.828: W/System.err(998): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-18 07:15:53.828: W/System.err(998): at dalvik.system.NativeStart.main(Native Method)
03-18 07:15:53.908: E/MediaPlayer(998): Attempt to call getDuration without a valid mediaplayer
03-18 07:15:53.908: E/MediaPlayer(998): error (-38, 0)
03-18 07:15:53.918: E/MediaPlayer(998): Error (-38,0)
代碼:
public void onCreate(Bundle savedInstanceState) {
// Mediaplayer
mp = new MediaPlayer();
mp.setOnCompletionListener(this);
mp.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
Log.d("onPrepared", "mp.start()");
mp.start();
}
});
}
public void onCompletion(MediaPlayer arg0) {
Log.d("onCompletion", ""+currentSongIndex);
// check for repeat is ON or OFF
if(isRepeat){
// repeat is on play same song again
playSong(currentSongIndex);
} else if(isShuffle){
// shuffle is on - play a random song
Random rand = new Random();
currentSongIndex = rand.nextInt((songsList.size() - 1) - 0 + 1) + 0;
playSong(currentSongIndex);
} else{
// no repeat or shuffle ON - play next song
if(currentSongIndex < (songsList.size() - 1)){
playSong(currentSongIndex + 1);
currentSongIndex = currentSongIndex + 1;
}else{
// play first song
playSong(0);
currentSongIndex = 0;
}
}
}
public void playSong(int songIndex){
// Play song
//mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
Log.d("playSong", ""+ songsList.size());
mp.reset();
mp.setDataSource(songsList.get(songIndex).get("audio_url"));
mp.prepare();
//mp.start(); //
// Displaying Song title
String songTitle = songsList.get(songIndex).get("title_a");
songTitleLabel.setText(songTitle);
// Changing Button Image to pause image
btnPlay.setImageResource(R.drawable.btn_pause);
// set Progress bar values
songProgressBar.setProgress(0);
songProgressBar.setMax(100);
// Updating progress bar
updateProgressBar();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
你是對的,它是返回正確的歌曲索引,但音頻文件不存在於服務器上......傻我:) – 2012-03-19 17:37:07