我有一些代码设置,使用/raw/
对象一个接一个地在我的android.media.MediaPlayer
文件夹中播放3个声音文件。每次调用MediaPlayer播放时,都会随机选择3种声音中的两种。
然而,我已经意识到这真的不是最好的方法,因为声音片段太短了。我已经做了一些关注android.media.SoundPool
。但是,我不确定如何合并SoundPool
而不是使用MediaPlayer
。这就是我现在所拥有的。
任何有关如何从使用MediaPlayer改为SoundPool的指示都将不胜感激。
mpButtonOne = MediaPlayer.create(camera_page.this, R.raw.default);
if (mpButtonOne==null){
//display a Toast message here
return;
}
mpButtonOne.start();
mpButtonOne.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
int audio = mfile[rnd.nextInt(SOUND_CLIPS)];
audioReplayPrimary = audio;
mpButtonOne.reset();
mpButtonOne = MediaPlayer.create(camera_page.this, audio);
if (mpButtonOne==null){
//display a Toast message here
return;
}
mpButtonOne.start();
mpButtonOne.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
int audio2 = mfile2[rnd.nextInt(SOUND_CLIPS2)];
audioReplaySecondary = audio2;
mpButtonOne.reset();
mpButtonOne = MediaPlayer.create(camera_page.this, audio2);
if (mpButtonOne==null){
//display a Toast message here
return;
}
mpButtonOne.start();
mpButtonOne.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mpButtonOne.release();
}
更新
private static SoundPool mSoundPool;
private static AudioManager mAudioManager;
private final int SOUND_CLIPS3 = 5;
private int mfile3[] = new int[SOUND_CLIPS3];
private Random rnd = new Random();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sptest);
mfile3[0] = R.raw.one;
mfile3[1] = R.raw.two;
mfile3[2] = R.raw.three;
mfile3[3] = R.raw.four;
mfile3[4] = R.raw.five;
mSoundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
mAudioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
Button spTest = (Button) findViewById(R.id.buttonSp);
spTest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int audioPre = mfile3[rnd.nextInt(SOUND_CLIPS3)];
int sound1 = mSoundPool.load(sptestclass.this, audioPre, 1);
mSoundPool.play(sound1, 1f, 1f, 1, 0, 1f);
}
});
答案 0 :(得分:1)
您可以像这样使用android.media.SoundPool
对象:
import android.media.SoundPool;
//...
//inside your class:
//define: mContext = <your context>
private static SoundPool mSoundPool;
private static AudioManager mAudioManager;
//inside your method:
mSoundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
int sound1 = mSoundPool.load(mContext, R.raw.sound1, 1); //<context, soundId, priority>
int sound2 = mSoundPool.load(mContext, R.raw.sound2, 1); //<context, soundId, priority>
int sound3 = mSoundPool.load(mContext, R.raw.sound3, 1); //<context, soundId, priority>
//play a sound:
mSoundPool.play(sound1, 1f, 1f, 1, 0, 1f);//soundId (from load()), volLeft, volRight, priority, loop(boolean), playbackRate
//register a callback for onLoad...:
mSoundPool1.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
//call .play() for the next sound
}
});
//if you want to explicitly stop the sound:
mSoundPool.stop(sound1);