Android 使用声音池的无缝循环
我想用MediaPlayer来循环这首歌,但它在结尾时发出了奇怪的小故障噪音,这首歌在Audacity中似乎运行良好,它使用了。OGG,我尝试过使用SoundPool,但我似乎无法做到这一点Android 使用声音池的无缝循环,android,loops,media-player,soundpool,Android,Loops,Media Player,Soundpool,我想用MediaPlayer来循环这首歌,但它在结尾时发出了奇怪的小故障噪音,这首歌在Audacity中似乎运行良好,它使用了。OGG,我尝试过使用SoundPool,但我似乎无法做到这一点 SoundPool pool = new SoundPool(1, AudioManager.STREAM_MUSIC,0); AssetFileDescriptor lfd = this.getResourc es().openRawResourceFd(R.raw.dishwasher
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);
希望有帮助