经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 软件/图像 » unity » 查看文章
Unity调取移动端的麦克风进行录音并播放
来源:jb51  时间:2019/6/19 13:11:41  对本文有异议

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

1.对MicroPhone类的理解

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

2.代码演示

  1. #region 模块信息
  2. // **********************************************************************
  3. // Copyright (C) 2018 Blazors
  4. // Please contact me if you have any questions
  5. // File Name: VoiceChat
  6. // Author: romantic123fly
  7. // WeChat||QQ: at853394528 || 853394528
  8. // **********************************************************************
  9. #endregion
  10. using System.Collections;
  11. using System.Collections.Generic;
  12. using UnityEngine;
  13. using UnityEngine.EventSystems;
  14. using UnityEngine.UI;
  15. //此脚本须挂在录音按钮上
  16. public class Record : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
  17. {
  18. float tirecordingTimemer = 0;//录音时长限制
  19. public AudioSource aud;//存储声音
  20. public Text ShowTimeHint;//剩余时间的文字提示
  21. public void OnPointerDown(PointerEventData eventData)
  22. {
  23. Debug.Log("Start");
  24. StartCoroutine("KeepTime");
  25. aud.clip = Microphone.Start("Built-in Microphone", false, 60, 44100);
  26. }
  27. public void OnPointerUp(PointerEventData eventData)
  28. {
  29. Microphone.End("Built-in Microphone");
  30. StopCoroutine("KeepTime");
  31. Debug.Log("Over");
  32. aud.Play();
  33. }
  34. //此处开携程也行,用while也可以,放在updata里也没问题
  35. IEnumerator KeepTime()
  36. {
  37. for (tirecordingTimemer = 10; tirecordingTimemer >= 0; tirecordingTimemer -= Time.deltaTime)
  38. {
  39. if (tirecordingTimemer <= 10)
  40. {
  41. ShowTimeHint.text = "你还可以录 " + (int)tirecordingTimemer + " 秒";
  42. if (tirecordingTimemer < 1)
  43. {
  44. ShowTimeHint.text = "时间到";
  45. Microphone.End("Built-in Microphone");
  46. }
  47. }
  48. yield return 0;
  49. }
  50. }
  51. }

对应的ui组件挂靠一下直接运行工程就好了

3.运行结果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号