Exemplo n.º 1
0
    private void ReceiveAudio(byte[] bytes)
    {
        //反序列化
        M_AudioClip mac = (M_AudioClip)FormatterHelper.Deserialize(bytes);
        //生成新的声音片段
        AudioClip ac = AudioClip.Create("s", mac.length, 1, mac.frequency, false);

        //float数组转audioclip
        ac.SetData(mac.audioData, 0);
        //播放
        audioSource.clip = ac;
        audioSource.Play();
    }
Exemplo n.º 2
0
    public void OnPress(bool isPress)
    {
        if (isPress)//点击
        {
            messageLabel.text = "";
            Microphone.End(null);                                        //关闭录音.
            audioClip = Microphone.Start(null, false, 10, samplingRate); //开始录音
        }
        else//弹起按钮
        {
            int audioLength;//录音的长度,单位为秒,ui上可能需要显示
            int lastPos = Microphone.GetPosition(null);
            if (Microphone.IsRecording(null))
            {
                audioLength = lastPos / samplingRate;
            }
            else
            {
                audioLength = 10;
            }

            Microphone.End(null);//此时录音结束,audioClip可以播放

            if (audioLength < 1.0f)
            {
                messageLabel.text = "说话时间太短";
                return;
            } //录音小于1秒不处理

            //将声音转成float数组
            float[] samples = new float[audioLength * samplingRate];
            audioClip.GetData(samples, 0);
            int i = 0;
            while (i < samples.Length)
            {
                samples[i] = samples[i] * 0.5F;
                ++i;
            }

            //实例化M_AudioClip类 传入声音采样率 声道 音频 和float数组
            M_AudioClip mac = new M_AudioClip(audioLength * samplingRate, audioClip.frequency, samples);

            //向rpc发送M_AudioClip序列化后的byte数组
            nv.RPC("ReceiveAudio", RPCMode.Others, FormatterHelper.Serialize(mac));
        }
    }