2012-03-02 4 views
4

sdcard에서 오디오 파일을 재생하고 싶습니다. 오디오 파일을 읽고 어떻게 재생할 수 있습니까? 다음은 오디오 파일 재생 내 코드입니다 : 내가 원시 폴더에서 오디오 파일을 재생 soundpool을 사용하고 위의 코드에 다음Sdcard에서 오디오 파일 재생

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

는하지만 난 soundpool를 사용하여 SDCARD에서 오디오 파일을 재생해야합니다.

지금은 sdcard에서 오디오 재생에 관심이 있습니다.

어떻게 작성 하시겠습니까? 이 문제를 최대한 빨리 해결할 수 있도록 도와주세요.

+0

파일 개체에서 사운드를로드 할 수 있어야합니다. – L7ColWinters

+0

미안하지만 못 들었어? – Goofy

+0

http://developer.android.com/guide/topics/data/data-storage.html#filesExternal – L7ColWinters

답변

19

아래 코드를 사용해보세요.

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

@Goofy : 내 답변이 도움이 되었다면 정답으로 표시하십시오. 다른 사람들이 비슷한 문제를 겪고 있다면 도움이 될 것입니다. – MobiDev

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