【发布时间】:2011-05-31 15:48:38
【问题描述】:
我最近购买了三星 Galaxy S,Android 2.1 更新,在运行我的应用程序后,我发现一些音效会同时播放两次。这很奇怪,因为表现出这种行为的声音效果似乎是随机的——在某些情况下,有些会播放两次,而有些会按预期播放一次。此错误尚未在我的应用程序的任何其他硬件平台上报告。我在这个网站上只看到过一个这样的报告事件,并且这个人转而使用 MediaPlayer,但是我真的很想为此得到补救。
当应用程序运行时,它会按如下方式启动 Soundpool,
public static final int SOUND_EXPLOSION = 1;
public static final int SOUND_CLEAR = 2;
public static final int SOUND_CLICK = 3;
public static final int SOUND_MAGIC = 4;
public static final int SOUND_ROCKET = 5;
public static final int SOUND_MELT = 6;
private SoundPool soundPool;
private AudioManager mgr;
private HashMap<Integer, Integer> soundPoolMap;
private void initSounds()
{
soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
soundPoolMap = new HashMap<Integer, Integer>();
soundPoolMap.put(SOUND_EXPLOSION, soundPool.load(getContext(), R.raw.explosion3, 1));
soundPoolMap.put(SOUND_CLEAR, soundPool.load(getContext(), R.raw.pop, 1));
soundPoolMap.put(SOUND_CLICK, soundPool.load(getContext(), R.raw.click, 1));
soundPoolMap.put(SOUND_MAGIC, soundPool.load(getContext(), R.raw.swoosh, 1));
soundPoolMap.put(SOUND_ROCKET, soundPool.load(getContext(), R.raw.rocket, 1));
soundPoolMap.put(SOUND_MELT, soundPool.load(getContext(), R.raw.melt3, 1));
mgr = (AudioManager)getContext().getSystemService(Context.AUDIO_SERVICE);
}
然后通过调用以下子播放声音效果,(PlaySound 是由用户选项切换的全局)
public void playSound(int sound)
{
if (PlaySound == true)
{
Log.w("playSound","Playing Sound" + sound);
float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = streamVolumeCurrent / streamVolumeMax;
soundPool.play(soundPoolMap.get(sound), volume, volume, 1, 0, 1f);
}
}
正如你所看到的,有一个日志调用,我用它来查看 sub 被调用了多少次,尽管这表明当错误发生时,当从设备听到两次声音时,例程只会调用一次。
我还有最后一个 sub,它在表面视图被破坏以进行整理时调用。
public void ReleaseSounds()
{
if (soundPool != null)
{
soundPool.release();
soundPool = null;
}
}
有没有其他人遇到过这个问题,如果有,您是如何解决的?对此的任何帮助将不胜感激,
在此先感谢
【问题讨论】:
-
还好它的早期,但问题似乎已经消失了。 raw 文件夹中的声音文件是 wav 和 mp3 的混合(原始格式),我已经将它们转换为 OGG 格式,从最初的测试来看,它似乎更稳定。
标签: android