using UnityEngine;
using System.Collections;

public class MicPhoneScripts : MonoBehaviour
{
    private AudioSource audioSource;
    AudioClip clip;
    void Awake()
    {
        audioSource = GetComponent<AudioSource>();
        
    }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            StartRecord();

        }
        if (Input.GetKeyUp(KeyCode.Space))
        {
            StopRecord();
        }
    }

    /// <summary>
    /// 开始录音
    /// </summary>
  public   void StartRecord()
    {
        Microphone.End(null);
        clip = Microphone.Start(null, false, 20, 8000);
    }
    /// <summary>
    /// 结束录音
    /// </summary>
  public   void StopRecord()
    {
        if (Microphone.IsRecording(null))
        {
            Microphone.End(null);
            audioSource.clip = clip;
            audioSource.Play();
        }
    }
}

注:UI中绑定StartRecord()和StopRecord()方法 打包到手机即可

 

相关文章:

  • 2022-03-13
  • 2021-12-28
  • 2022-12-23
  • 2022-12-23
  • 2021-12-22
  • 2021-11-14
  • 2021-12-28
  • 2021-04-24
猜你喜欢
  • 2021-08-30
  • 2021-04-13
  • 2021-12-27
  • 2021-12-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案