Unity調(diào)取移動(dòng)端的麥克風(fēng)進(jìn)行錄音并播放
本文實(shí)例為大家分享了Unity調(diào)取移動(dòng)端的麥克風(fēng)進(jìn)行錄音并播放的具體代碼,供大家參考,具體內(nèi)容如下
1.對(duì)MicroPhone類(lèi)的理解
對(duì)麥克風(fēng)的調(diào)用在Unity里主要是用到了MicroPhone這個(gè)類(lèi),此類(lèi)里面有幾個(gè)方法可以方便我們實(shí)現(xiàn)功能

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;//錄音時(shí)長(zhǎng)限制
public AudioSource aud;//存儲(chǔ)聲音
public Text ShowTimeHint;//剩余時(shí)間的文字提示
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();
}
//此處開(kāi)攜程也行,用while也可以,放在updata里也沒(méi)問(wèn)題
IEnumerator KeepTime()
{
for (tirecordingTimemer = 10; tirecordingTimemer >= 0; tirecordingTimemer -= Time.deltaTime)
{
if (tirecordingTimemer <= 10)
{
ShowTimeHint.text = "你還可以錄 " + (int)tirecordingTimemer + " 秒";
if (tirecordingTimemer < 1)
{
ShowTimeHint.text = "時(shí)間到";
Microphone.End("Built-in Microphone");
}
}
yield return 0;
}
}
}
對(duì)應(yīng)的ui組件掛靠一下直接運(yùn)行工程就好了
3.運(yùn)行結(jié)果


以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
jQuery結(jié)合C#實(shí)現(xiàn)上傳文件的方法
這篇文章主要介紹了jQuery結(jié)合C#實(shí)現(xiàn)上傳文件的方法,涉及C#文件上傳的相關(guān)技巧,需要的朋友可以參考下2015-04-04
C#實(shí)現(xiàn)炫酷啟動(dòng)圖-動(dòng)態(tài)進(jìn)度條效果
這篇文章主要介紹了基于C#實(shí)現(xiàn)炫酷啟動(dòng)圖-動(dòng)態(tài)進(jìn)度條 效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05
C#使用FolderBrowserDialog類(lèi)實(shí)現(xiàn)選擇打開(kāi)文件夾方法詳解
這篇文章主要介紹了C#選擇文件夾/打開(kāi)文件夾/瀏覽文件夾等代碼方法,大家參考使用2013-11-11

