2012-11-29 88 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; 
      } 
     }