unity www "aborted"错误
Unity www "aborted" error
我有以下错误
当我尝试使用 unity www class 下载 mp4 文件时,它失败了,在 www.error
中只有 "aborted" 错误消息
这是一个奇怪的错误,它只出现在某些设备上,我已经在 galaxy note 5 上尝试过并且运行良好,当有人在 galaxy s7 上尝试过时,他得到了那个错误
有谁知道发生了什么事吗?
感谢大家的帮助。
下载视频的代码
private IEnumerator DownloadVideo()
{
showDownloadProgress = true;
downloadProgress.gameObject.SetActive(true);
videoURL = MainPlayerCTRL.mediaURL;
Uri uri = new Uri(videoURL);
string filename = System.IO.Path.GetFileName(uri.LocalPath);
string localFilePath = Application.persistentDataPath + "/"+ filename;
bool tryVideoDownload = true;
if (!File.Exists(localFilePath))
{
while (tryVideoDownload)
{
downloadProgressText.text = "Downloading";
showDownloadProgress = true;
downloadProgress.gameObject.SetActive(true);
www = new WWW(videoURL);
yield return www;
if (String.IsNullOrEmpty(www.error))
{
byte[] bytes = www.bytes;
FileStream fs = new FileStream(localFilePath, FileMode.Create);
fs.Write(bytes, 0, bytes.Length);
downloadProgressText.text = "Download done!";
yield return new WaitForSeconds(2);
tryVideoDownload = false;
}//Video downloaded
else
{
showDownloadProgress = false;
downloadProgress.gameObject.SetActive(true);
downloadProgressText.text = "Download ERROR \n ";
downloadProgressText.text += www.error;
yield return new WaitForSeconds(2);
downloadProgressText.text = "Attempting to download again";
yield return new WaitForSeconds(2);
tryVideoDownload = true;
}
}
yield return new WaitForEndOfFrame();
}
}
我首先怀疑的是内存不足,这通常发生在使用 FileStream
复制大文件时,但您提到您从 www.error
得到了 "aborted" 错误。罪魁祸首可能是 WWW
,首先要做的是使用 UnityWebRequest
. I replaced WWW
in your function with UnityWebRequest
。
您需要在顶部包含 using UnityEngine.Networking;
才能使用它。另一件事是 yield return new WaitForEndOfFrame();
应该在 while 循环内部而不是在它外部并且使用 yield return null;
更好。
private IEnumerator DownloadVideo()
{
showDownloadProgress = true;
downloadProgress.gameObject.SetActive(true);
videoURL = MainPlayerCTRL.mediaURL;
Uri uri = new Uri(videoURL);
string filename = System.IO.Path.GetFileName(uri.LocalPath);
string localFilePath = Application.persistentDataPath + "/" + filename;
bool tryVideoDownload = true;
if (!File.Exists(localFilePath))
{
while (tryVideoDownload)
{
downloadProgressText.text = "Downloading";
showDownloadProgress = true;
downloadProgress.gameObject.SetActive(true);
UnityWebRequest www = UnityWebRequest.Get(videoURL);
yield return www.Send();
if (String.IsNullOrEmpty(www.error))
{
byte[] bytes = www.downloadHandler.data;
FileStream fs = new FileStream(localFilePath, FileMode.Create);
fs.Write(bytes, 0, bytes.Length);
downloadProgressText.text = "Download done!";
yield return new WaitForSeconds(2);
tryVideoDownload = false;
}//Video downloaded
else
{
showDownloadProgress = false;
downloadProgress.gameObject.SetActive(true);
downloadProgressText.text = "Download ERROR \n ";
downloadProgressText.text += www.error;
yield return new WaitForSeconds(2);
downloadProgressText.text = "Attempting to download again";
yield return new WaitForSeconds(2);
tryVideoDownload = true;
}
yield return null;
}
}
}
我有以下错误
当我尝试使用 unity www class 下载 mp4 文件时,它失败了,在 www.error
中只有 "aborted" 错误消息这是一个奇怪的错误,它只出现在某些设备上,我已经在 galaxy note 5 上尝试过并且运行良好,当有人在 galaxy s7 上尝试过时,他得到了那个错误
有谁知道发生了什么事吗?
感谢大家的帮助。
下载视频的代码
private IEnumerator DownloadVideo()
{
showDownloadProgress = true;
downloadProgress.gameObject.SetActive(true);
videoURL = MainPlayerCTRL.mediaURL;
Uri uri = new Uri(videoURL);
string filename = System.IO.Path.GetFileName(uri.LocalPath);
string localFilePath = Application.persistentDataPath + "/"+ filename;
bool tryVideoDownload = true;
if (!File.Exists(localFilePath))
{
while (tryVideoDownload)
{
downloadProgressText.text = "Downloading";
showDownloadProgress = true;
downloadProgress.gameObject.SetActive(true);
www = new WWW(videoURL);
yield return www;
if (String.IsNullOrEmpty(www.error))
{
byte[] bytes = www.bytes;
FileStream fs = new FileStream(localFilePath, FileMode.Create);
fs.Write(bytes, 0, bytes.Length);
downloadProgressText.text = "Download done!";
yield return new WaitForSeconds(2);
tryVideoDownload = false;
}//Video downloaded
else
{
showDownloadProgress = false;
downloadProgress.gameObject.SetActive(true);
downloadProgressText.text = "Download ERROR \n ";
downloadProgressText.text += www.error;
yield return new WaitForSeconds(2);
downloadProgressText.text = "Attempting to download again";
yield return new WaitForSeconds(2);
tryVideoDownload = true;
}
}
yield return new WaitForEndOfFrame();
}
}
我首先怀疑的是内存不足,这通常发生在使用 FileStream
复制大文件时,但您提到您从 www.error
得到了 "aborted" 错误。罪魁祸首可能是 WWW
,首先要做的是使用 UnityWebRequest
. I replaced WWW
in your function with UnityWebRequest
。
您需要在顶部包含 using UnityEngine.Networking;
才能使用它。另一件事是 yield return new WaitForEndOfFrame();
应该在 while 循环内部而不是在它外部并且使用 yield return null;
更好。
private IEnumerator DownloadVideo()
{
showDownloadProgress = true;
downloadProgress.gameObject.SetActive(true);
videoURL = MainPlayerCTRL.mediaURL;
Uri uri = new Uri(videoURL);
string filename = System.IO.Path.GetFileName(uri.LocalPath);
string localFilePath = Application.persistentDataPath + "/" + filename;
bool tryVideoDownload = true;
if (!File.Exists(localFilePath))
{
while (tryVideoDownload)
{
downloadProgressText.text = "Downloading";
showDownloadProgress = true;
downloadProgress.gameObject.SetActive(true);
UnityWebRequest www = UnityWebRequest.Get(videoURL);
yield return www.Send();
if (String.IsNullOrEmpty(www.error))
{
byte[] bytes = www.downloadHandler.data;
FileStream fs = new FileStream(localFilePath, FileMode.Create);
fs.Write(bytes, 0, bytes.Length);
downloadProgressText.text = "Download done!";
yield return new WaitForSeconds(2);
tryVideoDownload = false;
}//Video downloaded
else
{
showDownloadProgress = false;
downloadProgress.gameObject.SetActive(true);
downloadProgressText.text = "Download ERROR \n ";
downloadProgressText.text += www.error;
yield return new WaitForSeconds(2);
downloadProgressText.text = "Attempting to download again";
yield return new WaitForSeconds(2);
tryVideoDownload = true;
}
yield return null;
}
}
}