Audio interruption! How do I start paused audio behind the word it paused on?

I’m new to unity and have been using bolt to learn coding. just so you know


I have audio that plays as dialogue for a character and if I do something that interrupts his dialogue his interruption dialogue will play and his original dialogue will pause.
Once the interruption dialogue is finished I need something to detect if my paused audio is on a word and have it back up the time line to the point before the word for when its un-paused.

Anyone know how I could do this?

So, have unity and audacity open at the same time.
Create two float arrays. Start Times and End Times.
The index of these two arrays will represent a words start and end. Just pick the words/segments you want restart in the event of interruption.

Now manually fill the array with that data by fetching the times of words.
You will want an array of the words as well, so you know what the times correspond to, so that you know where you are when you want to make changes.

Pseudo code

//...
public string[] words;
public float[] startTimes;
public float[] endTimes;


//When the dialogue gets interrupted
//Loop through the times arrays and decide what to do based on what time the audiosource is at.

myAudioSource.Pause();

for (int x = 0; x < startTimes.Length; x++) {

 if ((myAudioSource.time > startTimes[x]) && (myAudioSource.time < endTimes[x])) {
  //We were interrupted during this word. Reset audio time to word start
  myAudioSource.time = startTimes[x];
  break;
 }

}

If your game is full of dialogue and this is a core mechanic that you can do all the time, and you want to automate this instead of doing it manually, then that’s not so easy.

It’s unlikely you’d get it as accurate or as flexible as doing it manually, but Audioclip.GetData can supposedly get amplitude data from a clip. You might be able to use that to find and automatically map out all the quiet parts and talking parts for you and fill the array with the times. It will probably require significant averaging of the data and fiddling with and testing different thresholds.

If you don’t want to deal with maps, and you’d rather deal with it at runtime (which will cost you efficiency), then you can use myAudioSource.GetOutputData, which also gets the amplitudes, but in real time as they are played. It will still require fiddling, but you could just keeping saving the latest relatively quiet time, and then when you get interrupted in a louder time, just reset to that quiet time.

1 Like