Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by PabloVermicelli · Jun 02, 2015 at 02:29 AM · multiplayeraudiophotonmusic

Synching music in multiplayer game

Hi all,

I'm making a multiplayer shooter where there is a jukebox that plays music. The music can be heard by all players, but is not in sync. Is it possible to make it so that when a player joins it syncs the audio with the other player?

I am using PUN (Photon Unity Networking) for my networking.

Here's my current code for the music player:

AudioSource audio;

 public AudioClip[] playlist;

 public AudioClip track1;
 public AudioClip track2;
 public AudioClip track3;
 public AudioClip track4;
 public AudioClip track5;

 // Use this for initialization
 void Start () {
     audio = GetComponent<AudioSource> ();
     playlist = new AudioClip[5];
     playlist [0] = track1;
     playlist [1] = track2;
     playlist [2] = track3;
     playlist [3] = track4;
     playlist [4] = track5;
 }
 
 // Update is called once per frame
 void Update () {
     if (!audio.isPlaying) {
         photonView.RPC ("PLAYADAMUSIC", PhotonTargets.All);
     }
 }

 [RPC]
 void PLAYADAMUSIC(){
     audio.clip = playlist[Random.Range(0, 4)];
     audio.Play ();
 }
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
0

Answer by YoungDeveloper · Jun 02, 2015 at 02:39 AM

If you want close to 100% sync you could send a timestamp and on receive play it considering the receive delay.

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
avatar image
0

Answer by Eno-Khaon · Jun 02, 2015 at 02:40 AM

I'd say start with a look at this. Your server/host can potentially know "best" at what point the music track is currently at, then tell the clients when they're in range to start the music starting at a specific point (using audio.timeSamples).

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 PabloVermicelli · Jun 02, 2015 at 10:28 AM 0
Share

This indeed seems like the right way to do it. Though I'm not really sure how I can set the offset of the music track for each specific player when they join

avatar image Eno-Khaon · Jun 02, 2015 at 10:38 AM 0
Share

Depending on the audio data rate Unity is set to (i.e. 48000kHz or 44100kHz or the like), that number multiplied by the number of seconds into the playing song is where it will be at the time.

You can get the value from the host/server through audio.timeSamples, then use a time stamp to send to the clients to tell them what (time * sample rate) to play the song from for them, setting their current time into the music using the same audio.timeSamples.

Once the latency is interpreted, it should be possible to match up the audio with reasonably great precision.

avatar image PabloVermicelli · Jun 02, 2015 at 11:22 AM 0
Share

I understand what you're saying, but I'm not exactly sure how I go about doing that. Do I let my networkmanager script do all the work, or can I do this within the script of the music player?

avatar image Eno-Khaon · Jun 02, 2015 at 06:30 PM 0
Share

The network manager would likely need some piece of information to distribute to other players, so it could potentially just be sent the timeSamples data from the music player, to then be distributed among other players. If the network manager isn't the exclusive source of network data, then there probably wouldn't be any reason why the music player couldn't send small bits of data itself.

In that case, however, a host/server differentiation would still be quite important when synchronization is key.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Audio being added to android playlist, unwanted 1 Answer

how to sync audio in instantiated object so all players can hear it? 0 Answers

Why does Photon find a room so quick! 1 Answer

Bootcamp Multiplayer error photon 0 Answers

How to make enemy players invisible using Photon? 1 Answer


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