2012-03-18 207 views
1

我有兩個活動:流媒體在線音頻 - 播放第一個音頻僅

  1. MediaPlayer_Activity(顯示像播放,暫停,停止,下一控制....)
  2. 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(); 
      } 
     } 

回答

1

從堆棧跟蹤看來,準備調用由於java.io.IOException而失敗。數據源url是否正確? songsList列表可能沒有正確填充?

+0

你是對的,它是返回正確的歌曲索引,但音頻文件不存在於服務器上......傻我:) – 2012-03-19 17:37:07

0

看來,您的應用程序因爲 mp.getDuration()呼叫崩潰。我的猜測是,這是發生在你的 updateProgressBar()方法,當它在不合時宜的執行。

既然你在playSong方法註冊一個OnCompletionListener,你應該叫prepareAsync()代替prepare()

編輯看着堆棧跟蹤接近,看來問題不是從updateProgressBar()內發生,而是直接從呼叫prepare()playSong()。看看是否切換到prepareAsync()會改變行爲。

+0

我忘了提及我嘗試了prepareAsync並且它保持循環,這意味着在mediaplayer活動中,我的歌名稱標題發生變化,並且它是無限循環,我必須殺死仿真器才能停止它。 – 2012-03-18 13:50:54

+0

@AbuHamzah - 這很奇怪。我不知道它會在哪裏循環。從我所知道的情況來看,你只能從onCompletion中的一首歌曲前進到下一首歌曲,該歌曲只能在歌曲結束時執行。你是否確定了循環中的事件序列? – 2012-03-18 17:12:30