android - 在 Android 上将循环设置为 -1 时不播放声音

标签 android audio soundpool

我正在使用 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

有没有人知道这个烦人问题的任何解决方案,或者我没有尝试过的任何解决方法?

最佳答案

我能想到的最佳解决方案是为执行此操作的声音设置一个特殊案例,并为这些声音使用 MediaPlayer。然后我必须在不使用它们时停止并释放它们,然后在我想再次播放它们时重新加载它们。当声音改变时有明显但轻微的加载时间,但这是我能做的最好的。任何其他答案仍将不胜感激。

关于android - 在 Android 上将循环设置为 -1 时不播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8714078/

相关文章:

java - 如何在两个 Activity 之间翻转动画(如卡片翻转)?

android - 如何修复 "UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView"

windows - Windows 10 音频输入设备设置存储在哪里?

javascript - cordova媒体插件状态不起作用

android - Volley 无法创建缓存目录。无法清理文件

android - StylePlayerView Controller xml 覆盖播放和暂停按钮不起作用

c# - Monogame-音乐和声音效果无法同时播放

java - SoundPool 和 MediaPlayer 中的并发性?

android - 如何在android中将录制的男声转换为女声?

android - 如何在 Android 中跨不同设备在准确的时间段播放声音