void RpcPlayAudio(byte[] encoded) { if (lastPlayed >= recordFrequency) { lastPlayed -= recordFrequency; } float[] decoded = VoiceUtils.Decompress(encoded); percentPlayed = percentPlayed + ((double)decoded.Length / aud.clip.samples); GetComponent <AudioSource> ().clip.SetData(decoded, lastPlayed); if (!isPlaying) { GetComponent <AudioSource> ().Play(); aud.loop = true; isPlaying = true; } if (GetComponent <AudioSource> ().time >= percentPlayed) { //GetComponent<AudioSource> ().Pause (); isPlaying = false; Debug.Log("not caught up"); } if (percentPlayed >= 1) { percentPlayed = percentPlayed - 1; } Debug.Log("Recording sent."); lastPlayed += decoded.Length; }
void Update() { if (!isLocalPlayer) { return; } playbackDelay += Time.deltaTime; if (isTransmitting) { int currentPos = Microphone.GetPosition(null); int diff = currentPos - lastPos; int partitionSize = recordFrequency / cBuffer.BufferLength; if (currentPos < lastPos) { diff = recordFrequency - lastPos + currentPos - 1; } if (diff >= partitionSize) { sampleBuffer = new float[diff * aud.clip.channels]; aud.clip.GetData(sampleBuffer, lastPos); cBuffer.Enqueue(sampleBuffer); lastPos = currentPos; } if (!cBuffer.IsEmpty) { if (playbackDelay >= 0.05f) { byte[] sampleBytes = new byte[sampleBuffer.Length * 4]; sampleBytes = VoiceUtils.Compress(cBuffer.Dequeue()); CmdStopRecording(sampleBytes); playbackDelay = 0; } } } if (Input.GetKeyDown(KeyCode.O)) { Debug.Log("Recording started."); StartRecording(); } if (Input.GetKeyDown(KeyCode.P)) { Microphone.End(null); Debug.Log("Recording ended."); isTransmitting = false; } }