- Home /
How to make two audio tracks play in sync?
I'm pretty new to Unity, so I really don't understand quite a lot, but basically what I am trying to understand is how to get two audio tracks to start at the exact same time.
For Example, in my game, there will be a main track playing all the time, but lets say when the characters HP is low, there's a hidden part of the song that is in a second file all by itself that is turned on. So then it would be main track + extra track that was not heard until then.
Another example would be if there was song playing but it lacked a beat and when the character encountered a monster or something of the sort, the beat track would present itself.
I hope I'm being fairly clear about this, but I could use some help on how I would even go about creating a system such as this.
If any of you have played Little Big Planet and have messed around with the Interactive music, this is essentially what I'm talking about.
Answer by rutter · Sep 14, 2014 at 08:43 PM
Each AudioSource tracks its playback time. You can check this by reading its time field (which gives you time in seconds) or its timeSamples field (which gives you time in terms of sample count).
Even better: you can assign these values to other AudioSources, to keep them in sync with each other. This is easiest to do if all involved audio clips have the same length, sample rate, and compression settings, but if you're able to sort out the math, you can achieve it in other cases.
Time in seconds is easier to understand, but less accurate and therefore slightly less useful.
Time in samples is very accurate, but can be hard to work with if your audio clips have varying sample rates.
The simplest possible case could go something like this, synchronizing sample time once per frame:
using UnityEngine;
using System.Collections;
public class AudioSync : MonoBehaviour {
//set these in the inspector!
public AudioSource master;
public AudioSource slave;
void Update() {
slave.timeSamples = master.timeSamples;
}
}
You can get quite a bit more involved than that, but that should be enough to get you started.
I was looking for a way more complicated solution...Thank u!
Does this actually work with compressed sounds on mobile? I can only get my sounds to sync up if I'm using an uncompressed format, which is not ideal since they're HUGE!
I will be trying this out when I get home tonight. I am very excited! I was worried a trick like this would be a gigantic pain or even impossible for one reason or another.
Answer by KevinAnthony · May 19, 2017 at 02:05 PM
Old thread but I wanted to sync more than one AudioSource and found this solution. Not sure if it's too safe, but it's working right now for me. The previous code block caused a lot of clipping, which I image could get a lot worse with a poor frame rate and multiple AudioSources.
//set these in the inspector!
public AudioSource master;
public AudioSource[] slaves;
private IEnumerator SyncSources()
{
while (true)
{
foreach (var slave in slaves)
{
slave.timeSamples = master.timeSamples;
yield return null;
}
}
}