Unity调取移动端的麦克风进行录音并播放
人气:11.对MicroPhone类的理解
对麦克风的调用在Unity里主要是用到了MicroPhone这个类,此类里面有几个方法可以方便我们实现功能
2.代码演示
#region 模块信息 // ********************************************************************** // Copyright (C) 2018 Blazors // Please contact me if you have any questions // File Name: VoiceChat // Author: romantic123fly // WeChat||QQ: at853394528 || 853394528 // ********************************************************************** #endregion using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; //此脚本须挂在录音按钮上 public class Record : MonoBehaviour, IPointerDownHandler, IPointerUpHandler { float tirecordingTimemer = 0;//录音时长限制 public AudioSource aud;//存储声音 public Text ShowTimeHint;//剩余时间的文字提示 public void OnPointerDown(PointerEventData eventData) { Debug.Log("Start"); StartCoroutine("KeepTime"); aud.clip = Microphone.Start("Built-in Microphone", false, 60, 44100); } public void OnPointerUp(PointerEventData eventData) { Microphone.End("Built-in Microphone"); StopCoroutine("KeepTime"); Debug.Log("Over"); aud.Play(); } //此处开携程也行,用while也可以,放在updata里也没问题 IEnumerator KeepTime() { for (tirecordingTimemer = 10; tirecordingTimemer >= 0; tirecordingTimemer -= Time.deltaTime) { if (tirecordingTimemer <= 10) { ShowTimeHint.text = "你还可以录 " + (int)tirecordingTimemer + " 秒"; if (tirecordingTimemer < 1) { ShowTimeHint.text = "时间到"; Microphone.End("Built-in Microphone"); } } yield return 0; } } }
对应的ui组件挂靠一下直接运行工程就好了
3.运行结果
您可能感兴趣的文章:
加载全部内容