2013-05-11 147 views
0

這是我的代碼來播放和更新跟蹤和按鈕點擊我呼籲update()功能,但軌道沒有正確改變。Mediaplayer跟蹤改變問題

第一首曲目播放得很好,但當我嘗試切換到下一首時,它的行爲不正確,我錯了。

MediaPlayer mPlayer; 


     @Override 
     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mPlayer=new MediaPlayer(); 
     mPlayer.setOnPreparedListener(this); 
     } 

     public void prepareMediaPlayer(FileDescriptor fd) 
     { 
      try { 
       mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 
       Log.d("player", "setAudiotStreamType"); 
       mPlayer.setDataSource(fd); 
       Log.d("player", "setDataSource"); 
       mPlayer.prepareAsync(); 
       Log.d("player", "prepareAsync"); 

      } 
      catch (IOException e) {} 
      catch (IllegalArgumentException e) {} 
      catch (IllegalStateException e) {} 
     } 

     @Override 
     public void onPrepared(MediaPlayer arg0) { 
     } 

     public void updateTrack() 
     { 
         File f=(File) imageFilexxSm.values().toArray()[index]; 
        FileInputStream inputStream = new FileInputStream(f); 
        prepareMediaPlayer(inputStream.getFD()); 
        inputStream.close(); 

        if(mPlayer.isPlaying()) 
        { 
        mPlayer.stop(); 
        mPlayer.reset(); 
        mPlayer.reset(); 
        } 

        else 
        { 
        mPlayer.start();  
        } 

     } 

登錄:

05-11 13:17:42.782: E/MediaPlayer(14969): start called in state 4 
05-11 13:17:42.782: E/MediaPlayer(14969): error (-38, 0) 
05-11 13:17:42.821: I/dalvikvm-heap(14969): Grow heap (frag case) to 34.372MB for 4194320-byte allocation 
05-11 13:17:43.040: I/System.out(14969): index value is 0/1024/0 
05-11 13:17:43.087: I/dalvikvm-heap(14969): Grow heap (frag case) to 30.457MB for 2261008-byte allocation 
05-11 13:17:43.189: E/MediaPlayer(14969): attachNewPlayer called in state 8 
05-11 13:17:43.220: I/Choreographer(14969): Skipped 35 frames! The application may be doing too much work on its main thread. 
05-11 13:17:43.275: I/dalvikvm-heap(14969): Grow heap (frag case) to 34.452MB for 4194320-byte allocation 
05-11 13:17:43.392: I/MediaPlayer(14969): Don't send intent. msg.arg1 = 0, msg.arg2 = 0 
05-11 13:17:43.392: E/MediaPlayer(14969): Error (-38,0) 
05-11 13:17:57.642: I/System.out(14969): action down 
05-11 13:17:57.642: I/System.out(14969): points 1.6014493?21 
05-11 13:17:57.642: I/System.out(14969): show one page 
05-11 13:17:57.642: I/System.out(14969): start curl right 
05-11 13:17:57.650: I/System.out(14969): index value is 1/1024/1 
05-11 13:17:57.681: I/dalvikvm-heap(14969): Grow heap (frag case) to 30.401MB for 2261008-byte allocation 
05-11 13:17:57.775: E/MediaPlayer(14969): attachNewPlayer called in state 128 
05-11 13:17:57.821: I/dalvikvm-heap(14969): Grow heap (frag case) to 34.401MB for 4194320-byte allocation 
05-11 13:18:04.704: I/System.out(14969): action down 
05-11 13:18:04.704: I/System.out(14969): points 1.692029?21 
05-11 13:18:04.704: I/System.out(14969): show one page 
05-11 13:18:04.704: I/System.out(14969): start curl right 
05-11 13:18:04.704: I/System.out(14969): index value is 2/1024/2 
05-11 13:18:04.736: I/dalvikvm-heap(14969): Grow heap (frag case) to 30.315MB for 2261008-byte allocation 
05-11 13:18:04.829: E/MediaPlayer(14969): attachNewPlayer called in state 128 
05-11 13:18:04.876: I/dalvikvm-heap(14969): Grow heap (frag case) to 34.316MB for 4194320-byte allocation 
05-11 13:18:08.845: I/System.out(14969): action down 
05-11 13:18:08.845: I/System.out(14969): points 1.5471015?21 
05-11 13:18:08.845: I/System.out(14969): show one page 
05-11 13:18:08.845: I/System.out(14969): start curl right 
05-11 13:18:08.845: I/System.out(14969): index value is 3/1024/3 
05-11 13:18:08.876: I/dalvikvm-heap(14969): Grow heap (frag case) to 30.316MB for 2261008-byte allocation 
05-11 13:18:08.970: E/MediaPlayer(14969): attachNewPlayer called in state 16 
05-11 13:18:09.025: I/dalvikvm-heap(14969): Grow heap (frag case) to 34.316MB for 4194320-byte allocation 
05-11 13:18:12.892: I/System.out(14969): action down 
05-11 13:18:12.892: I/System.out(14969): points 1.5688405?21 
05-11 13:18:12.892: I/System.out(14969): show one page 
05-11 13:18:12.892: I/System.out(14969): start curl right 
05-11 13:18:12.892: I/System.out(14969): index value is 4/1024/4 
05-11 13:18:12.923: I/dalvikvm-heap(14969): Grow heap (frag case) to 30.316MB for 2261008-byte allocation 
05-11 13:18:13.025: E/MediaPlayer(14969): start called in state 4 
05-11 13:18:13.025: E/MediaPlayer(14969): error (-38, 0) 
05-11 13:18:13.064: I/dalvikvm-heap(14969): Grow heap (frag case) to 34.316MB for 4194320-byte allocation 
05-11 13:18:13.126: E/MediaPlayer(14969): Error (-38,0) 
05-11 13:18:13.126: I/MediaPlayer(14969): Don't send intent. msg.arg1 = 0, msg.arg2 = 0 
05-11 13:18:18.228: I/System.out(14969): action down 
05-11 13:18:18.236: I/System.out(14969): points -1.5289855?21 
05-11 13:18:18.236: I/System.out(14969): show one page 
05-11 13:18:18.236: I/System.out(14969): index value is 3/1024/3 
05-11 13:18:18.259: I/dalvikvm-heap(14969): Grow heap (frag case) to 30.316MB for 2261008-byte allocation 
05-11 13:18:18.353: E/MediaPlayer(14969): attachNewPlayer called in state 8 
05-11 13:18:18.400: I/dalvikvm-heap(14969): Grow heap (frag case) to 34.316MB for 4194320-byte allocation 
+0

