how to convert string to audioclip?

i need to reproduce audio depending on given string
playing an specific animation with a string is easy. it’s just this:

[SerializeField] Animator animator;

animator.Play(clouds);

but how to play an audiosource or audioclip with string?

further context i’m with a dialogue system that uses tags (strings). and i want to control audio from it

THANKS IN ADVANCE!

Set up your audio to be addressables. Use the string as the addressable path and load up the audioclip and pass that to your audiosource. At least, that’s how I did it in a recent project. But, you really just need a way to associate your string with the proper audioclip.

1 Like

haven’t heard of addressables. i’ll give them a try
THANKS! <3

how i associate string with audioclip?

Ah, you might be better using scriptableobjects.

But for addressables, you need to be able to load it from the addressable system.

public static void GetAudioClip(string clipId, Action<AudioClip> callback)
{
   Addressables.LoadAssetAsync<AudioClip>(clipId).Completed += (AsyncOperationHandle<AudioClip> handle) =>
   {
       callback?.Invoke(handle.Result);
   }
}

Essentially you pass in the addressable path and it will return the audioclip tied to that path. Note that you can change the addressable path to whatever you want, you don’t have to use the default it puts in for you.

AudioSource.clip is of type AudioClip, you are assigning a value of type string to it. AudioClips are not strings, therefor you cant make the assignment to that variable.

you need something like

audioSource.clip = GetClipFromString("cloud");

AudioClip GetClipFromString(string clipName){
//return the AudioClip associated with clipName
}

this function is an example of an association from string to AudioClip that Brethnann was referring to. The simplest way for you to acheive your goal may be the following, but i dont think it is very scaleable and wouldn’t use it for 10000 clips.

public class AudioClipGetter{

[SerializeField] List<AudioClip> clips; //add your clips to this list in inspector
Dictionary<string, AudioClip> dict;
void Awake(){
    dict = new Dictionary<string, AudioClip>();
    for(int i = 0; i < clips.Count; i++){
            AudioClip clip = clips[i];
            dict[clip.name] = clip;
    }
}

//this is your association function
public AudioClip GetClip(string clipName){
    if(dict.ContainsKey(clipName)) return dict[clipName];
    else return null;
}

}

Again this is not very scaleable, but should do the job.

2 Likes

thanks!

    for(int i = 0; i < clips.Count; i++){
        dict[clip.name] =clip;
    }

that code is there to populate the dictionary right? but what’s happening in second line? i don’t get it

Yes that populates the dictionary. if by second line, you mean the ‘GetClip’ function, that is how you will be able to get an audioClip from a string, allowing you to perform

myAudioSource.clip = myClipGetter.GetClip("cloud.mp3");

notice the return type of the function is of type AudioClip, making it a valid value to assign to audioSource.clip.
I added some more notes to the class for you or anyone in the future stumbling upon this.

public class AudioClipGetter{
    //add your clips to this list in inspector
    [SerializeField] List<AudioClip> clips;

    //this is the the object that will hold a mapping of strings to AudioClips
    Dictionary<string, AudioClip> dict;


    void Awake(){
        //initialize an empty dictionary, so we can add clip names and their associated AudioClips to it
        dict = new Dictionary<string, AudioClip>();

        //add all clips to the dictionary, such that they string that will be used to be associated
        //with them will be the name of the clip file (i.e. 'cloud.mp3')
        for(int i = 0; i < clips.Count; i++){
            AudioClip clip = clips[i];
            dict[clip.name] = clip;
        }
    }
  
    //this is your association function. you provide it the name of the clip (i.e. 'cloud.mp3')
    //as the parameter to the function. It returns the clip with that name, if you added that clips
    //to the serialized list field 'clips'.
    public AudioClip GetClip(string clipName){
        //check that there is a clip associated with clip
        if(dict.ContainsKey(clipName)) return dict[clipName];
        else return null;
    }
}
1 Like

thanks!!
by second line i meant the second one in the code i quoted. sorry for the confusion! TT_TT

dict[clip.name] =clip;

ahh, that is what actually creates the mapping in the dictionary between clip.name and the AudioClip. I editted that part of the code in my response because there was a missing line of code. check out “for loop” and “Dictionary” in C# (but they are popular in most languages) if any of it seems foreign to you.

1 Like

thanks!! <3

1 Like