Unity原生实现录音功能

版权声明:本文为Jumbo原创文章,采用[知识共享 署名-非商业性使用-禁止演绎 4.0 国际 许可协议],转载前请保证理解此协议
原文出处:https://www.jianshu.com/p/fd9f8594b371

声音直接转成byte[]数据,方便使用

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();
    }
    
    //获取声音Byte数组数据
    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;
    }
 
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,779评论 18 399
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,993评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,595评论 25 708
  • 11.15日精进:工作中相互配合才能更好的服务客户,今天可谓是笑料百出,虽然很多家人出去学习了,但是工作起来还是很...
    王佳欢雪阅读 141评论 0 1
  • 2016过去了,大家都很怀念它,又到了回头望的时候。 一年匆匆流过,回想起来,有没有一个人、一件事,让你怀有深深的...
    菲乐阅读 287评论 0 2