您使用了UnityWebRequest 错误。如果您使用 new 关键字创建了 UnityWebRequest 的新实例,则只需创建 DownloadHandler 的新实例。
如果您使用 UnityWebRequest.Get 和 UnityWebRequest.Post 等静态函数创建新的 UnityWebRequest 实例,则不必这样做。
为回答您的问题,以下是使用Handheld.PlayFullScreenMovie 下载和播放视频的步骤。
1.使用UnityWebRequest下载视频。
2.将视频字节从UnityWebRequest.downloadHandler.data保存到Application.persistentDataPath。你错过了Application.persistentDataPath 部分。
3.播放视频:
安卓:
使用Application.persistentDataPath/videoName.mp4 作为Handheld.PlayFullScreenMovie 函数的路径。
iOS:
使用"file://" + Application.persistentDataPath/videoName.mp4; 作为Handheld.PlayFullScreenMovie 函数的路径。
请注意,这在编辑器中不起作用。没关系。 Handheld.PlayFullScreenMovie 是为 Android 和 iOS 设计的,而不是为台式机设计的。 只需为 Android/iOS 构建它来测试它。
以下是完整的移动设备下载和播放示例:
用法:
void Start()
{
string url = "http://techslides.com/demos/sample-videos/small.mp4";
StartCoroutine(downloadAndPlayVideo(url, "myvideo.mp4", true));
}
示例:
//Downloads, Saves and Plays the Video
IEnumerator downloadAndPlayVideo(string videoUrl, string saveFileName, bool overwriteVideo)
{
//Where to Save the Video
string saveDir = Path.Combine(Application.persistentDataPath, saveFileName);
//Play back Directory
string playbackDir = saveDir;
#if UNITY_IPHONE
playbackDir = "file://" + saveDir;
#endif
bool downloadSuccess = false;
byte[] vidData = null;
/*Check if the video file exist before downloading it again.
Requires(using System.Linq)
*/
string[] persistantData = Directory.GetFiles(Application.persistentDataPath);
if (persistantData.Contains(playbackDir) && !overwriteVideo)
{
Debug.Log("Video already exist. Playing it now");
//Play Video
playVideo(playbackDir);
//EXIT
yield break;
}
else if (persistantData.Contains(playbackDir) && overwriteVideo)
{
Debug.Log("Video already exist [but] we are [Re-downloading] it");
yield return downloadData(videoUrl, (status, dowloadData) =>
{
downloadSuccess = status;
vidData = dowloadData;
});
}
else
{
Debug.Log("Video Does not exist. Downloading video");
yield return downloadData(videoUrl, (status, dowloadData) =>
{
downloadSuccess = status;
vidData = dowloadData;
});
}
//Save then Play if there was no download error
if (downloadSuccess)
{
//Save Video
saveVideoFile(saveDir, vidData);
//Play Video
playVideo(playbackDir);
}
}
//Downloads the Video
IEnumerator downloadData(string videoUrl, Action<bool, byte[]> result)
{
//Download Video
UnityWebRequest webRequest = UnityWebRequest.Get(videoUrl);
webRequest.Send();
//Wait until download is done
while (!webRequest.isDone)
{
Debug.Log("Downloading: " + webRequest.downloadProgress);
yield return null;
}
//Exit if we encountered error
if (webRequest.isError)
{
Debug.Log("Error while downloading Video: " + webRequest.error);
yield break; //EXIT
}
Debug.Log("Video Downloaded");
//Retrieve downloaded Data
result(!webRequest.isError, webRequest.downloadHandler.data);
}
//Saves the video
bool saveVideoFile(string saveDir, byte[] vidData)
{
try
{
FileStream stream = new FileStream(saveDir, FileMode.Create);
stream.Write(vidData, 0, vidData.Length);
stream.Close();
Debug.Log("Video Downloaded to: " + saveDir.Replace("/", "\\"));
return true;
}
catch (Exception e)
{
Debug.Log("Error while saving Video File: " + e.Message);
}
return false;
}
//Plays the video
void playVideo(string path)
{
Handheld.PlayFullScreenMovie(path, Color.black,
FullScreenMovieControlMode.Full, FullScreenMovieScalingMode.AspectFill);
}
此答案仅用于帮助使用 Unity 5.5 及以下版本的人。如果您使用上述任何内容,则有一个新的 API 可以播放视频,它适用于移动和台式计算机。您可以找到示例here。