Unity获取麦克风录音的方法

话不多说直接上代码

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
 
public class MicphoneTest : MonoBehaviour
{
    AudioSource _audio;
    AudioSource audio
    {
        get
        {
            if (_audio == null)
            {
                _audio = gameObject.AddComponent<AudioSource>();
            }
            return _audio;
        }
    }
 
    void Start()
    {
        string[] ms = Microphone.devices;
        deviceCount = ms.Length;
        if (deviceCount == 0)
        {
            Log("no microphone found");
        }
    }
 
    string sLog = "";
    void Log(string log)
    {
        sLog += log;
        sLog += "\r\n";
    }
    int deviceCount;
    string sFrequency = "10000";
    void OnGUI()
    {
        if (deviceCount > 0)
        {
            GUILayout.BeginHorizontal();
            if (!Microphone.IsRecording(null) && GUILayout.Button("Start", GUILayout.Height(Screen.height / 20), GUILayout.Width(Screen.width / 5)))
            {
                StartRecord();
            }
            if (Microphone.IsRecording(null) && GUILayout.Button("Stop", GUILayout.Height(Screen.height / 20), GUILayout.Width(Screen.width / 5)))
            {
                StopRecord();
            }
            if (!Microphone.IsRecording(null) && GUILayout.Button("Play", GUILayout.Height(Screen.height / 20), GUILayout.Width(Screen.width / 5)))
            {
                PlayRecord();
            }
            if (!Microphone.IsRecording(null) && GUILayout.Button("Print", GUILayout.Height(Screen.height / 20), GUILayout.Width(Screen.width / 5)))
            {
                PrintRecord();
            }
            sFrequency = GUILayout.TextField(sFrequency, GUILayout.Width(Screen.width / 5), GUILayout.Height(Screen.height / 20));
            GUILayout.EndHorizontal();
        }
        GUILayout.Label(sLog);
    }
    void StartRecord()
    {
        audio.Stop();
        audio.loop = false;
        audio.mute = true;
        audio.clip = Microphone.Start(null, false, 1, int.Parse(sFrequency));    
        while (!(Microphone.GetPosition(null) > 0))
        {
        }
        audio.Play();
        Log("StartRecord");
    }
    void StopRecord()
    {
        if (!Microphone.IsRecording(null))
        {
            return;
        }
        Microphone.End(null);
        audio.Stop();
    }
    void PrintRecord()
    {
        if (Microphone.IsRecording(null))
        {
            return;
        }
        byte[] data = GetClipData();
        string slog = "total length:" + data.Length + " time:" + audio.time;
        Log(slog);
    }
    void PlayRecord()
    {
        if (Microphone.IsRecording(null))
        {
            return;
        }
        if (audio.clip == null)
        {
            return;
        }
        audio.mute = false;
        audio.loop = false;
        audio.Play();
    }
    public byte[] GetClipData()
    {
        if (audio.clip == null)
        {
            Debug.Log("GetClipData audio.clip is null");
            return null;
        }
 
        float[] samples = new float[audio.clip.samples];
 
        audio.clip.GetData(samples, 0);
 
 
        byte[] outData = new byte[samples.Length * 2];
 
        int rescaleFactor = 32767;
 
        for (int i = 0; i < samples.Length; i++)
        {
            short temshort = (short)(samples[i] * rescaleFactor);
 
            byte[] temdata = System.BitConverter.GetBytes(temshort);
 
            outData[i * 2] = temdata[0];
            outData[i * 2 + 1] = temdata[1];
 
 
        }
        if (outData == null || outData.Length <= 0)
        {
            Debug.Log("GetClipData intData is null");
            return null;
        }
        return outData;
    }
 
}
接口都是留好的,要修改的话调用一下就可以。

声音已经导出成了byte数据,想存想传,实现一下就好,Good Luck!

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值