SoundPool 减慢应用程序速度

发布于 2024-11-16 03:48:17 字数 1306 浏览 2 评论 0原文

我有一个名为 Sound.java 的类,它由 3 个函数(initSound、addSound 和 playSound)组成。

public static void initSound(Context con) {
    mContext = con;
    soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
    soundPoolMap = new HashMap<Integer, Integer>();
    audioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
    streamVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    streamVolume = streamVolume / audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
}

public static void addSound(int index, int SoundID) {
    soundPoolMap.put(index, soundPool.load(mContext, SoundID, 1));
}

public static void playSound(int index) {
    soundPool.play(soundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
}

我在 MainGame.java 构造函数中调用了 initSound 和 addSound。

Sound.initSound(getContext());
Sound.addSound(1, R.raw.machine_gun);

并在 MainGame.java 中的线程(循环)内调用 playSound。 PlaySound 在事件触发时每秒调用一次(例如,当敌人出现在视野中时,部队(多个)将连续射击(播放声音)直到敌人死亡)。

Sound.playSound(1);

问题是当声音播放时,应用程序速度变慢。 我使用 soundpool 是因为据我所知,soundpool 比 mediaplayer 的音效更好。 (我尝试过媒体播放器,延迟甚至更多。)

我使用的声音文件是.wav(无符号8位PCM,1通道,8000hz),大小为5.44KB。

有没有更好的方法来播放效果声音而不降低游戏性能,或者我错了?我真的很感谢任何想法和回应。

I have a class called Sound.java that consists of 3 functions (initSound, addSound, and playSound).

public static void initSound(Context con) {
    mContext = con;
    soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
    soundPoolMap = new HashMap<Integer, Integer>();
    audioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
    streamVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    streamVolume = streamVolume / audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
}

public static void addSound(int index, int SoundID) {
    soundPoolMap.put(index, soundPool.load(mContext, SoundID, 1));
}

public static void playSound(int index) {
    soundPool.play(soundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
}

I called the initSound and addSound in the MainGame.java constructor.

Sound.initSound(getContext());
Sound.addSound(1, R.raw.machine_gun);

and called playSound inside a Thread (looping) in MainGame.java. PlaySound called every second when an event is triggered (for example, when an enemy is in sight, troops (more than one) will shoot (play the sound) continuously until the enemy is dead).

Sound.playSound(1);

The problem is when the sound plays, the app is slowing down.
I'm using soundpool because as far as I know for sound effects, soundpool is better than mediaplayer. (I've tried mediaplayer and the lag is even more.)

The sound file that I use is .wav (unsigned 8bit PCM, 1 Channel, 8000hz) with size 5.44KB.

Is there a better way to play effect sounds without slowing the game performance, or is mine wrong? I really appreciate any ideas and responses.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我喜欢麦丽素 2024-11-23 03:48:17

根据SoundPool 文档,它解码为 16 位 PCM,因此您可以更改为该格式,看看是否能从中获得一些性能。除此之外,您的代码看起来与我之前做过的代码非常相似(至少据我所知),并且我没有看到任何重大的性能问题(我相信我什至没有使用 WAV,我只是使用 OGG,但我不太记得了)。您是在实际设备上尝试还是仅在模拟器上尝试?

According to the SoundPool docs, it decodes to 16-bit PCM, so you could change to that and see if you get some performance out of that. Other than that, your code seems pretty similar (at least as far as I can remember) to stuff I've done before and I didn't see any significant perf problems (I believe I wasn't even using WAV, I was just using OGGs, but I don't remember for sure). Are you trying this on an actual device, or just the emulator?

木緿 2024-11-23 03:48:17

我也有同样的问题。然后我为 soundpool 创建了一个新线程,并在 while 循环中添加了 20 毫秒的睡眠时间。问题就消失了。

I had the same problem. Then I've created a new thread for the soundpool and added 20ms sleep into the while loop. The problem is gone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文