首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UnityEngine.Microphone.devices.Length =0

UnityEngine.Microphone.devices.Length =0
EN

Stack Overflow用户
提问于 2021-02-09 23:03:12
回答 1查看 326关注 0票数 0

在我的统一应用程序中录制声音时,我遇到了一个问题。在我启动我的应用程序之后,我就点击了一个按钮,这个按钮叫做协同线:

代码语言:javascript
复制
private IEnumerator coroutine_recording()
    {
        // Check if a microphone is present on the device
        if (UnityEngine.Microphone.devices.Length > 0)
        {
            //Check if user already gave authorization for using the microphone
            if (Application.HasUserAuthorization(UserAuthorization.Microphone) == false)
            {
                //Requets Authorization to use microphone
                yield return Application.RequestUserAuthorization(UserAuthorization.Microphone);

                while (Application.isFocused == false)
                    yield return new WaitForSeconds(0.1f);

                //Wait a bit so the device process the authorization and next step will not throw an error
                yield return new WaitForSeconds(1.0f);
            }
    }

在调试时,如果调用太早,则device.length似乎等于零。设置授权时,不再存在错误。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-09 23:03:12

解决方案是在代码开始时添加一个延迟,等待设备数量超过零。因此,在协同线的开头,我添加了以下内容:

代码语言:javascript
复制
//Wait until a microphone can be found
  while(UnityEngine.Microphone.devices.Length == 0)
  yield return new WaitForSeconds(0.1f);

这将确保您可以使用麦克风。

请注意,如果设备真的没有麦克风,请添加一个保护,但这在这里帮助了我。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66128536

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档