Unity使用Cscore录制Window麦克风声音

资料

Github cscore

注意

笔记本可直接使用麦克风,台式电脑可能需要耳机辅助
Api Levle 设置为.Net 4.x

测试脚本

using UnityEngine;
using CSCore.SoundIn;
using CSCore.Codecs.WAV;
public class Recorder : MonoBehaviour
{
    
    
    private WasapiCapture capture;
    private WaveWriter writer;

    private void OnGUI()
    {
    
    
        if (GUILayout.Button("recorder"))
            startRecording();

        if (GUILayout.Button("stop"))
            stopRecording();
    }
    private void startRecording()
    {
    
    
        capture = new WasapiCapture();
        capture.Initialize();
        writer = new WaveWriter("file.wav", capture.WaveFormat);//声音保存为wav格式
        capture.DataAvailable += (s, capData) =>
        {
    
    
            writer.Write(capData.Data, capData.Offset, capData.ByteCount);
        };//录制回调
        capture.Start();//开始录制
    }

    private void stopRecording()
    {
    
    
        if (writer != null && capture != null)
        {
    
    
            capture.Stop();
            writer.Dispose();
            capture.Dispose();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43796392/article/details/131524352