/// <summary> /// Decode and buffer audio data to be played /// </summary> /// <param name="data">The data passed to USpeakOnSerializeAudio()</param> public void ReceiveAudio(byte[] data) { if (settings == null) { Debug.LogWarning("Trying to receive remote audio data without calling InitializeSettings!\nIncoming packet will be ignored"); } if (MuteAll || Mute || (SpeakerMode == SpeakerMode.Local && !DebugPlayback)) { return; } if (SpeakerMode == SpeakerMode.Remote) { talkTimer = 1.0f; } int offset = 0; while (offset < data.Length) { int len = System.BitConverter.ToInt32(data, offset); byte[] frame = new byte[len + 6]; System.Array.Copy(data, offset, frame, 0, frame.Length); USpeakFrameContainer cont = default(USpeakFrameContainer); cont.LoadFrom(frame); playBuffer.Add(USpeakAudioClipCompressor.DecompressAudioClip(cont.encodedData, (int)cont.Samples, 1, false, settings.bandMode, RemoteGain)); offset += frame.Length; } }
public void SetData(byte[] data, string id, Action <string> end) { this.id = id; onEnd = end; ac = this.gameObject.AddComponent <AudioSource> (); if (data == null) { this.Clear(); return; } // byte[] bs = Tools.Decompress (data); // float[] da = new float[bs.Length / 4]; // for (var i = 0; i < da.Length; i++) // { // da [i] = BitConverter.ToSingle (bs, i * 4); // } // ac.clip = AudioClip.Create ("clip", da.Length, 1, frequency, false); // ac.clip.SetData (da, 0); ac.clip = USpeakAudioClipCompressor.DecompressAudioClip(data, 0, 1, false, 1.0f); ac.mute = false; ac.Play(); //倒计时 清理语音 clearTimer = TimerManager.inst.Add(ac.clip.length + 1, 1, (float time) => { if (isClear) { return; } this.onEnd(this.id); this.Clear(); }); }
public ulong PlaySound(byte[] data) { List <AudioClip> clipList = new List <AudioClip>(); int offset = 0; while (offset < data.Length) { int len = System.BitConverter.ToInt32(data, offset); byte[] frame = new byte[len + 6]; System.Array.Copy(data, offset, frame, 0, frame.Length); USpeakFrameContainer cont = default(USpeakFrameContainer); cont.LoadFrom(frame); AudioClip clip = USpeakAudioClipCompressor.DecompressAudioClip(cont.encodedData, (int)cont.Samples, 1, false, BandMode.Narrow, 1); //GetComponent<AudioSource>().clip = clip; //GetComponent<AudioSource>().Play(); clipList.Add(clip); offset += frame.Length; } ulong delay = 0; ulong totalLength = 0; foreach (AudioClip clip in clipList) { USpeakAudioManager.PlayClipAtPoint(clip, transform.position, delay, false); delay += (uint)((44100.0f / (float)8000) * ((uint)clip.samples)); totalLength += (uint)clip.samples; } return((ulong)(totalLength * (float)8000 / 44100.0f) / 1000); }