2012-11-29 36 views
0

讓我清楚地解釋:使用MediaPlayer播放SDCard中的聲音?

  1. 我已經文件夾命名爲「聲音」,並有一些.OGG文件播放。
  2. 我得到了使用Environment.getExternalStorageDirectory().getAbsolutePath() + mySoundsPath;的文件夾路徑。
  3. 我從該文件夾中的所有列表,並將其保存到數組:List<String> soundList;

我的問題是:

  1. 如何調用從soundList我創建的,因此所有可以播放的聲音? ?
  2. 是否需要解碼(如圖像,解碼爲位圖)?

對不起,我的語法。 在此先感謝。

回答

0
+0

感謝參考.. 我用我的邏輯從參考.. MediaPlayer的只是可以從渣油和URI .. 這是不可能的,如果我使用渣油,所以我嘗試將字符串轉換烏里.. 和它的工作.. 並再次感謝您的參考..:D – Faras

0

公共無效播放(字符串路徑){ 計數++; playFile = path;

  //showNotification(); 
     new NotificationPanel(activity); 


      if(mediaPlayer!=null && mediaPlayer.isPlaying()){ 
       Log.d("*****begin*****", "playing"); 
       stopPlaying(); 
       Log.d("*****begin*****", "stoping"); 
       } else{ 
       Log.d("*****begin*****", "nothing"); 
       } 


      Uri myUri1 = Uri.parse(path); 
      mediaPlayer = new MediaPlayer(); 
      mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 

      try { 
       mediaPlayer.setDataSource(activity, myUri1);    
      } catch (IllegalArgumentException e) { 
       Toast.makeText(activity, "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); 
      } catch (SecurityException e) { 
       Toast.makeText(activity, "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); 
      } catch (IllegalStateException e) { 
       Toast.makeText(activity, "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      try { 
       mediaPlayer.prepare(); 
      } catch (IllegalStateException e) { 
       Toast.makeText(activity, "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); 
      } catch (IOException e) { 
       Toast.makeText(activity, "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); 
      } 
      mediaPlayer.start(); 

     } 

     private void stopPlaying() { 
      if (mediaPlayer != null) { 
       mediaPlayer.stop(); 
       mediaPlayer.release(); 
       mediaPlayer = null; 
      } 
     }