使用Android上的Handheld.PlayFullScreenMovie下载并播放视频

时间:2017-05-18 08:15:11

标签: c# android video unity3d

我正在开发使用Unity制作的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编辑器中有效。但由于Android上没有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(); 

2 个答案:

答案 0 :(得分:1)

您使用UnityWebRequest错误。如果您使用DownloadHandler关键字创建了UnityWebRequest的新实例,则只需创建new的新实例。

如果您使用UnityWebRequest.GetUnityWebRequest.Post函数等静态函数创建UnityWebRequest的新实例,则不必如此。

要回答您的问题,请按以下步骤使用Handheld.PlayFullScreenMovie下载和播放视频。

1 。使用UnityWebRequest下载视频。

2 。保存从UnityWebRequest.downloadHandler.dataApplication.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播放视频,该API适用于移动和台式计算机。您可以找到示例here

答案 1 :(得分:0)

不要把视频放在你的路上。

StreamingAssets是一个特殊的文件夹,无论是android平台还是ios,都必须放入视频。其次,没有必要提供流媒体资产的路径,其隐含地理解为统一参考路径是流资产文件夹。所以把myvideo.mp4放在流媒体资源文件夹中并像这样使用它:

Handheld.PlayFullScreenMovie("myvideo.mp4", Color.black, FullScreenMovieControlMode.Hidden);

here。根据这个,您需要检查mp4是否具有正确的配置,编解码器,分辨率等。

我最好的猜测是你有一个错字。文件名和扩展名区分大小写!