我有以下错误
当我尝试下载带有统一www类的mp4文件时,它失败并且只有“中止”错误消息在www.error
它是一个奇怪的错误,它只出现在某些设备上,我在Galaxy note 5上尝试过它并且运行良好,当有人在Galaxy s7上试过它时,他得到了这个错误
任何一个mybe知道什么是hapening?
感谢您的帮助。
下载视频的代码
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();
}
}
答案 0 :(得分:1)
我怀疑的第一件事就是在使用FileStream
复制大文件时通常会发生内存不足但是你提到你已经中止"来自www.error
的错误。罪魁祸首可能是WWW
,首先要做的是使用UnityWebRequest
。我用UnityWebRequest
替换了函数中的WWW
。
您需要在顶部添加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;
}
}
}