我正在开发一款与团结公司一起制作的Android游戏。您可以在游戏中下载视频,它们将存储在以下路径:
Android/data/com.mycompany.mygame/files/Movies/myvideo.mp4
更准确地说,我使用以下方式下载它:
FileStream stream = new FileStream (folder + "/" + fileName, FileMode.Create);
stream.Write(BytesDownload, 0, BytesDownload.Length); 其中BytesDownload是从UnityWebRequest中获得的。folder + "/" + fileName是上面提到的路径。
我打电话给Handheld.PlayFullScreenMovie是为了看这个视频:
Handheld.PlayFullScreenMovie(videoPath, Color.black, FullScreenMovieControlMode.Hidden);
在本例中,videoPath采用Movies/myvideo.mp4值。然而,这是行不通的(我在我的Android平板电脑上得到了一个黑色屏幕,而不是视频播放)。
我在文档中看到,视频需要存储在StreamingAssets文件夹中,这确实是在Unity中工作的。但是由于安卓上没有StreamingAssets文件夹,所以我试着把视频放在上面提到的路径中。
我如何下载视频并在Android上播放呢?
编辑-用于下载视频的代码:
webRequest = UnityWebRequest.Get(serverURL + "/" + serverFolder + "/" + fileName);
DownloadHandler receiveBundle = new DownloadHandlerBuffer();
webRequest.downloadHandler = receiveBundle;
webRequest.Send();
do {
yield return null;
} while (!webRequest.downloadHandler.isDone);
byte[] BytesDownload = receiveBundle.data;
FileStream stream = new FileStream (folder + "/" + fileName, FileMode.Create);
stream.Write(BytesDownload, 0, BytesDownload.Length);
stream.Close(); 发布于 2017-05-18 18:34:15
你使用UnityWebRequest是错误的。如果使用DownloadHandler关键字创建了UnityWebRequest的新实例,则只需要创建new的新实例。
如果使用静态函数(如UnityWebRequest和UnityWebRequest.Post函数)创建新的实例,则不必这样做。
为了回答您的问题,下面是下载并使用Handheld.PlayFullScreenMovie播放视频的步骤。
1.Download与UnityWebRequest的视频。
2.Save从UnityWebRequest.downloadHandler.data到Application.persistentDataPath的视频字节。你错过了Application.persistentDataPath的部分。
3.Play视频:
Android:
使用Application.persistentDataPath/videoName.mp4作为带有Handheld.PlayFullScreenMovie函数的路径。
iOS:
使用"file://" + Application.persistentDataPath/videoName.mp4;作为带有Handheld.PlayFullScreenMovie函数的路径。
注意,这将不能在编辑器中工作。这很好。Handheld.PlayFullScreenMovie 是为安卓开发的,iOS不是为桌面电脑设计的。只是为安卓/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);
}这个答案只是为了帮助那些使用Unity5.5和更低版本的人。如果您正在使用上面的任何内容,将会有一个新的API来播放视频,它可以在移动计算机和桌面计算机上使用。您可以找到示例这里。
发布于 2017-05-18 08:28:27
不要把视频放在你的路上。
StreamingAssets是一个特殊的文件夹,无论是安卓平台还是ios,都必须把视频放进去。其次,没有必要给出流资产的路径,它隐含地理解为引用路径是流资产文件夹。因此,将myvideo.mp4放在StreamingAssets文件夹中,并按如下方式使用:
Handheld.PlayFullScreenMovie("myvideo.mp4", Color.black, FullScreenMovieControlMode.Hidden);看这里。根据这一点,您需要检查mp4是否具有正确的配置、编解码器、分辨率等。
我最好的猜测是你有一个错误。文件名和扩展名区分大小写!
https://stackoverflow.com/questions/44042182
复制相似问题