Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

Loading AudioClips from AssetBundles in Unity 5 does not work

Discussion in 'Scripting' started by fredrik-larsson, Oct 5, 2016.

  1. fredrik-larsson

    fredrik-larsson

    Joined:
    Nov 3, 2015
    Posts:
    29
    Hi!

    After upgrading a project from 4.6.5f1 to 5.4.0f3 we've found a severe problem with loading audioclips from assetbundles in Unity 5.

    http://answers.unity3d.com/question...o-from-asset-bundles-not-working-in-un-1.html
    http://answers.unity3d.com/questions/1096450/loading-audio-from-asset-bundles-not-working-in-un.html

    And many more appears to have the same problem. The clip is correctly recognized after unpacking the assetbundle but the AudioData for the clip is not loadable and thus the clip can never be played. Even after explicitly calling:

    Code (CSharp):
    1.  
    2. if(clip.loadType == AudioClipLoadType.Streaming)
    3. {
    4.      bool success = clip.LoadAudioData();
    5.      if (!success)
    6.          Debug.LogError("Could not load AudioData for: " + clip.name);
    7. }
    8.  
    The LoadAudioData() function returns true marking that it was successful in loading the audiodata but this produces the
    exception in the first link above.

    What is the correct way to place AudioClips in assetbundles and load them correctly in Unity 5?
     
    Last edited: Oct 5, 2016
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,749
    Not sure if it will help and I haven't tried to use audioclips in bundles yet. But I'm using unity's manager
    https://www.assetstore.unity3d.com/en/#!/content/45836

    Which comes with examples. The one I'm using includes this type of example.
    AssetBundleLoadAssetOperation solutionRequest = AssetBundleManager.LoadAssetAsync(assetBundleName, assetSolutionName, typeof(Sprite));

    To load a sprite. Then I'm doing
    Sprite solution = solutionRequest.GetAsset<Sprite>();

    Then of course, assigning the sprite to it's image. So if sprite is replaced with audioclip, perhaps you can get it to work.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    4,527
    You may need to ensure you don't have two assets (say and AudioClip and something else) with precisely the same name (not including the extension obviously).

    When you use the generic form of Resources.Load<>(), it will correctly find the typed asset. If I recall correctly, the bundle loader uses a subtly different approach of loading the asset by name (regardless of type), then casting it to the type, which can result in failure to load an asset, i.e., if it loaded an identically-named Sprite first, then tried to cast it to an AudioClip.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,749
    Good point for OP. I know my bundles have a prefab and an image, they are not named the same.
     
  5. fredrik-larsson

    fredrik-larsson

    Joined:
    Nov 3, 2015
    Posts:
    29
    Thanks for the reply. Putting breakpoints in visual studio correctly shows the AudioClip after AssetBundle.LoadAsset has been called for the container prefab as a valid reference.

    This is my container class which is built to an AssetBundle:

    Code (CSharp):
    1. public class InterstitialData : ScriptableObject
    2. {
    3.     public AreaNotificationsDatabaseString Text = new AreaNotificationsDatabaseString();
    4.     public AudioClip VoiceOver;
    5.     public AudioClip Music;
    6.     public Texture Portrait;
    7. }
    After loading the InterstitialData from the AssetBundle, I can correctly load and play the "VoiceOver" clip. The music clip however is not possible to play.

    This is due to the import settings being different between the two. As the Music clip is a lot larger than the VoiceOver, I want to be able to stream this clip instead of preloading and decompressing the entire clip into memory on play.

    These are the relevant AudioClip settings for the objects:

    voice_clip.JPG music.JPG

    Where you can see that the VoiceClip can correctly check the "Preload Audio Data" checkbox due to it being marked as "Compressed In Memory" allowing it to be correctly played when loaded from an assetbundle.

    The Music clip however, is not able to check the "Preload Audio Data" box and we are unable to make an explicit call to the LoadAudioData() function as described above, even though it returns as succeeding.