Search Unity

AudioClip from AssetBundle cannot be played after changed scene

Discussion in 'Scripting' started by rockyip, Nov 19, 2017.

  1. rockyip

    rockyip

    Joined:
    Jul 23, 2012
    Posts:
    10
    I am using Unity2017.1.1f1 to make a mobile game which need to optimize the size
    So I decide to move all audioclips from Resource to AssetBundles
    I use UnityWebRequest to download and cache the assetbundles.
    Then I load all sounds from bundles and store them into a dictionary for later use

    At the first scene, all sounds are played properly, but after I load another scene, all the sounds from the dictionary become Unloaded.
    I tried to check the loadedState and call LoadAudioData if it is unloaded. Unfortunately, it will throw an error
    Error: Cannot create FMOD::Sound instance for resource archive...... (File not found. )

    And it seems only happen in Mac version of Unity. I tested on Window and all sounds play well in different scenes.

    I searched there are some posts talking about this problem since Unity 5.* but still no solution ?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    There is a load-cached-or-download option to UnityWebRequest that is analogous to the old one on WWW... I forget the syntax exactly, but why don't you just reload them from there into your dictionary when you start each scene? Unless they're massive I assume they'll be already cached transparently by Unity and will instantly load.

    The syntax for the above is subtly different from the WWW but there is sample code out there. If I recall you access the DownloadHandler property inside the UnityWebRequest and then call .GetContent() on it to get the bundle out.
     
  3. rockyip

    rockyip

    Joined:
    Jul 23, 2012
    Posts:
    10
    Do you mean I need to reload all sounds from assetBundle per scenes?
    Thanks for advice. I have added code to load all the sounds from bundle again if any sound is lost.

    But for the BGM it will lost suddenly after scene changed which I think it is not a good experience to players.
    But I wonder if it is an expected behaviour that audioclips may become unloaded after scene changed?
     
  4. iamvideep

    iamvideep

    Joined:
    Oct 27, 2017
    Posts:
    118
    Does Dont destory on load work in this case? I cam curious to know if it can keep the audio files safe!
    Or how about static variable? That can store values which can be fetched from another scene.

    let me know...

    Thanks
     
  5. rockyip

    rockyip

    Joined:
    Jul 23, 2012
    Posts:
    10
    I have a SoundManager using Singleton Pattern, there is a list inside to store all audioclips
    The problem is that the audio clips from the assetBundle did not lost after scene change. They are still in the list but just the loadedState become Unloaded and cannot play.

    On the other hand, load the audio clips by using Resource.Load do not have this problem
     
  6. iamvideep

    iamvideep

    Joined:
    Oct 27, 2017
    Posts:
    118
    Any specific reasons to use a singleton?
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Here's just an odd thing to try: after you load the audioclips out of the asset bundle, keep a static reference to that asset bundle, which should prevent it from getting unloaded when you change scenes. I wonder if the linkage between the loaded audioclip and the bundle they came out of is failing on that one platform and allowing the underlying data to get disposed. It's worth a try...
     
  8. rockyip

    rockyip

    Joined:
    Jul 23, 2012
    Posts:
    10
    I want the SoundManager can be accessed anytime to play sounds, so I choose to use Singleton pattern. Well, it's also used DontDestoryOnLoad