強火で進め

このブログではプログラム関連の記事を中心に書いてます。

Android 環境で AssetBundle 化した Video Clip を再生する時の注意点

サポートは Unity 2018.1 から

まず、 Android 環境での AssetBundle 化した Video Clip の再生は Unity 2018.1 からのサポートに成ります。それ以前の Unity では使えません。

2018.1のリリースノートに「Video: Added support for reading videos from AssetBundles on Android.」という記述が有ります。

What's new in Unity 2018.1 - Unity
https://unity3d.com/jp/unity/whats-new/unity-2018.1.0

ファイルの場所とフォーマット

次に 公式ドキュメントの VideoPlayer のページを見ると Android の部分に以下の記述が有ります。

Playback from asset bundles is only supported for uncompressed bundles, read directly from disk.

ざっくり翻訳すると「AssetBundle から再生するにはディスクから直接読み込める非圧縮の AssetBundle だけサポートしてるよ」と書かれています。

つまり、 AssetBundle を作る時の BuildPipeline.BuildAssetBundles() のオプションで BuildAssetBundleOptions.UncompressedAssetBundle を使わないとダメという事です。

圧縮したものを読み込もうとするとこの様なエラー文が出力されます。

AndroidVideoMedia::OpenExtractor could not translate archive:/CAB-xxxxxxxxxxxxxxxxx/CAB-yyyyyyyyyyyyy.resource to local file. Make sure file exists, is on disk (not in memory) and not compressed.
AndroidVideoMedia: Error opening extractor: -10004

また、以下の様にダウンロードしたものをそのまま再生すると失敗します。

        using (UnityWebRequest uwr = UnityWebRequestAssetBundle.GetAssetBundle(url))
		{
			yield return uwr.SendWebRequest();

			if (uwr.isNetworkError || uwr.isHttpError)
			{
				Debug.Log(uwr.error);
			}
			else
			{
				bundle = DownloadHandlerAssetBundle.GetContent(uwr);
				var vc = bundle.LoadAsset<VideoClip>("assets/movie.mp4");
				videoPlayer.clip = vc;
				videoPlayer.Prepare();
			}
		}

Application.persistentDataPath のフォルダなどに一度保存したものを再生する場合は問題無く再生されます。

[コード例(一部)]

        string assetFilePath = Application.persistentDataPath + "/movie";
        using (UnityWebRequest www = UnityWebRequest.Get(url))
        {
            Debug.LogFormat("Start DL: {0}", url);
            yield return www.SendWebRequest();

            if (www.isNetworkError || www.isHttpError)
            {
                Debug.Log(www.error);
            }
            else
            {
                File.WriteAllBytes(assetFilePath, www.downloadHandler.data);
                Debug.Log("AssetFile Path: " + assetFilePath);
                bundle = AssetBundle.LoadFromFile(assetFilePath);
                Debug.Log(bundle);
                Debug.Log("-------AssetNames");
                foreach (var n in bundle.GetAllAssetNames())
                {
                    Debug.Log(n);
                }
                var vc = bundle.LoadAsset<VideoClip>("assets/movie.mp4");
                Debug.Log(vc);

                videoPlayer.clip = vc;
                videoPlayer.Prepare();
            }
        }

Graphics API

Vulkan の環境で再生出来るのは 2019.2.0 からです。それ以前の Unity で作ったアプリが Vulkan の環境で動画再生をするとクラッシュします。 OpenGL ES のみの環境で実行される様にアプリの設定を行っておきましょう。

Unity Issue Tracker - [Android][Vulkan][VideoPlayer] VideoPlayer causes a crash with Vulkan API
https://issuetracker.unity3d.com/issues/android-vulkan-videoplayer-video-doesnt-play-or-causes-a-crash-on-newer-version-of-unity-with-vulkan-api

Unity 2019.2.0 に「Android: The Unity video player can now be used with the Vulkan renderer.」という記述が有り、このバージョンから Vulkan 環境での再生に対応している事が確認できます。

What's new in Unity 2019.2.0 - Unity
https://unity3d.com/jp/unity/whats-new/2019.2.0