Loading an AudioClip Nightmare of Despair and Sorrow

Why can’t you define a variable…

var noize : AudioClip;

and then…

Start();
{
Somehow load an audio clip into noize that wasn't dragged there in the Inspector?
}

I have read forum post after forum post after forum post of people asking for a way to do this, and I’ve never seen an answer that works. Sorry if I sound gruff, my air conditioning is out, it’s a hundred degrees in here, and two lines of code (or an inability to have those two lines) may have just destroyed two months of work.

  • Bro B

Don’t despair ! There is more than likely a solution. I am aware of this but havn’t used it personally (yet) :

Resource Folders

Resource Folders are collections of assets that are included in the built Unity player, but are not necessarily linked to any GameObject in the Inspector.

To put anything into a Resource Folder, you simply create a new folder inside the Project View, and name the folder “Resources”. You can have multiple Resource Folders organized differently in your Project. Whenever you want to load an asset from one of these folders, you call Resources.Load().

info : http://docs.unity3d.com/Documentation/Manual/LoadingResourcesatRuntime.html

API Load : http://docs.unity3d.com/Documentation/ScriptReference/Resources.Load.html

API Unload_Unused : http://docs.unity3d.com/Documentation/ScriptReference/Resources.UnloadUnusedAssets.html

let me know if there are any probs using this =]

Is this what you’re looking for? In the following example I play a clip until termination, then load another, different clip and play it until termination too. The source is attached to the main camera and my sound assets are located in my scene’s Resources/Audio Sources/ directory. My default scene’s Audio Source/Audio Clip has “None (Audio Clip)”, meaning, I do not pre-load any audio assets.

My environment is probably different than most in that I’m in QA, so “Testing” functions aren’t available to you, or desirable, but the general algorithm should be sound for general use.

using UnityEngine;
using System.Collections;

public class AudioSequencerTest : MonoBehaviour
{
    /// <summary>
    /// The main camera with the audio source and test code attached
    /// </summary>
    public GameObject MainCamera;

    /// <summary>
    /// Define the Audio Source
    /// </summary>
    public GameObject AudioSource;

    /// <summary>
    /// initialize the default sound clip
    /// </summary>
    private string audioClipName1 = "SaucerAnimation";

    private string audioClipName2 = "LGMAttack";

    /// <summary>
    /// Use this for initialization and launch of tests
    /// </summary>
    void Start()
    {
        //give plenty of time for the audio clips to play fully
        Testing.SetRealTimeTimeout(30.0f);
        Testing.TestIsSupposedToTimeout();
        
        Invoke("InitializeTests", 0.2f);
    }
   
    /// <summary>
    /// Staging method for calling tests
    /// </summary>
    private void InitializeTests()
    {
        StartCoroutine("PlayOneClip");  
    }

    /// <summary>
    /// Play audio, 2nd one starts after first one finishes
    /// </summary>
    /// <returns>enumerator for method position</returns>
    protected IEnumerator PlayOneClip()
    {
        //dynamically load up the desired audio asset
        MainCamera.audio.clip = GetAudioClip(audioClipName1);

        //play the audio asset
        audio.Play();
       
        //besides being able to listen to the clip, also document it
        if(audio.isPlaying)
        {
            AudioClipIsPlaying(MainCamera.audio.clip.name);
        }

        //yield back to the InitializeTests after the whole clip plays
        yield return new WaitForSeconds(MainCamera.audio.clip.length);

        //returned from InitializeTests, load up the next desired audio asset
        MainCamera.audio.clip = GetAudioClip(audioClipName2);

        //play it
        audio.Play();

        if(audio.isPlaying)
        {
            AudioClipIsPlaying(MainCamera.audio.clip.name);
        }

        //we're done here.  return after whole clip plays
        yield return new WaitForSeconds(MainCamera.audio.clip.length);
    }
    
    #region helper methods

    /// <summary>
    /// Document progress of testing
    /// </summary>
    /// <param name="s">the name of the audio asset to load</param>
    private void AudioClipIsPlaying(string s)
    {
        Testing.PrintExpected(s + " audio clip is playing");
        Testing.Print(s + " audio clip is playing");
    }

    /// <summary>
    /// load audio asset as type AudioClip from subdir in Resources dir
    /// </summary>
    /// <param name="audioClipName"></param>
    /// <returns>audio asset as AudioClip</returns>
    private AudioClip GetAudioClip(string audioClipName)
    {
        return Resources.Load("Audio Sources/" + audioClipName) as AudioClip;
    }

    #endregion
}