Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by Sousai · Sep 14, 2014 at 08:28 PM · audioaudiosourcesynchronizationsync

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sousai · Sep 21, 2014 at 09:18 PM 0
Share

Ill mess with this. Thank you!

avatar image bestimmaa · Mar 30, 2015 at 03:29 AM 0
Share

I was looking for a way more complicated solution...Thank u!

avatar image tayl0r · Apr 13, 2017 at 11:13 PM 0
Share

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!

avatar image Shadoninja · Feb 08, 2018 at 08:48 PM 0
Share

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.

avatar image
1

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;
             }
         }    
     } 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Video Texture and Audio start to desync on Pause and Play. 1 Answer

Syncing Audio with GetOutputData 1 Answer

Problem using AudioSource.timeSamples 1 Answer

Sample-precise sync on multiple audio sources ? 1 Answer

Particle System + Audio Source sync? (specifically sub effect) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges