Unity Audio Networking Stops Playing after 10s
Hello, I'm trying to get audio networking running in a multiplayer VR application I'm working on, which is currently using the Photon Cloud as a dedicated server, but is NOT using the Photon Voice Chat System (and I don't want it to, this has to do with the fact that I'm prototyping currently, but will be moving it to a dedicated server eventually.
My problem is: I Instantiate the Listener and Source along with the player, enabled with photonView.isMine (the photon checker for if the prefab belongs to the player) and everything runs perfectly with one person
even if their are two people everything is instantiated correctly and each AudioSource is playing its own clip from the player microphone and sending it as an RPC to all players, again everything instantiates correctly and runs perfectly but only for about ten seconds, then the Microphone on both players stops recording (or so it seems)
Once one player disconnects, eventually the Microphone begins to play correctly again.
My guess, is that it has something to do with the encoding or something stupid I don't know about RPC's (like that I should have one for each player) but I am at a loss.
Here is my code for the VoiceChat void Update() { // If there is a connection //Debug.Log(ready); if(ready == true){ if (photonView.isMine) { //Debug.Log("Connected for Audio"); int pos = Microphone.GetPosition(null); int diff = pos-lastSample;
if (diff > 0)
{
float[] samples = new float[diff * sendingClip.channels];
sendingClip.GetData (samples, lastSample);
byte[] ba = ToByteArray (samples);
PhotonNetwork.RPC (photonView, "Send", PhotonTargets.AllViaServer, false, ba, sendingClip.channels);
//Debug.Log(Microphone.GetPosition(null).ToString());
}
lastSample = pos;
}
}
}
[PunRPC]
public void Send(byte[] ba, int chan) {
float[] f = ToFloatArray(ba);
if(audio != null){
audio.clip = AudioClip.Create("" +photonView.ownerId, f.Length, chan, FREQUENCY,true,false);
audio.clip.SetData(f, 0);
Debug.Log(audio.isPlaying);
if (!audio.isPlaying) audio.Play();
}
}
// Used to convert the audio clip float array to bytes
public byte[] ToByteArray(float[] floatArray) {
int len = floatArray.Length * 4;
byte[] byteArray = new byte[len];
int pos = 0;
foreach (float f in floatArray) {
byte[] data = System.BitConverter.GetBytes(f);
System.Array.Copy(data, 0, byteArray, pos, 4);
pos += 4;
}
return byteArray;
}
// Used to convert the byte array to float array for the audio clip
public float[] ToFloatArray(byte[] byteArray) {
int len = byteArray.Length / 4;
float[] floatArray = new float[len];
for (int i = 0; i < byteArray.Length; i+=4) {
floatArray[i/4] = System.BitConverter.ToSingle(byteArray, i);
}
return floatArray;
}
Your answer
Follow this Question
Related Questions
Getting server browser with photon ? 3 Answers
Check what group id you have after being instantiated. (PUN) 1 Answer
Problem in Setting Photon Teams 2 Answers
Client Disconnect on Photon Server using MLAPI 0 Answers
Unity Photon doesn't Spawn Player 0 Answers