2012-03-02 54 views
4

我想從SD卡播放音頻文件。我如何讀取音頻文件並播放它?下面是我的代碼來播放音頻文件:在我使用的Soundpool打從原文件夾中的音頻文件,上面的代碼從Sdcard播放音頻文件

int sound1; 
sound1 = mSoundPool.load(this, R.raw.om, 1); 
mSoundPool.play(sound1, 1, 1, 1, time - 1, 1); 

這裏,但我需要從SD卡使用的Soundpool播放的音頻文件。

現在有興趣播放SD卡的音頻。

如何實現這個目標?請幫助我需要儘快解決此問題

+0

您必須能夠從文件對象加載聲音。 – L7ColWinters 2012-03-02 05:17:13

+0

對不起,我沒有得到你? – Goofy 2012-03-02 05:18:07

+0

http://developer.android.com/guide/topics/data/data-storage.html#filesExternal – L7ColWinters 2012-03-02 05:23:29

回答

19

使用下面的代碼爲我工作。

MediaPlayer mp = new MediaPlayer(); 
mp.setDataSource("/mnt/sdcard/yourdirectory/youraudiofile.wav"); 
mp.prepare(); 
mp.start(); 
+0

@Goofy:如果我的答案適合您,請將其標記爲正確答案。如果他們面臨類似的問題,這將有助於他人。 – MobiDev 2016-01-19 07:39:35

3

我從SD卡中使用下面的代碼播放的聲音和我的作品。

private SoundPool mSoundPool; 
private int mSoundID; 
private boolean isLoaded = false; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Log.d(TAG, "Start Greeting View Activity..."); 

    setContentView(R.layout.greeting_view_activity); 
    //mGiGiView = (GreetingWidget) findViewById(R.id.gigi_greet); 
    //mGiGiView.setOnTouchListener(this); 

    //Set default animation sound path. 
    String soundAnimUrl = "/gigi/anim/evening.ogg"; 

    // get the Bundle out of the Intent. 
    Bundle extras = getIntent().getExtras(); 
    if (extras != null) { 

     // check to see if "soundAnimUrl" is in the bundle, if so then 
     // assign it's value to animUrl if not, assign null to soundAnimUrl. 
     soundAnimUrl = extras.containsKey("soundAnimUrl") ? extras 
       .getString("soundAnimUrl") : null; 
    } 

    // Set the hardware buttons to control the music. 
    this.setVolumeControlStream(AudioManager.STREAM_MUSIC); 

    // Load the sound. 
    mSoundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); 
    mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() { 
     @Override 
     public void onLoadComplete(SoundPool soundPool, int sampleId, 
       int status) { 
      isLoaded = true; 

      // Play the sound when loaded 
      play(); 
     } 
    }); 

    mSoundID = mSoundPool 
      .load(getFile(Environment.DIRECTORY_MUSIC, soundAnimUrl) 
        .getPath(), 1); 

    //Play sound from raw directory 
    // soundID = soundPool.load(this, R.raw.greeting1, 1);  
} 

private void play() { 
    // Getting the user sound settings 
    AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE); 
    float actualVolume = (float) audioManager 
      .getStreamVolume(AudioManager.STREAM_MUSIC); 
    float maxVolume = (float) audioManager 
      .getStreamMaxVolume(AudioManager.STREAM_MUSIC); 
    float volume = actualVolume/maxVolume; 

    // Is the sound loaded already? 
    if (isLoaded) { 
     mSoundPool.play(mSoundID, volume, volume, 1, 0, 1f); 
     Log.d(TAG, "Played sound"); 
    } 
} 

@Override 
public boolean onTouch(View v, MotionEvent event) { 
    if (event.getAction() == MotionEvent.ACTION_DOWN) { 
     switch (v.getId()) { 
     case R.id.gigi_greet: 
      play(); 
      break; 

     default: 
      break; 
     } 
    } 
    return false; 
} 

/** 
* Get File instance from sd card path. 
* 
* @param deviceFolderPath 
*   - Pictures, Music, etc 
* @param dbFilePath 
*   - path stored in db (/gigi/anim/morning.ogg) 
* @return 
*/ 
public File getFile(final String deviceFolderPath, final String dbFilePath) { 

    // Create full path 
    String picturePath = deviceFolderPath.concat(File.separator).concat(
      dbFilePath); 

    // Create file 
    File mFile = getExternalFilesDir(picturePath); 

    return mFile; 
}