点击图片按钮与音效匹配

时间:2016-01-09 16:03:50

标签: android button soundeffect animator

下面的代码是单击带有动画的图像按钮,同时具有声音效果。 问题是当我重复点击按钮时声音轨道与按钮点击不匹配,声音效果延迟且与点击次数不匹配。

[班级档案] //音轨来自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();

        }
    });

1 个答案:

答案 0 :(得分:0)

您可能需要考虑使用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

相关问题