我想用MediaPlayer循环这首曲子,但它最后发出了奇怪的杂音,曲目在Audacity中似乎工作得很好,它使用.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);发布于 2013-12-18 07:10:29
您需要移动:
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);希望这能有所帮助。
https://stackoverflow.com/questions/20584823
复制相似问题