美文网首页
MicroPhoneM

MicroPhoneM

作者: 萧非子 | 来源:发表于2017-11-24 16:01 被阅读15次

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MicroPhoneM : MonoBehaviour {
bool micConnected = false;//麦克风链接
int minFreq, maxFreq;//频率
AudioSource goAudioSource;
void Start () {

    //查询机器上有没有麦克风
    if (Microphone.devices.Length <= 0)
    {
        Debug.LogWarning("没有麦克风");
    }
    else {
        micConnected = true;
        Debug.Log("麦克风准备就绪");
        Microphone.GetDeviceCaps(null,out minFreq,out maxFreq);//麦克风属性
        if (minFreq==0&&maxFreq==0)
        {
            maxFreq = 44100;
        }
        goAudioSource = GetComponent<AudioSource>();
    }
}

// Update is called once per frame
void Update () {
    
}
private void OnGUI()
{
    if (micConnected)
    {
        if (!Microphone.IsRecording(null))
        {
            if (GUI.Button(new Rect(Screen.width/2-100,Screen.height/2-25,200,50),"开始录音"))
            {
                goAudioSource.clip = Microphone.Start(null,true,20,maxFreq);
            }
        }
        else
        {
            if (GUI.Button(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 25, 200, 50), "停止录音并播放"))
            {
                Microphone.End(null);

                SaveWav.Save("mywave.wav",goAudioSource.clip);
                goAudioSource.Play();
            }
            GUI.Label(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 25, 200, 50), "正在录音中");

        }
    }
}

}

相关文章

  • MicroPhoneM

    using System.Collections;using System.Collections.Generic...

网友评论

      本文标题:MicroPhoneM

      本文链接:https://www.haomeiwen.com/subject/hbrfbxtx.html