简体   繁体   中英

Load and play sounds from sd card?

I have button that plays sounds using SoundPool. I want to open my app and load sounds from SD Card and then play them with my button. Can anyone give me an example how to do that? This is my Java code :

package com.example.idea;

import android.media.SoundPool;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {
SoundPool sp;
int mSoundId;
int mStreamId;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mSoundId = sp.load(this, R.raw.sound1, 1);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
public void button1(View view){
    if (mStreamId != 0) {
        sp.stop(mStreamId);
    }
    mStreamId = sp.play(mSoundId, 1, 1, 1, 0, 1f);
}
}

Why not use SoundPool.load(String path, int priority) ?

Using it you should be able to open file from SD card.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM