【问题标题】:Click an image button match with sound effect [duplicate]单击与音效匹配的图像按钮[重复]
【发布时间】:2016-01-09 16:03:50
【问题描述】:

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

[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();

        }
    });

【问题讨论】:

    标签: android button soundeffect animator


    【解决方案1】:

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

    【讨论】:

    • 感谢信息老兄,但我做了一些调整。 SoundPool soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 100); int soundId = soundPool.load(this, R.raw.track, 1); soundPool.play(soundId, 1, 1, 1, 0, 0);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-30
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多