Audio information from AudioManager

Hello everyone,
I am new to Unity and I hope you can help me with my problem. My project requires that sounds are played after another. To achieve this, it seems necessary to access the length of the first sound file to create a delay for the second one. However, I created a script to implent an AudioManager an now dont know how to access these information.

I think was I need is a way to receive the AudioSource from the AudioManager.

My code looks as follows:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Windows.Speech;
using System.Linq;

public class VoiceMovement : MonoBehaviour
{
    public Animator anim;

    public AudioManager theAudio;

    public KeywordRecognizer keywordRecognizer;
    public Dictionary<string, System.Action> actions = new Dictionary<string, System.Action>();

    public void Start()
    {

        actions.Add("start", start);

        anim = GetComponent<Animator>();

        theAudio = GameObject.Find("AudioManager").GetComponent<AudioManager>();

        keywordRecognizer = new KeywordRecognizer(actions.Keys.ToArray());
        keywordRecognizer.OnPhraseRecognized += KeyWordRecognizerOnPhraseRecognized;
        keywordRecognizer.Start();
    }

    public void KeyWordRecognizerOnPhraseRecognized(PhraseRecognizedEventArgs speech)
    {

        Debug.Log(speech.text);
        actions[speech.text].Invoke();

    }

    public void start()
    {
        anim.Play("intro");
        theAudio.Play("intro_audio");

        // HERE I WANT TO ADD THE DELAY
        yield return new WaitForSeconds(HERE!!!);

        // HERE I WANT TO PLAY THE SECOND SOUND
        theAudio.Play("start_audio");

    }

Thanks!

1 Answer

1

Hey,
AudioClip.length is the length in seconds of any audioclip (unless you’re streaming)

you can access the current AudioClip using AudioSource.clip

so in your case:

theAudio.Play("intro_audio");
yield return new WaitForSeconds(theAudio.Source.clip.length);

This assumes:

  1. AudioManager exposes an AudioSource Source property
  2. your audioclip will have been changed to the correct clip when you yield
  3. you’re not streaming the clip from somewhere