我在原始文件夹中有三个声音文件。我想一个接一个地播放它们,它们之间没有延迟。我试过他们用下面的代码玩,但所有三个文件都在一起玩,而不是一个接一个。这三个文件都小于 50 kb。我还想更改它们的播放速率,这就是我使用 SoundPool 而不是 MediaPlayer 的原因。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
mSoundPoolMap = new HashMap<Integer, Integer>();
mAudioManager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
final int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mSoundPoolMap.put(1, mSoundPool.load(this, R.raw.nancy, 1));
mSoundPoolMap.put(2, mSoundPool.load(this, R.raw.cymbal, 1));
mSoundPoolMap.put(3, mSoundPool.load(this, R.raw.njs, 1));
Button SoundButton = (Button)findViewById(R.id.Button);
SoundButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundPool.play(mSoundPoolMap.get(1), streamVolume, streamVolume, 1, 0, 1f);
mSoundPool.play(mSoundPoolMap.get(2), streamVolume, streamVolume, 1, 0, 1f);
mSoundPool.play(mSoundPoolMap.get(3), streamVolume, streamVolume, 1, 0, 1f);
}
});
最佳答案
为了完成您想要做的事情,您可能必须创建自己的自定义 AsyncTask,它接收一组 SoundPool 对象(或者可能是一个 HashMap),实现方式如下:
private class SoundPoolTask extends AsyncTask <Soundpool, String, String>
{
@Override
protected String doInBackground(SoundPool... params) {
try {
//For each SoundPool object
// int soundLength = however long the SoundPool object is
// Play the sound
// Thread.sleep(soundLength);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(String... values) {}
@Override
protected void onPostExecute(String result) {
//Clean up your class objects
}
}
您可能需要对声音的原始长度和播放速率进行某种计算,以确定每个声音的长度。希望对您有所帮助!
关于android - 使用 Android 的 SoundPool 类播放音频文件序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12283604/