unity3d录音播放

直接挂载到组件上面,打包apk测试即可

using UnityEngine;

    /// 
    /// 录音播放
    /// 
public class MicroPhoneManager : MonoBehaviour
{
    /// 
    /// 设备长度
    /// 
    public int DeviceLength;
    /// 
    /// 录音频率
    /// 
    public string Frequency = "5000";
    /// 
    /// 录音采样率
    /// 
    public int Samplerate = 44100;
    public int MicSecond = 2;
    string infoLog = "";

    AudioSource _curAudioSource;

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

    #region [public Way]

    /// 
    /// 获取麦克风设备
    /// 
    public void GetMicrophoneDevice()
    {
        string[] mDevice = Microphone.devices;
        DeviceLength = mDevice.Length;
        if (DeviceLength == 0)
            ShowInfoLog("找不到麦克风设备!");
    }

    /// 
    /// 开始录音
    /// 
    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("开始录音.....");
    }

    /// 
    /// 停止录音
    /// 
    public void StopRecordAudio()
    {
        ShowInfoLog("结束录音.....");
        if (!Microphone.IsRecording(null))
            return;
        Microphone.End(null);
        CurAudioSource.Stop();

    }

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

    /// 
    ///  打印录音信息
    /// 
    public void PrintRecordData()
    {
        if (Microphone.IsRecording(null))
            return;
        byte[] data = GetClipData();
        string infoLog = "total length:" + data.Length + " time:" + CurAudioSource.time;
        ShowInfoLog(infoLog);
    }

    /// 
    /// 获取音频数据
    /// 
    /// The clip data.
    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("获取音频数据失败!");
            return null;
        }
        return outData;
    }

    #endregion


    void OnGUI()
    {

        if (DeviceLength == 0)
        {
            if (ShowGUIButton("获取麦克风设备"))
            {
                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("开始录音"))
            {
                StartRecordAudio();
            }
            if (ShowGUIButton("结束录音"))
            {
                StopRecordAudio();
            }
            if (ShowGUIButton("回放录音"))
            {
                PlayRecordAudio();
            }
            if (ShowGUIButton("获取录音数据"))
            {
                PrintRecordData();
            }

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

    }

    #region [Private Way]

    /// 
    /// 显示GUI 按钮
    /// 
    /// true, if GUI button was shown, false otherwise.
    /// Button name.
    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

}

你可能感兴趣的:(unity3d录音播放)