使用MediaPlayer播放SDCard的声音?

时间:2012-11-29 07:15:16

标签: android android-mediaplayer

让我解释一下:

  1. 我有一个名为“Sounds”的文件夹,还有一些.ogg文件可以播放。
  2. 我使用Environment.getExternalStorageDirectory().getAbsolutePath() + mySoundsPath;获得了文件夹路径。
  3. 我从该文件夹中获取了所有列表并将其保存到数组:List<String> soundList;
  4. 我的问题是:

    1. 如何调用我创建的soundList的声音,这样他们都可以播放?
    2. 是否需要解码(如图像,解码为Bitmap)??
    3. 对不起我的语法。 提前谢谢。

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

public void play(String path){                 计数++;                 playFile =路径;

            //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;
           } 
        }