[英]UnityWebRequest is not completed after selecting a file from Open File Panel in Unity
string path;
AudioSource audio = GetComponent<AudioSource>();
path = EditorUtility.OpenFilePanel("Audio Files", "", "wav");
if (path != null)
{
using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip("file:///" + path, AudioType.WAV))
if (www.result == UnityWebRequest.Result.ConnectionError)
{
Debug.LogError(www.error);
}
else
{
//Debug.Log(www.url);
audio.clip = DownloadHandlerAudioClip.GetContent(www);
audio.Play();
yield return www.SendWebRequest();
}
}
after executing this block of code, on Play mode, A file picker runs successfully, however after selecting an audio file, the console throws the InvalidOperationException: Cannot get content from an unfinished UnityWebRequest object which caused by this line执行此代码块后,在播放模式下,文件选择器成功运行,但是在选择音频文件后,控制台抛出 InvalidOperationException: Cannot get content from an unfinished UnityWebRequest object 这是由该行引起的
audio.clip = DownloadHandlerAudioClip.GetContent(www);
my assumption is that I somehow missing a step in between getting the audio file path from the file picker and stream the actual audio clip using the path.我的假设是,我在从文件选择器获取音频文件路径和 stream 使用该路径的实际音频剪辑之间以某种方式错过了一步。
Debug.Log(www.url) will successfully print the file URI scheme. Debug.Log(www.url) 将成功打印文件 URI 方案。
In general watch out with your using
.. you should wrap it in { }
for readability and for preventing unexpected behavior一般来说,注意你的
using
.. 你应该将它包装在{ }
中以提高可读性并防止意外行为
Then for some reason you do然后由于某种原因你这样做
yield return www.SendWebRequest();
after trying to access the results.... this line should be the first after the using
line.. before trying to check whether the request was sent correctly and trying to access the download content在尝试访问结果之后.. 此行应该是
using
行之后的第一行.. 在尝试检查请求是否正确发送并尝试访问下载内容之前
using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip("file:///" + path, AudioType.WAV))
{
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.ConnectionError)
{
Debug.LogError(www.error);
}
else
{
audio.clip = DownloadHandlerAudioClip.GetContent(www);
audio.Play();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.