本文实例为大家分享了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.运行结果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_37310110/article/details/79712298