libgdx音频延迟(不到一秒钟,但很明显)... Java / LibGDX

时间:2018-10-31 22:21:11

标签: java audio libgdx

我有一个中等大小的Java / Libgdx应用程序,播放时所有的声音片段都略有延迟。在应用程序的开头,这些工具均已加载资产管理器。它们的大小从7到220 kb不等。我尝试减少和增加文件的大小。我尝试了wav和ogg。有两个类处理与声音有关的操作,AssetMgr和声音。通过资产管理器加载声音后,将进行声音类活动。如有必要,我可以发布指向GitHub中类的链接。就内存消耗而言,虽然本来是一个很小的游戏,但也存在一些相当大的图形,但它们的总大小不足1 mb。我正在加载几个较小的像素图。

  

正在排队等待资产管理器加载声音...   //元素=要加载的声音列表。必须包含完整路径和扩展名。例如:“ assets \ beep.wav”,“ assets \ click.wav”。       公共无效queueSounds(String ... elements)       {

    // The function adds the passed sounds to the asset manager for future loading.

    // Loop through each element passed to function.
    for (String element : elements)
    {
        // Add current element in loop to queue.
        manager.load(element, Sound.class);
    }

}
  

正在复制Sounds类中对哈希映射的引用(请忽略音乐作品)...

    String filePath; // (Relative) path to sound or music -- used as key in asset manager.
    String musicKey; // Key to use with hash map -- one of the MusicEnum values, as a String.
    String soundKey; // Key to use with hash map -- one of the SoundEnum values, as a String.

    // Initialize the hash maps.
    soundMap = new HashMap<>();
    musicMap = new HashMap<>();

    // Populate hash map for sounds.

    // Loop through sound enumerations.
    for (HeroineEnum.SoundEnum soundEnum : HeroineEnum.SoundEnum.values())

    {

        // Get key / file path for sound.
        filePath = soundEnum.getValue_FilePath();

        // Get enumerated value for the sound, as a String.
        soundKey = soundEnum.toString();

        // Add sound related to current enumeration to hash map.
        soundMap.put(soundKey, hdg.getAssetMgr().manager.get(filePath));

    }
  

播放声音...

// whichSound = Name of sound to play.  Corresponds to one of the enumerated 
values in SoundEnum.
public void playSound(HeroineEnum.SoundEnum whichSound)
{

    // The function plays the passed sound -- matching the enumerated key value to the key in the hash map.

    // Play sound.
    soundMap.get(whichSound.toString()).play(audioVolume);

}

0 个答案:

没有答案