1
\$\begingroup\$

I have a Unity project with 3 separate looping Audio Clips and a single Audio Source. Based on certain events, I swap between which of them are playing.

This works fine, but whenever I swap between Audio Clips, the AudioSource stops playing, and must be restarted... which means it starts over from the beginning.

As the Audio Clips are the same length, how should I handle the logic to pickup where the previous Clip last left off? Neither Unity's AudioClip nor AudioSource classes seem to have either a "PlayFromTime()" or "GetPercentPlayed()" method that would facilitate using a single Audio Source.

What's the expected way to handle such a scenario? Simultaneously "play" from 3 AudioSources at once, muting 2 of them and leaving the last the only one that is actually heard?

\$\endgroup\$
2
\$\begingroup\$

You can use audio.time to get the current time for the music and also setting the time from where the music should start playing.

Link :- http://docs.unity3d.com/ScriptReference/AudioSource-time.html

Pseudo code (not tested)

AudioSource audio1;
AudioSource audio2;
AudioSource audio3;

audio1.time = audio2.time;
audio2.Stop();
audio1.Play();
\$\endgroup\$
  • 1
    \$\begingroup\$ An even better solution which prevents the music from changing too abruptly could be to cross-fade from one audio-source to the other. Keep playing the old source while starting to play the new one, but set the volume of the new one to 0. Then gradually increase the volume of the new source while decreasing the volume of the old. When the volume reached 0, stop the old source. (bonus points for having all soundtracks composed with the same beats-per-minute rate and starting them synchronized - this can make the transition seem almost unnoticeable). \$\endgroup\$ – Philipp Sep 8 '15 at 9:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.