Unity 内置的录音和回放

Unity 内置的录音和回放_第1张图片1,将脚本挂载到实例上,添加上按钮事件。

Unity 内置的录音和回放_第2张图片

2,点击开始录音,讲话,然后点击播放录音,就能够听到自己说的话了。

AudioRecorder.cs

using UnityEngine;

public class AudioRecorder : MonoBehaviour
{
    private bool isRecording = false;
    private AudioClip recordedClip;

    // Start recording audio input
    public void StartRecording()
    {
        if (!isRecording)
        {
            // Start recording for 2 seconds at 44.1kHz sample rate
            recordedClip = Microphone.Start(null, false, 2, 44100);
            isRecording = true;
        }
    }

    // Stop recording audio input and play the recorded clip
    public void PlayRecordedClip()
    {
        if (isRecording)
        {
            // Wait for recording to finish
            while (Microphone.IsRecording(null)) { }

            // Stop recording and save the recorded clip
            Microphone.End(null);

            isRecording = false;

            // Play the recorded clip
            AudioSource audioSource = gameObject.AddComponent();
            audioSource.clip = recordedClip;
            audioSource.Play();
        }
    }
}

你可能感兴趣的:(GamePlay,unity,游戏引擎)