在 Android 上,当循环设置为 -1 时,不会播放声音
我正在使用 SoundPool 为我的 Android 应用程序播放声音。所有声音都在应用程序开始时加载,以防止应用程序本身出现任何滞后。只要未设置为循环,所有声音都可以正常播放。当我将循环设置为 true(设置为 -1)时,声音不会播放。
有 6 种不同的声音,根据用户的输入停止和开始。它可以正常播放第一个声音,但随后的声音无法播放。
我尝试了各种方法来解决这个问题,例如暂停而不是停止,将音量设置为0并将循环设置为0而不是暂停,将循环设置为任意大的数字而不是真正的重复,使声音更短,但这些都不是已经工作了。
我的代码如下:
public int loadSound(int soundFile)
{
int soundID;
try
{
if (m_loadedSounds.containsKey(soundFile))
{
soundID = m_loadedSounds.get(soundFile);
}
else
{
soundID = m_soundPool.load(m_context, soundFile, 1);
m_loadedSounds.put(soundFile, soundID);
}
}
catch (Exception e)
{
soundID = -1;
}
return soundID;
}
public void playSound(int soundFile, boolean loop)
{
// Grab it from the map
int soundID = loadSound(soundFile);
int loops = loop ? -1 : 0;
float streamVolume = m_audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume /= m_audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
// If succeeded then play the sound
if (soundID != -1)
{
if (m_soundStreams.containsKey(soundID))
{
int streamID = m_soundStreams.get(soundID);
m_soundPool.setLoop(streamID, loops);
m_soundPool.resume(streamID);
}
else
{
int streamID = m_soundPool.play(soundID, streamVolume, streamVolume, 1, loops, 1.0f);
Log.d("SOUNDS", "Sound played! ID: " + soundID + " At volume: " + streamVolume + " Looping: " + (loop ? "Yes": "No") + " Success: " + (streamID != 0 ? "Yes" : "No"));
if (streamID != 0)
{
m_soundStreams.put(soundID, streamID);
}
}
}
}
public void stopSound(int soundFile)
{
int soundID = loadSound(soundFile);
Integer streamID = m_soundStreams.get(soundID);
if (streamID != null)
{
m_soundPool.pause(streamID);
}
}
LogCat 在运行时给出的错误是:
01-03 16:03:20.142: ERROR/AudioFlinger(2359): not enough memory for AudioTrack size=670096
01-03 16:03:20.142: DEBUG/MemoryDealer(2359): AudioTrack (0x389a0, size=1048576)
01-03 16:03:20.142: DEBUG/MemoryDealer(2359): 0: 0005b090 | 0x00000000 | 0x00010080 | F
01-03 16:03:20.142: DEBUG/MemoryDealer(2359): 1: 0006db58 | 0x00010080 | 0x0007B8A0 | A
01-03 16:03:20.142: DEBUG/MemoryDealer(2359): 2: 0005af70 | 0x0008B920 | 0x0005C280 | A
01-03 16:03:20.142: DEBUG/MemoryDealer(2359): 3: 000752c0 | 0x000E7BA0 | 0x00018460 | F
01-03 16:03:20.142: DEBUG/MemoryDealer(2359): size allocated: 883488 (862 KB)
01-03 16:03:20.142: ERROR/AudioTrack(11584): AudioFlinger could not create track, status: -12
01-03 16:03:20.142: ERROR/SoundPool(11584): Error creating AudioTrack
有谁知道这个恼人问题的任何解决方案,或者我还没有尝试过的任何解决方法?
I'm using a SoundPool to play sounds for my Android app. All of the sounds are loaded up at the beginning of the app to prevent any lagging during the app itself. All sounds play fine as long as they are not set to looping. When i set looping to be true (set as -1) then the sound does not play.
There are 6 different sounds that are stopping and starting depending on the user's input. It plays the first sound fine but then sounds afterwards fail to play.
I have tried various ways around it, such as pausing rather than stopping, setting volume to 0 and loops to 0 rather than pausing, setting loops to an arbitrary large-ish number rather than true repeating, making the sounds much shorter and none of these have worked.
My code is as follows:
public int loadSound(int soundFile)
{
int soundID;
try
{
if (m_loadedSounds.containsKey(soundFile))
{
soundID = m_loadedSounds.get(soundFile);
}
else
{
soundID = m_soundPool.load(m_context, soundFile, 1);
m_loadedSounds.put(soundFile, soundID);
}
}
catch (Exception e)
{
soundID = -1;
}
return soundID;
}
public void playSound(int soundFile, boolean loop)
{
// Grab it from the map
int soundID = loadSound(soundFile);
int loops = loop ? -1 : 0;
float streamVolume = m_audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume /= m_audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
// If succeeded then play the sound
if (soundID != -1)
{
if (m_soundStreams.containsKey(soundID))
{
int streamID = m_soundStreams.get(soundID);
m_soundPool.setLoop(streamID, loops);
m_soundPool.resume(streamID);
}
else
{
int streamID = m_soundPool.play(soundID, streamVolume, streamVolume, 1, loops, 1.0f);
Log.d("SOUNDS", "Sound played! ID: " + soundID + " At volume: " + streamVolume + " Looping: " + (loop ? "Yes": "No") + " Success: " + (streamID != 0 ? "Yes" : "No"));
if (streamID != 0)
{
m_soundStreams.put(soundID, streamID);
}
}
}
}
public void stopSound(int soundFile)
{
int soundID = loadSound(soundFile);
Integer streamID = m_soundStreams.get(soundID);
if (streamID != null)
{
m_soundPool.pause(streamID);
}
}
And the error given in LogCat at run time is:
01-03 16:03:20.142: ERROR/AudioFlinger(2359): not enough memory for AudioTrack size=670096
01-03 16:03:20.142: DEBUG/MemoryDealer(2359): AudioTrack (0x389a0, size=1048576)
01-03 16:03:20.142: DEBUG/MemoryDealer(2359): 0: 0005b090 | 0x00000000 | 0x00010080 | F
01-03 16:03:20.142: DEBUG/MemoryDealer(2359): 1: 0006db58 | 0x00010080 | 0x0007B8A0 | A
01-03 16:03:20.142: DEBUG/MemoryDealer(2359): 2: 0005af70 | 0x0008B920 | 0x0005C280 | A
01-03 16:03:20.142: DEBUG/MemoryDealer(2359): 3: 000752c0 | 0x000E7BA0 | 0x00018460 | F
01-03 16:03:20.142: DEBUG/MemoryDealer(2359): size allocated: 883488 (862 KB)
01-03 16:03:20.142: ERROR/AudioTrack(11584): AudioFlinger could not create track, status: -12
01-03 16:03:20.142: ERROR/SoundPool(11584): Error creating AudioTrack
Does anybody know of any solutions to this annoying problem, or any workarounds that I haven't tried?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我能想到的最佳解决方案是为正在执行此操作的声音添加一个特殊情况,并为这些声音使用 MediaPlayer。然后我必须停下来&当不使用它们时释放它们,然后当我想再次播放它们时重新加载它们。当声音发生变化时,加载时间明显但很短,但这是我能做的最好的了。任何其他答案仍然将不胜感激。
The best solution I could come up with is to put a special case in for the sounds that are doing this and use a MediaPlayer for these sounds. Then I have to stop & release them when they're not used and then reload them when I want to play them again. Has a noticeable, yet slight, loading time when the sounds change but it's the best I can do. Any other answers would still be much appreciated.
确保在 onPause() 中释放 SoundPool 资源以防止内存泄漏。您还应该在 onResume() 中加载声音。
Make sure you are releasing your SoundPool resources in onPause() to prevent leaking memory. You should be loading your sounds in onResume() also.
我也有同样的问题。当您开始播放想要循环播放的声音时,将声音的优先级设置为 99,然后降低其余的。值越高优先级越高。然而,我在播放声音时遇到了很大的延迟问题。如果您有任何解决方案,请通知我。(我也使用 soundpool 和 int 数组作为 ID 池。)
I had the same problem. When you are begining to play the sound you want to be looped, set priority of sound to 99 and lower the rest. The heigher value the heigher priority. However I have a big problem of latency when playing sounds. If you have any solution please notify me.(I'm using soundpool too and int array as ID pool.)
尝试这个解决方案。它应该有效。
Try this solution. its should work.