SoundPoolUtils-播放声音
播放声音。
更新于:2015-08-10
import android.media.AudioManager;
import android.media.SoundPool;
public class SoundPoolUtils {
private static SoundPool sp;
/**
* 播放一段声音
*
* @param path 声音文件路径,如:/mnt/sdcard/abc/abc.mp3
* @return 播放声音的id
*/
public static int play(String path) {
if (sp == null) {
sp = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
}
int id = sp.load(path, 1);
sp.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
soundPool.play(sampleId, 1f, 1f, 1, 0, 1);
}
});
return id;
}
/**
* 停止播放
*
* @param soundId 由play方法返回的id
*/
public static void stopSound(int soundId) {
if (sp != null) {
sp.stop(soundId);
}
}
public static void release() {
if (sp != null) {
sp.release();
sp = null;
}
}
}