在我的统一应用程序中录制声音时,我遇到了一个问题。在我启动我的应用程序之后,我就点击了一个按钮,这个按钮叫做协同线:
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似乎等于零。设置授权时,不再存在错误。
发布于 2021-02-09 23:03:12
解决方案是在代码开始时添加一个延迟,等待设备数量超过零。因此,在协同线的开头,我添加了以下内容:
//Wait until a microphone can be found
while(UnityEngine.Microphone.devices.Length == 0)
yield return new WaitForSeconds(0.1f);这将确保您可以使用麦克风。
请注意,如果设备真的没有麦克风,请添加一个保护,但这在这里帮助了我。
https://stackoverflow.com/questions/66128536
复制相似问题