MicroPhoneM

2017-11-24  本文已影响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), "正在录音中");

        }
    }
}

}

上一篇 下一篇

猜你喜欢

热点阅读