1
\$\begingroup\$

I am making a system where I have one long audio clip made up of voice lines which I want to be able to play one after the other with a variable pause in between.

I press the space key to start the clip, and I want it to pause after a set time and then I will press the enter key to start the next line(s).

I am able to get the audio to pause inside Update() with AudioSource.Pause(); but for some reason it will not work inside the coroutine I have set to pause it after a number of seconds.

Since I do not want to have to time the pauses myself, how can I achieve this? The debug messages inside the SoundWaiter() function are all printing, but the audio is not being paused.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SoundTracker : MonoBehaviour
{
    public AudioSource soundFX;
    public bool pausing = false;
    public int count = 0;

    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Return))
        {
            pausing = !pausing;
        }

        if(pausing)
        {
            // This works, but I don't want to have to time the pauses myself
            //soundFX.Pause();
        }

        if (!pausing)
        {
            soundFX.UnPause();
        }

        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (count == 0)
            {
                soundFX.Play();
                StartCoroutine(SoundWaiter(10));
            }
            //if (count == 1)
            //{
                //soundFX.UnPause();
                //StartCoroutine(SoundWaiter(5));
            //}
            //if (count == 2)
            //{
                //soundFX.UnPause();
                //StartCoroutine(SoundWaiter(12));
            //}
            //count = count + 1;
        }
    }
    IEnumerator SoundWaiter(float duration)
    {
        Debug.Log("In waiter...");
        yield return new WaitForSeconds(duration);
        Debug.Log("Pausing audio...");
        soundFX.Pause();
        Debug.Log("Paused audio.");
    }
}
```
\$\endgroup\$
1
  • 1
    \$\begingroup\$ You might want to look into using fmod for your audio; it's a huge improvement over the built in audio and probably free for you. \$\endgroup\$ Oct 27 at 16:31

1 Answer 1

3
\$\begingroup\$

Okay, I figured out the issue. It is pausing the audio, but since I left soundFX.UnPause(); inside the if (!pausing) block inside Update(), right after it pauses it will be unpaused by Update().

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Awesome! Once the timer expires, you can accept this answer as the right one so other users will know it's been answered correctly. :) \$\endgroup\$
    – Almo
    Oct 26 at 17:39
  • 1
    \$\begingroup\$ Sure, I will do that! \$\endgroup\$
    – Michael
    Oct 27 at 15:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .