VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • Unity调取移动端的麦克风进行录音并播放

本文实例为大家分享了Unity调取移动端的麦克风进行录音并播放的具体代码,供大家参考,具体内容如下

1.对MicroPhone类的理解

对麦克风的调用在Unity里主要是用到了MicroPhone这个类,此类里面有几个方法可以方便我们实现功能

2.代码演示

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#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.运行结果


相关教程