如何正确地在游戏中使用的Soundpool? [英] How to properly use SoundPool on a game?

查看:100
本文介绍了如何正确地在游戏中使用的Soundpool?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用的Soundpool有性能问题。每次我播放声音,帧速率下降。我加了一些日志,我可以看到的logcat的玩功能有时需要8ms的。

I'm having performance issues while using SoundPool. Everytime I play a sound, the frame rate drops. I added some logs and I can see on logcat that the "play" function sometimes takes 8ms.

我使用的是* .OGG文件和的Soundpool初始化在应用程序启动时完成的:

I'm using *.ogg files and the SoundPool initialization is done on the app startup:

mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
mSoundPool = new SoundPool(size, AudioManager.STREAM_MUSIC, 0);
mSoundPoolMap = new HashMap<Integer, Integer>();
mSoundPoolMap.put(index, mSoundPool.load(context, R.raw.shot, 1));

要播放声音,我用下面的code(游戏内循环):

To play the sound, I use the following code (inside the game loop):

mSoundPool.play(id, streamVolume, streamVolume, 1, loop, 1f);

我的问题是:

My questions are:

  • 该不该叫玩另一个线程,游戏外循环?
  • 我应该叫玩通过服务?
  • 难道我做错什么?

谢谢!

更新:

我只是测试播放声音在另一个线程,并通过服务播放声音,但滞后仍然是present。

I just tested playing the sound on another thread and playing the sound through a service, but the lag is still present.

然后我做了以下测试: *玩1000毫秒的时间间隔的声音 - >滞后总是会发生 *播放的声音在200毫秒的时间间隔 - >滞后从未发生

Then I did the following tests: * play the sound on 1000ms intervals -> the lag always happens * play the sound on 200ms intervals -> the lag NEVER happens

这怎么可能?!?!? 这些测试之后,似乎在没有什么打,的Soundpool被重置,当它要再次发挥,这需要更长的时间来初始化......很奇怪!

How is that possible?!?!? After these tests, it seems that when there is nothing playing, SoundPool is resetting and when it is going to play again, it takes longer to initialize... very weird!

推荐答案

我通过播放柔和的声音循环解决的问题:

I solved the issue by playing a muted sound in loop:

mSoundPool.play(id, 0, 0, 1, -1, 1f);

滞后完全消失!

The lag disappeared completely!

这似乎不应该做的事情,因此,如果任何人知道如何解释这一点,请告诉我们!

This doesn't seem the right thing to do, so if anyone knows how to explain this, please let us know!

更多信息: <一href="http://www.thiagorosa.com.br/en/how-to/improve-soundpool-performance">http://www.thiagorosa.com.br/en/how-to/improve-soundpool-performance

这篇关于如何正确地在游戏中使用的Soundpool?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