最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

unity實(shí)現(xiàn)錄音并保存本地

 更新時(shí)間:2020年04月16日 11:10:29   作者:貪玩的孩紙時(shí)代  
這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)錄音并保存本地,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了unity實(shí)現(xiàn)錄音并保存本地的具體代碼,供大家參考,具體內(nèi)容如下

我們可以使用unity自帶的MicroPhone類(lèi)來(lái)錄音,回放錄音,保存錄音

具體代碼如下:

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class MicroPhoneManager : MonoBehaviour {

  public int DeviceLength;
  /// <summary>
  /// 錄音頻率
  /// </summary>
  public string Frequency = "44100";
  public int Samplerate = 44100;
  /// <summary>
  /// 錄音時(shí)長(zhǎng)
  /// </summary>
  public int MicSecond = 2;
  string infoLog = "";

  AudioSource _curAudioSource;

  AudioSource CurAudioSource
  {
    get
    {
      if (_curAudioSource == null)
      {
        _curAudioSource = gameObject.AddComponent<AudioSource>();
      }
      return _curAudioSource;
    }
  }

  #region [public Way]

  /// <summary>
  /// 獲取麥克風(fēng)設(shè)備
  /// </summary>
  public void GetMicrophoneDevice()
  {
    string[] mDevice = Microphone.devices;
    DeviceLength = mDevice.Length;
    if (DeviceLength == 0)
      ShowInfoLog("找不到麥克風(fēng)設(shè)備!");
  }

  /// <summary>
  /// 開(kāi)始錄音
  /// </summary>
  public void StartRecordAudio()
  {
    CurAudioSource.Stop();
    CurAudioSource.loop = false;
    CurAudioSource.mute = true;
    CurAudioSource.clip = Microphone.Start(null, true, MicSecond, int.Parse(Frequency));
    while (!(Microphone.GetPosition(null) > 0))
    {

    }
    CurAudioSource.Play();
    ShowInfoLog("開(kāi)始錄音.....");
  }

  /// <summary>
  /// 停止錄音
  /// </summary>
  public void StopRecordAudio()
  {
    ShowInfoLog("結(jié)束錄音.....");
    if (!Microphone.IsRecording(null))
      return;
    Microphone.End(null);
    CurAudioSource.Stop();

  }
  /// <summary>s
  /// 回放錄音
  /// </summary>
  public void PlayRecordAudio()
  {
    if (Microphone.IsRecording(null))
      return;
    if (CurAudioSource.clip == null)
      return;
    CurAudioSource.mute = false;
    CurAudioSource.loop = false;
    CurAudioSource.Play();
    ShowInfoLog("播放錄音.....");
  }

  /// <summary>
  /// 打印錄音信息
  /// </summary>
  public void PrintRecordData()
  {
    if (Microphone.IsRecording(null))
      return;
    byte[] data = GetClipData();
    #region 用戶(hù)自由固定錄音時(shí)長(zhǎng)
    int position = Microphone.GetPosition(null);
    var soundata = new float[CurAudioSource.clip.samples * CurAudioSource.clip.channels];
    CurAudioSource.clip.GetData(soundata, 0);

    var newdata = new float[position * CurAudioSource.clip.channels];
    for (int i = 0; i < newdata.Length; i++) {
      newdata[i] = soundata[i];
    }
    CurAudioSource.clip = AudioClip.Create(CurAudioSource.clip.name, position, CurAudioSource.clip.channels, CurAudioSource.clip.frequency, false);
    CurAudioSource.clip.SetData(newdata, 0);

    Microphone.End(null);
    #endregion
    using (FileStream fs = CreateEmpty(Application.persistentDataPath + "/dd.wav")) {
      ConvertAndWrite(fs, CurAudioSource.clip);
      WriteHeader(fs, CurAudioSource.clip);
    }

    string infoLog = "total length:" + data.Length + " time:" + CurAudioSource.time;
    ShowInfoLog(infoLog);
  }
  private void WriteHeader(FileStream stream, AudioClip clip)
  {
    int hz = clip.frequency;
    int channels = clip.channels;
    int samples = clip.samples;

    stream.Seek(0, SeekOrigin.Begin);

    Byte[] riff = System.Text.Encoding.UTF8.GetBytes("RIFF");
    stream.Write(riff, 0, 4);

    Byte[] chunkSize = BitConverter.GetBytes(stream.Length - 8);
    stream.Write(chunkSize, 0, 4);

    Byte[] wave = System.Text.Encoding.UTF8.GetBytes("WAVE");
    stream.Write(wave, 0, 4);

    Byte[] fmt = System.Text.Encoding.UTF8.GetBytes("fmt ");
    stream.Write(fmt, 0, 4);

    Byte[] subChunk1 = BitConverter.GetBytes(16);
    stream.Write(subChunk1, 0, 4);

    UInt16 two = 2;
    UInt16 one = 1;

    Byte[] audioFormat = BitConverter.GetBytes(one);
    stream.Write(audioFormat, 0, 2);

    Byte[] numChannels = BitConverter.GetBytes(channels);
    stream.Write(numChannels, 0, 2);

    Byte[] sampleRate = BitConverter.GetBytes(hz);
    stream.Write(sampleRate, 0, 4);

    Byte[] byteRate = BitConverter.GetBytes(hz * channels * 2); // sampleRate * bytesPerSample*number of channels, here 44100*2*2 
    stream.Write(byteRate, 0, 4);

    UInt16 blockAlign = (ushort)(channels * 2);
    stream.Write(BitConverter.GetBytes(blockAlign), 0, 2);

    UInt16 bps = 16;
    Byte[] bitsPerSample = BitConverter.GetBytes(bps);
    stream.Write(bitsPerSample, 0, 2);

    Byte[] datastring = System.Text.Encoding.UTF8.GetBytes("data");
    stream.Write(datastring, 0, 4);

    Byte[] subChunk2 = BitConverter.GetBytes(samples * channels * 2);
    stream.Write(subChunk2, 0, 4);

  }
  private FileStream CreateEmpty(string filepath)
  {
    FileStream fileStream = new FileStream(filepath, FileMode.Create);
    byte emptyByte = new byte();

    for (int i = 0; i < 44; i++) //preparing the header 
    {
      fileStream.WriteByte(emptyByte);
    }

    return fileStream;
  }
  private void ConvertAndWrite(FileStream fileStream, AudioClip clip)
  {

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

    clip.GetData(samples, 0);

    Int16[] intData = new Int16[samples.Length];

    Byte[] bytesData = new Byte[samples.Length * 2];

    int rescaleFactor = 32767; //to convert float to Int16 

    for (int i = 0; i < samples.Length; i++)
    {
      intData[i] = (short)(samples[i] * rescaleFactor);
      Byte[] byteArr = new Byte[2];
      byteArr = BitConverter.GetBytes(intData[i]);
      byteArr.CopyTo(bytesData, i * 2);
    }
    fileStream.Write(bytesData, 0, bytesData.Length);
  }
  /// <summary>
  /// 獲取音頻數(shù)據(jù)
  /// </summary>
  /// <returns>The clip data.</returns>
  public byte[] GetClipData()
  {
    if (CurAudioSource.clip == null)
    {
      ShowInfoLog("缺少音頻資源!");
      return null;
    }

    float[] samples = new float[CurAudioSource.clip.samples];
    CurAudioSource.clip.GetData(samples, 0);

    byte[] outData = new byte[samples.Length * 2];
    int reScaleFactor = 32767;

    for (int i = 0; i < samples.Length; i++)
    {
      short tempShort = (short)(samples[i] * reScaleFactor);
      byte[] tempData = System.BitConverter.GetBytes(tempShort);

      outData[i * 2] = tempData[0];
      outData[i * 2 + 1] = tempData[1];
    }
    if (outData == null || outData.Length <= 0)
    {

      ShowInfoLog("獲取音頻數(shù)據(jù)失?。?);
      return null;
    }
    return outData;
  }

  #endregion


  void OnGUI()
  {

    if (DeviceLength == 0)
    {
      if (ShowGUIButton("獲取麥克風(fēng)設(shè)備"))
      {
        GetMicrophoneDevice();
      }
    }
    else if (DeviceLength > 0)
    {
      GUILayout.Label("錄音頻率:");
      Frequency = GUILayout.TextField(Frequency, GUILayout.Width(Screen.width / 5), GUILayout.Height(Screen.height / 20));
      GUILayout.BeginVertical();

      if (ShowGUIButton("開(kāi)始錄音"))
      {
        StartRecordAudio();
      }
      if (ShowGUIButton("結(jié)束錄音"))
      {
        StopRecordAudio();
      }
      if (ShowGUIButton("回放錄音"))
      {
        PlayRecordAudio();
      }
      if (ShowGUIButton("獲取錄音數(shù)據(jù)"))
      {
        PrintRecordData();
      }

      GUILayout.EndVertical();
    }
    GUILayout.Label(infoLog);

  }

  #region [Private Way]

  /// <summary>
  /// 顯示GUI 按鈕
  /// </summary>
  /// <returns><c>true</c>, if GUI button was shown, <c>false</c> otherwise.</returns>
  /// <param name="buttonName">Button name.</param>
  bool ShowGUIButton(string buttonName)
  {
    return GUILayout.Button(buttonName, GUILayout.Height(Screen.height / 20), GUILayout.Width(Screen.width / 5));
  }

  void ShowInfoLog(string info)
  {
    infoLog += info;
    infoLog += "\r\n";
  }

  #endregion
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#創(chuàng)建自定義控件及添加自定義屬性和事件使用實(shí)例詳解

    C#創(chuàng)建自定義控件及添加自定義屬性和事件使用實(shí)例詳解

    這篇文章主要給大家介紹了關(guān)于C#創(chuàng)建自定義控件及添加自定義屬性和事件使用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • C#單例模式(Singleton Pattern)實(shí)例教程

    C#單例模式(Singleton Pattern)實(shí)例教程

    這篇文章主要介紹了C#單例模式(Singleton Pattern)的實(shí)現(xiàn)方法,主要講述了即時(shí)加載的單例模式、延遲加載的單例模式與線(xiàn)程安全的單例模式,需要的朋友可以參考下
    2014-09-09
  • jQuery結(jié)合C#實(shí)現(xiàn)上傳文件的方法

    jQuery結(jié)合C#實(shí)現(xiàn)上傳文件的方法

    這篇文章主要介紹了jQuery結(jié)合C#實(shí)現(xiàn)上傳文件的方法,涉及C#文件上傳的相關(guān)技巧,需要的朋友可以參考下
    2015-04-04
  • 對(duì)C#中public、private、protect的區(qū)別說(shuō)明

    對(duì)C#中public、private、protect的區(qū)別說(shuō)明

    這篇文章主要介紹了對(duì)C#中public、private、protect的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-04-04
  • C#實(shí)現(xiàn)單例模式的6種方法小結(jié)

    C#實(shí)現(xiàn)單例模式的6種方法小結(jié)

    這篇文章主要介紹了C#實(shí)現(xiàn)單例模式的6種方法,C#中實(shí)現(xiàn)單例有很多種方法,本文將按順序介紹非線(xiàn)程安全、完全懶漢式、線(xiàn)程安全和低/高性能集中版本,需要的朋友可以參考下
    2022-09-09
  • c#委托詳解和和示例分享

    c#委托詳解和和示例分享

    這篇文章詳細(xì)探討了C#中的委托,列舉其主要的實(shí)現(xiàn)方式,并分析其在設(shè)計(jì)層面和編碼層面帶來(lái)的好處,最后會(huì)討論其安全性和執(zhí)行效率等,當(dāng)然還有實(shí)現(xiàn)示例
    2014-03-03
  • C# 改變無(wú)邊框窗體尺寸大小的方法

    C# 改變無(wú)邊框窗體尺寸大小的方法

    這篇文章介紹了C# 改變無(wú)邊框窗體尺寸大小的方法,有需要的朋友可以參考一下
    2013-10-10
  • 基于WPF實(shí)現(xiàn)視頻封面查看器

    基于WPF實(shí)現(xiàn)視頻封面查看器

    這篇文章主要為大家詳細(xì)介紹了WPF如何實(shí)現(xiàn)視頻封面查看器,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,感興趣的小伙伴可以了解一下
    2022-11-11
  • 淺解關(guān)于C#多線(xiàn)程的介紹

    淺解關(guān)于C#多線(xiàn)程的介紹

    本篇文章小編將為大家介紹,淺解關(guān)于C#的多線(xiàn)程,有需要的朋友可以參考一下
    2013-04-04
  • C#簡(jiǎn)單生成隨機(jī)密碼的方法示例

    C#簡(jiǎn)單生成隨機(jī)密碼的方法示例

    這篇文章主要介紹了C#簡(jiǎn)單生成隨機(jī)密碼的方法,結(jié)合具體實(shí)例形式分析了C#生成隨機(jī)密碼操作的前臺(tái)界面與后臺(tái)處理技巧,需要的朋友可以參考下
    2017-06-06

最新評(píng)論

肇庆市| 明星| 色达县| 沁阳市| 广东省| 囊谦县| 柘城县| 越西县| 临高县| 清涧县| 宽甸| 互助| 宁强县| 德钦县| 思南县| 东平县| 长岛县| 香河县| 尼勒克县| 榆中县| 德钦县| 万安县| 宁乡县| 东明县| 海阳市| 盖州市| 绿春县| 贵德县| 寿光市| 库车县| 木兰县| 定结县| 璧山县| 无为县| 文山县| 洛阳市| 柞水县| 云霄县| 纳雍县| 苗栗市| 东乡县|