这篇文章主要为大家详细介绍了Unity调取移动端的麦克风进行录音并播放,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了Unity调取移动端的麦克风进行录音并播放的具体代码,供大家参考,具体内容如下
1.对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.运行结果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
织梦狗教程
本文标题为:Unity调取移动端的麦克风进行录音并播放


基础教程推荐
猜你喜欢
- C#调用摄像头实现拍照功能的示例代码 2023-03-09
- C#中的Linq to JSON操作详解 2023-06-08
- C# 解析XML和反序列化的示例 2023-04-14
- 实例详解C#实现http不同方法的请求 2022-12-26
- Unity shader实现高斯模糊效果 2023-01-16
- C#获取指定目录下某种格式文件集并备份到指定文件夹 2023-05-30
- C#中 Json 序列化去掉null值的方法 2022-11-18
- Unity 如何获取鼠标停留位置下的物体 2023-04-10
- C#通过标签软件Bartender的ZPL命令打印条码 2023-05-16
- c# – USING块在网站与Windows窗体中的行为不同 2023-09-20