看到這個http://stackoverflow.com/questions/16315043/audio-is-not-playing-after-stop-serviec-in-media-player – 2013-05-11 08:55:30

+0

@chintankhetiya我找不到正確的解決方案,這就是爲什麼我已經發布我的帖子,你可以幫我解決這個問題。 – Prateek 2013-05-11 08:58:41

+0

給出的鏈接是完美的,你可以用它,即使我也發佈我的答案。一探究竟。它的工作。 – 2013-05-11 09:01:26

回答

6

兩件事伸出:

  1. 您使用prepareAsync,這是異步。也就是說,您應該等待onPrepared回調,然後再執行任何依賴準備完成的任務(如致電start)。這就是爲什麼你得到「start called in state 4」錯誤信息(狀態4是MEDIA_PLAYER_PREPARING)。

  2. 如果在正在進行的回放過程中調用updateTrack,在爲新歌準備前似乎沒有停止MediaPlayer。撥打電話prepareMediaPlayer後,您已撥打電話stop,但這對您不會有什麼好處。爲了能夠重新準備球員,您必須首先stop,如果您還想更改數據源,您必須在停止球員後reset

有關一組有效狀態轉換請參閱MediaPlayer documentation's state diagram

+0

我基本上有一組文件對象,即filedescriptor inputstreams,你可以在上面的代碼中看到。如何以及在哪裏可以編輯,因爲我知道'FileDescriptor'輸入流被改變了,但問題只在於媒體播放器。 – Prateek 2013-05-11 09:06:52

+0

這似乎不太重要。在setDataSource返回後關閉FileInputStream是安全的。重要的是確保MediaPlayer對象處於正確的狀態;即在調用'start'之前確保'prepareAsync'已完成,並確保在執行另一個'prepareAsync'之前停止'prepareAsync'。 – Michael 2013-05-11 09:13:38

+0

我如何才能知道prepareAsyc是否已完成? – Prateek 2013-05-11 09:16:55