您当前的位置: 首页 >  unity

呆呆敲代码的小Y

暂无认证

  • 12浏览

    0关注

    385博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【100个 Unity实用技能】| Unity中检测 设备麦克风权限

呆呆敲代码的小Y 发布时间:2022-10-16 12:11:54 ,浏览量:12

请添加图片描述

Unity 小科普

老规矩,先介绍一下 Unity 的科普小知识:

  • Unity是 实时3D互动内容创作和运营平台 。
  • 包括游戏开发、美术、建筑、汽车设计、影视在内的所有创作者,借助 Unity 将创意变成现实。
  • Unity 平台提供一整套完善的软件解决方案,可用于创作、运营和变现任何实时互动的2D和3D内容,支持平台包括手机、平板电脑、PC、游戏主机、增强现实和虚拟现实设备。
  • 也可以简单把 Unity 理解为一个游戏引擎,可以用来专业制作游戏!
  • 🎬 博客主页:https://xiaoy.blog.csdn.net

  • 🎥 本文由 呆呆敲代码的小Y 原创,首发于 CSDN🙉

  • 🎄 学习专栏推荐:Unity系统学习专栏

  • 🌲 游戏制作专栏推荐:游戏制作

  • 🌲Unity实战100例专栏推荐:Unity 实战100例 教程

  • 🏅 欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正!

  • 📆 未来很长,值得我们全力奔赴更美好的生活✨

  • ------------------❤️分割线❤️-------------------------

请添加图片描述

Unity 实用小技能学习 Unity中 检测当前设备是否有麦克风权限

在Unity中可以通过调用API检测可以从devices属性中获得已连接麦克风的列表 从而 判断有没有麦克风权限

Microphone 在这里插入图片描述

方法描述End停止记录。GetDeviceCaps获取设备的频率能力。GetPosition获取录音样本中的位置。IsRecording查询设备是否正在记录。Start开始记录设备。

具体使用示例:

        //获取麦克风设备,判断设备是否有麦克风
        string[] devices = Microphone.devices;
        if (devices.Length > 0)
        {
            Debug.Log("设备有麦克风:" + devices[0]);
        }
        else
        {
            Debug.Log("设备没有麦克风");
        }

记录麦克风播放的声音API如下:

        //
        // 摘要:
        //     Start Recording with device.
        //
        // 参数:
        //   deviceName:
        //     The name of the device.
        //
        //   loop:
        //     Indicates whether the recording should continue recording if lengthSec is reached,
        //     and wrap around and record from the beginning of the AudioClip.
        //
        //   lengthSec:
        //     Is the length of the AudioClip produced by the recording.
        //
        //   frequency:
        //     The sample rate of the AudioClip produced by the recording.
        //
        // 返回结果:
        //     The function returns null if the recording fails to start.
        public static AudioClip Start(string deviceName, bool loop, int lengthSec, int frequency);

如果有麦克风权限则可以直接使用AudioSource播放,将脚本挂载到场景中并添加一个AudioSource组件拖到脚本上即可!

代码如下:

    public AudioSource audioSource;
    private void OnClick()
    {
        audioSource.clip = Microphone.Start(null, true, 10, 44100);
        audioSource.Play();
    }

在这里插入图片描述

关注
打赏
1663854676
查看更多评论
立即登录/注册

微信扫码登录

0.0884s