单击与音效匹配的图像按钮
Click an image button match with sound effect
下面的代码是单击带有一些动画的图像按钮,同时它带有声音效果。
问题是当我重复点击按钮时,音轨与按钮点击不匹配,音效延迟并且与点击次数不匹配。
[Class file] // 音轨来自 res/raw/track.mp3
final MediaPlayer click= MediaPlayer.create(this, R.raw.track);
btn1= (ImageButton) findViewById(R.id.btn1);
txt1= (TextView) findViewById(R.id.txt1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(AnimationUtils.loadAnimation(this, R.anim.rotate));
click.start();
}
});
您可能要考虑使用 SoundPool
。
试试这个:
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));
然后使用
播放您的声音
soundPool.play(soundId, 1, 1, 1, 0, 0);
这个答案链接到这个 post:POST
下面的代码是单击带有一些动画的图像按钮,同时它带有声音效果。 问题是当我重复点击按钮时,音轨与按钮点击不匹配,音效延迟并且与点击次数不匹配。
[Class file] // 音轨来自 res/raw/track.mp3
final MediaPlayer click= MediaPlayer.create(this, R.raw.track);
btn1= (ImageButton) findViewById(R.id.btn1);
txt1= (TextView) findViewById(R.id.txt1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(AnimationUtils.loadAnimation(this, R.anim.rotate));
click.start();
}
});
您可能要考虑使用 SoundPool
。
试试这个:
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));
然后使用
播放您的声音soundPool.play(soundId, 1, 1, 1, 0, 0);
这个答案链接到这个 post:POST