簡體   English   中英

使用MediaPlayer播放SDCard的聲音嗎?

[英]Playing sound from SDCard with MediaPlayer?

讓我清楚地解釋一下:

  1. 我有一個名為“ Sounds”的文件夾,並且有一些.ogg文件要播放。
  2. 我使用Environment.getExternalStorageDirectory().getAbsolutePath() + mySoundsPath;獲得了文件夾路徑Environment.getExternalStorageDirectory().getAbsolutePath() + mySoundsPath;
  3. 我從該文件夾中獲取了所有列表,並將其保存到數組中: List<String> soundList;

我的問題是:

  1. 如何從我創建的soundList中調用聲音,以便它們都可以播放?
  2. 它是否需要解碼(如圖像,解碼為位圖)?

對不起,我的語法。 提前致謝。

public void play(String path){count ++; 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;
           } 
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM