Unity实现手机录音功能


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()方法 打包到手机即可

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM