Forum Discussion
Anonymous
7 years agoMultiplayer App Background Music Syncing
I'm developing a multiplayer VR app in Unity with Photon. I want to have background music which is in sync for all players. But what's happening with what I've tried so far is that the music starts fr...
Anonymous
7 years agoThanks for your input, MikeF. I should say I'm kind of a newbie at coding and Photon. Someone named JLF responded to the same post as this one on the Unity forum. He provided code which works to sync music in a single player game, but he's never worked with Photon. JLF's code is here: https://forum.unity.com/threads/multiplayer-app-background-music-syncing.767105/
I've tried unsuccessfully to adapt his code into a script which uses Photon. The problem I'm having is how to get the "firstStartTime" variable (as I understand it, the time in the game the music starts for the first player) to later players. My script (which doesn't work) is attached to the player prefab and is copied below. My current set up is not to have the first player instantiate an audio source/music clip object; rather, that object is attached to the player prefab. If you could point me in the right direction I'd really appreciate it!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using TMPro;
using Photon.Realtime;
I've tried unsuccessfully to adapt his code into a script which uses Photon. The problem I'm having is how to get the "firstStartTime" variable (as I understand it, the time in the game the music starts for the first player) to later players. My script (which doesn't work) is attached to the player prefab and is copied below. My current set up is not to have the first player instantiate an audio source/music clip object; rather, that object is attached to the player prefab. If you could point me in the right direction I'd really appreciate it!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using TMPro;
using Photon.Realtime;
public class DWM_PlayerSetup : MonoBehaviourPunCallbacks
{
{
[SerializeField] GameObject centerEyeAnchor;
[SerializeField] GameObject bulletStartPos;
[SerializeField] GameObject audioSourceObject;
[SerializeField] TextMeshProUGUI playerNameText;
// Per JLF script:
public AudioSource audioSource;
public double firstStartTime;
public double clipLength;
public int sampleRate;
public AudioSource audioSource;
public double firstStartTime;
public double clipLength;
public int sampleRate;
void Start()
{
//Per JLF script:
clipLength = GetClipLength();
// DWM:
if (PhotonNetwork.CurrentRoom.PlayerCount == 1)
photonView.RPC("StartMusic", RpcTarget.AllBuffered);
//else
//photonView.RPC("StartMusicWithDelay", RpcTarget.AllBuffered);
{
//Per JLF script:
clipLength = GetClipLength();
// DWM:
if (PhotonNetwork.CurrentRoom.PlayerCount == 1)
photonView.RPC("StartMusic", RpcTarget.AllBuffered);
//else
//photonView.RPC("StartMusicWithDelay", RpcTarget.AllBuffered);
if (photonView.IsMine == false)
{
transform.GetComponent<DWM_FPControllerVRAllPurpose3DII>().enabled = false;
bulletStartPos.GetComponent<DWM_Shoot>().enabled = false;
centerEyeAnchor.SetActive (false);
audioSourceObject.SetActive(false);
}
{
transform.GetComponent<DWM_FPControllerVRAllPurpose3DII>().enabled = false;
bulletStartPos.GetComponent<DWM_Shoot>().enabled = false;
centerEyeAnchor.SetActive (false);
audioSourceObject.SetActive(false);
}
SetPlayerUI();
}
void SetPlayerUI()
{
if (playerNameText != null)
{
playerNameText.text = photonView.Owner.NickName;
Debug.Log("Player has been named. - DWM");
}
}
{
if (playerNameText != null)
{
playerNameText.text = photonView.Owner.NickName;
Debug.Log("Player has been named. - DWM");
}
}
// Per JLF script:
double GetClipLength()
{
sampleRate = audioSource.clip.frequency;
double length = (double)audioSource.clip.samples / sampleRate;
return length;
}
[PunRPC]
void StartMusic()
{
firstStartTime = AudioSettings.dspTime + 1;
audioSource.PlayScheduled(firstStartTime);
}
double GetClipLength()
{
sampleRate = audioSource.clip.frequency;
double length = (double)audioSource.clip.samples / sampleRate;
return length;
}
[PunRPC]
void StartMusic()
{
firstStartTime = AudioSettings.dspTime + 1;
audioSource.PlayScheduled(firstStartTime);
}
public override void OnPlayerEnteredRoom(Player newPlayer)
{
gameObject.GetComponent<PhotonView>().RPC("StartMusicWithDelay", RpcTarget.OthersBuffered, firstStartTime);
}
{
gameObject.GetComponent<PhotonView>().RPC("StartMusicWithDelay", RpcTarget.OthersBuffered, firstStartTime);
}
[PunRPC]
public void StartMusicWithDelay()
{
double scheduledStartTime = AudioSettings.dspTime + 1;
double timeSinceStart = (scheduledStartTime - firstStartTime) % clipLength;
int samplePosition = (int)(sampleRate * timeSinceStart);
audioSource.timeSamples = samplePosition;
audioSource.PlayScheduled(scheduledStartTime);
}
}
public void StartMusicWithDelay()
{
double scheduledStartTime = AudioSettings.dspTime + 1;
double timeSinceStart = (scheduledStartTime - firstStartTime) % clipLength;
int samplePosition = (int)(sampleRate * timeSinceStart);
audioSource.timeSamples = samplePosition;
audioSource.PlayScheduled(scheduledStartTime);
}
}
Quick Links
- Horizon Developer Support
- Quest User Forums
- Troubleshooting Forum for problems with a game or app
- Quest Support for problems with your device
Other Meta Support
Related Content
- 6 months ago
- 2 years ago
- 13 days ago