本文介绍了按钮点击和声音之间的延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不太明白为什么按钮点击和声音之间会有延迟.
I do not quite understand why there is a delay between the button click and sound.
以下是我的代码
button = (Button) findViewById(R.id.playBtn);
final MediaPlayer playButtonClick = MediaPlayer.create(this, R.raw.buttonsound);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
playButtonClick.start();
Intent browserIntent =
new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
}
});
反正我可以减少延迟吗?
Is there anyway I can reduce the delay?
推荐答案
我认为您应该考虑使用SoundPool.
I think you should consider using SoundPool instead.
SoundPool soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 100);
HashMap<Integer, Integer> soundPoolMap soundPoolMap = new HashMap<Integer, Integer>();
soundPoolMap.put(soundID, soundPool.load(this, R.raw.your_sound, 1));
然后您可以使用以下声音播放声音:
And then you can play the sound using:
soundPool.play(soundId, 1, 1, 1, 0, 0);
这篇关于按钮点击和声音之间的延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!