我想使用循环的MediaPlayer这条赛道,但它使在最后这个奇怪的尖峰脉冲噪声,这条赛道似乎无畏做工精细,它使用.OGG,我尝试使用的Soundpool但我不能似乎是这一目标的工作。
SoundPool pool = new SoundPool(1, AudioManager.STREAM_MUSIC,0);
AssetFileDescriptor lfd = this.getResourc es().openRawResourceFd(R.raw.dishwasherloop);
//mediaPlayer = new MediaPlayer();
try
{
//mediaPlayer.setDataSource(lfd.getFileDescriptor(),lfd.getStartOffset(), lfd.getLength());
//mediaPlayer.prepare();
//mediaPlayer.start();
int dish = pool.load(lfd,1);
pool.play(dish,0.5f,0.5f,1,-1,1.0f);
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener()
{
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
}
});
int soundID = soundPool.load(this, R.raw.dishwasherloop, 1);
soundPool.play(soundID, 0.5f, 0.5f, 1, 0, 1f);
您需要移动:
soundPool.play(soundID, 0.5f, 0.5f, 1, 0, 1f);
入的onLoadComplete处理程序,否则的Soundpool将尝试播放声音它的实际加载之前。因此,像这样:
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener()
{
public void onLoadComplete(SoundPool soundPool, int sampleId, int status)
{
loaded = true;
soundPool.play(sampleId, 0.5f, 0.5f, 1, 0, 1f);
}
});
注:传递给的onLoadComplete处理程序sampleId
是加载soundId
。
此外,SoundPool.play(......)之内,你的循环标志设置为0,这意味着永远循环。如果您希望声音循环,这就需要为-1:
soundPool.play(sampleId, 0.5f, 0.5f, 1, -1, 1f);
希望帮助。