2013-08-19 130 views
1

我有一個ListView與一些項目,每個項目播放一定的聲音。這是代碼:再次播放之前停止MediaPlayer無法正常工作

listView.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
      long arg3) { 
      try { 
       player.setDataSource(externalStoragePath + "/Android/data/com.whizzappseasyvoicenotepad/" + recordedFilesArray.get(arg2) + ".mp3"); 
       player.prepare(); 
       player.start(); 
      } catch (IllegalArgumentException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (SecurityException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IllegalStateException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    }); 

如果我點擊一個項目,它開始播放,然後一個我之前點擊播放完畢,在MediaPlayer只會同時播放聲音在同一時間之前單擊另一個項目。如何讓我的MediaPlayer停止()播放先前的聲音並開始播放新的聲音,如果在停止播放之前點擊了該項目?

我嘗試在嘗試之前添加player.Stop(),以便每次單擊一個項目時,MediaPlayer都會在設置新源並重新開始之前停止,但這隻會停止以前的聲音並且不會播放新的聲音。

我也嘗試添加IF語句,就像這樣:

if (!player.isPlaying()){ 
//normally start the player 
} 
else if (player.isPlaying()){ 
//stop it before starting it again 
} 

我想複製我的實際代碼,但我已經刪除它,因爲它沒有工作。它也剛剛停止了以前的聲音,並沒有播放新的聲音。

回答

0

停止歌曲後重置您的播放器之前調用reset()

listView.setOnItemClickListener(new OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
     long arg3) { 
     try { 
      if (player!=null && player.isPlaying()) 
       {      
       player.stop(); 
       player.reset(); 
       } 

      player.setDataSource(externalStoragePath +  "/Android/data/com.whizzappseasyvoicenotepad/" + recordedFilesArray.get(arg2) + ".mp3"); 
      player.prepare(); 
      player.start(); 
     } catch (IllegalArgumentException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (SecurityException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IllegalStateException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
}); 
0

您必須在空閒狀態下撥打setDataSource()。所以,你必須調用setDateSource()

listView.setOnItemClickListener(new OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
     long arg3) { 
     try { 
      player.reset(); 
      player.setDataSource(externalStoragePath + "/Android/data/com.whizzappseasyvoicenotepad/" + recordedFilesArray.get(arg2) + ".mp3"); 
      player.prepare(); 
      player.start(); 
     } catch (IllegalArgumentException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (SecurityException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IllegalStateException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
}); 
0
listView.setOnItemClickListener(new OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
     long arg3) { 
     try { 
      stopPlaying(); 
      initializeMediaPlayer(externalStoragePath + "/Android/data/com.whizzappseasyvoicenotepad/" + recordedFilesArray.get(arg2) + ".mp3"); 
      startPlaying(); 

     } catch (IllegalArgumentException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (SecurityException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IllegalStateException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
}); 



private void stopPlaying() { 
    if (player!=null && player.isPlaying()) { 
     player.stop(); 
     player.release(); 
    } 
} 

private void initializeMediaPlayer(String path) { 
    player = new MediaPlayer(); 
    try { 
     player.setDataSource(path); 
    } catch (IllegalArgumentException e) { 
     e.printStackTrace(); 
    } catch (IllegalStateException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

private void startPlaying() { 
    try { 
     player.prepareAsync(); 
     player.setOnPreparedListener(new OnPreparedListener() { 
     @Override 
     public void onPrepared(MediaPlayer mp) { 
      if(player!=null) 
      player.start(); 
      } 
     }); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
0

重用MediaPlayer,則必須在設置新dataSource之前調用reset()。還有一件事,你不應該在UI線程中調用prepare(),它可能會導致ANR錯誤。您寧可致電prepareAsync()函數。