查看: 3190|回复: 0
打印 上一主题 下一主题

[其他] Unity原生实现录音功能(转载)

[复制链接]
may    

8830

主题

81

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
52344
精华
343

最佳新人 热心会员 灌水之王 活跃会员 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2016-3-14 01:36:05 |只看该作者 |倒序浏览
国际惯例,直接上代码
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;

  4. public class MicphoneTest : MonoBehaviour
  5. {
  6.     AudioSource _audio;
  7.     AudioSource audio
  8.     {
  9.         get
  10.         {
  11.             if (_audio == null)
  12.             {
  13.                 _audio = gameObject.AddComponent<AudioSource>();
  14.             }
  15.             return _audio;
  16.         }
  17.     }

  18.     void Start()
  19.     {
  20.         string[] ms = Microphone.devices;
  21.         deviceCount = ms.Length;
  22.         if (deviceCount == 0)
  23.         {
  24.             Log("no microphone found");
  25.         }
  26.     }

  27.     string sLog = "";
  28.     void Log(string log)
  29.     {
  30.         sLog += log;
  31.         sLog += "\r\n";
  32.     }
  33.     int deviceCount;
  34.     string sFrequency = "10000";
  35.     void OnGUI()
  36.     {
  37.         if (deviceCount > 0)
  38.         {
  39.             GUILayout.BeginHorizontal();
  40.             if (!Microphone.IsRecording(null) && GUILayout.Button("Start", GUILayout.Height(Screen.height / 20), GUILayout.Width(Screen.width / 5)))
  41.             {
  42.                 StartRecord();
  43.             }
  44.             if (Microphone.IsRecording(null) && GUILayout.Button("Stop", GUILayout.Height(Screen.height / 20), GUILayout.Width(Screen.width / 5)))
  45.             {
  46.                 StopRecord();
  47.             }
  48.             if (!Microphone.IsRecording(null) && GUILayout.Button("Play", GUILayout.Height(Screen.height / 20), GUILayout.Width(Screen.width / 5)))
  49.             {
  50.                 PlayRecord();
  51.             }
  52.             if (!Microphone.IsRecording(null) && GUILayout.Button("Print", GUILayout.Height(Screen.height / 20), GUILayout.Width(Screen.width / 5)))
  53.             {
  54.                 PrintRecord();
  55.             }
  56.             sFrequency = GUILayout.TextField(sFrequency, GUILayout.Width(Screen.width / 5), GUILayout.Height(Screen.height / 20));
  57.             GUILayout.EndHorizontal();
  58.         }
  59.         GUILayout.Label(sLog);
  60.     }
  61.     void StartRecord()
  62.     {
  63.         audio.Stop();
  64.         audio.loop = false;
  65.         audio.mute = true;
  66.         audio.clip = Microphone.Start(null, false, 1, int.Parse(sFrequency));   
  67.         while (!(Microphone.GetPosition(null) > 0))
  68.         {
  69.         }
  70.         audio.Play();
  71.         Log("StartRecord");
  72.     }
  73.     void StopRecord()
  74.     {
  75.         if (!Microphone.IsRecording(null))
  76.         {
  77.             return;
  78.         }
  79.         Microphone.End(null);
  80.         audio.Stop();
  81.     }
  82.     void PrintRecord()
  83.     {
  84.         if (Microphone.IsRecording(null))
  85.         {
  86.             return;
  87.         }
  88.         byte[] data = GetClipData();
  89.         string slog = "total length:" + data.Length + " time:" + audio.time;
  90.         Log(slog);
  91.     }
  92.     void PlayRecord()
  93.     {
  94.         if (Microphone.IsRecording(null))
  95.         {
  96.             return;
  97.         }
  98.         if (audio.clip == null)
  99.         {
  100.             return;
  101.         }
  102.         audio.mute = false;
  103.         audio.loop = false;
  104.         audio.Play();
  105.     }
  106.     public byte[] GetClipData()
  107.     {
  108.         if (audio.clip == null)
  109.         {
  110.             Debug.Log("GetClipData audio.clip is null");
  111.             return null;
  112.         }

  113.         float[] samples = new float[audio.clip.samples];

  114.         audio.clip.GetData(samples, 0);


  115.         byte[] outData = new byte[samples.Length * 2];

  116.         int rescaleFactor = 32767;

  117.         for (int i = 0; i < samples.Length; i++)
  118.         {
  119.             short temshort = (short)(samples[i] * rescaleFactor);

  120.             byte[] temdata = System.BitConverter.GetBytes(temshort);

  121.             outData[i * 2] = temdata[0];
  122.             outData[i * 2 + 1] = temdata[1];


  123.         }
  124.         if (outData == null || outData.Length <= 0)
  125.         {
  126.             Debug.Log("GetClipData intData is null");
  127.             return null;
  128.         }
  129.         return outData;
  130.     }

  131. }
复制代码
接口都是留好的,要修改的话调用一下就可以。

声音已经导出成了byte数据,想存想传,实现一下就好,Enjoy~
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-7-19 05:31 , Processed in 0.307591 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部