太大的文件可能需要很长的时间来提取和复制,有时他们 甚至可能导致用完存储空间或内存
这是真实的设备,但是你必须知道你是做。您不能仅将该文件与WWW
API或File.Copy
/File.WriteAllBytes
函数复制。
。复制视频块,然后播放它。这消除了内存不足的问题。
bool copyLargeVideoToPersistentDataPath(string videoNameWithExtensionName)
{
string path = Path.Combine(Application.streamingAssetsPath, videoNameWithExtensionName);
string persistentPath = Path.Combine(Application.persistentDataPath, "Videos");
persistentPath = Path.Combine(persistentPath, videoNameWithExtensionName);
bool success = true;
try
{
using (FileStream fromFile = new FileStream(path, FileMode.Open, FileAccess.Read))
{
using (FileStream toFile = new FileStream(persistentPath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
byte[] buffer = new byte[32 * 1024];
int bytesRead;
while ((bytesRead = fromFile.Read(buffer, 0, buffer.Length)) != 0)
{
toFile.Write(buffer, 0, bytesRead);
}
Debug.Log("Done! Saved to Dir: " + persistentPath);
Debug.Log(videoNameWithExtensionName + " Successfully Copied Video to " + persistentPath);
text.text = videoNameWithExtensionName + " Successfully Copied Video to " + persistentPath;
}
}
}
catch (Exception e)
{
Debug.Log(videoNameWithExtensionName + " Failed To Copy Video. Reason: " + e.Message);
text.text = videoNameWithExtensionName + " Failed To Copy Video. Reason: " + e.Message;
success = false;
}
return success;
}
我不希望有复制视频,有两个600MB的文件时使用 只有一个。
这里有你有其他的选择:
。 Resources文件夹。 (不推荐),因为这会减慢你的游戏加载时间。值得一提。随着新的统一VideoPlayer
,您可以加载视频是这样的:不需要这个
VideoClip video = Resources.Load("videoFile", typeof(VideoClip)) as VideoClip;
AVPro插件,并且不再需要统一。
如果这是你想要的,你也可以在AVPro插件中使用Resources文件夹。
只需将视频扩展名更改为.bytes
,然后将其加载为字节。如果AVPro插件可以按字节播放视频,那么你去!
TextAsset txtAsset = (TextAsset)Resources.Load("videoFile", typeof(TextAsset));
byte[] videoFile = txtAsset.bytes;
。用AssetBundle加载视频。过去,当新的Video API推出时,这不被支持,但现在应该得到支持。
IEnumerable LoadObject(string path)
{
AssetBundleCreateRequest bundle = AssetBundle.LoadFromFileAsync(path);
yield return bundle;
AssetBundle myLoadedAssetBundle = bundle.assetBundle;
if (myLoadedAssetBundle == null)
{
Debug.Log("Failed to load AssetBundle!");
yield break;
}
AssetBundleRequest request = myLoadedAssetBundle.LoadAssetAsync<GameObject>("VideoFile");
yield return request;
VideoClip clip = request.asset as VideoClip;
}
。最后,使用StreamingAssets
文件夹,但不是抄袭的视频,创建本地服务器与HttpListener
并将其指向的StreamingAssets
路径。
现在,连接到与新VideoPlayer
和播放视频。你可以在stackoverflow上找到很多HttpListener
服务器的例子。 我已经完成了这个过去,它的工作。
*“我读过,我不应该使用StreamingAssets文件夹”*您在哪里读过这些内容? – Programmer
https://forum.unity3d.com/threads/large-videos-not-playing-on-gear-vr-but-do-play-on-google-cardboard-any-ideas.395779/ –
我使用AVPro和它们的文档中说明了这一点。 “机器人 - ?为什么从StreamingAssets文件夹不是我的巨大的视频文件播放 –