从SD卡播放音频文件

4

我想从sd卡播放一个音频文件。我该如何读取音频文件并播放它呢?以下是我的播放音频文件的代码:

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

在上面的代码中,我使用SoundPool从raw文件夹播放音频文件,但是我需要使用SoundPool从sdcard播放音频文件。

现在有兴趣从sdcard播放音频。

如何实现这一点?请帮忙,我需要尽快解决这个问题。


你需要能够从文件对象中加载声音。 - L7ColWinters
http://developer.android.com/guide/topics/data/data-storage.html#filesExternal - L7ColWinters
2个回答

19

使用下面的代码,它对我有效。

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

@Goofy:如果我的答案对您有用,请将其标记为正确答案。这将帮助其他人解决类似的问题。 - AndroidDev

4
我使用以下代码从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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接