https://docs.unity3d.com/cn/2021.1/Manual/class-VideoPlayer.html
官方使用文档,参数不多,且介绍的很详细,一般来说,都是使用VideoClip的方式,照着文档来就可以正常走通。但是,如果把视频资源打进AssetBundle里,当在Android 9及更低版本的手机上时,就会一片黑色,无法正常播放。
查看系统log,会找到一个报错,说的是无法找到该视频。这大概是控件自身的兼容问题,在更高版本的系统上是可以正常播放的。虽然这个版本的设备已经很少,在我们的产品里,只占零点几个点,但还是要兼容一下。
这个时候就不能使用VideoClip的方式。而是将视频文件以.bytes扩展名打进AssetBundle中,在播放的时候,以TextAsset的形式把视频文件提取出来,再以.mp4的扩展存储到本地一份,然后通过VideoPlayer控件的URL模式来播放本地的视频文件,这样就正常了。
示例代码:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 
 | public RenderTexture texture;
 public VideoPlayer videoPlayer;
 public RawImage rawImage;
 
 
 public string videoPath = "";
 
 private IEnumerator Play()
 {
 
 var rect = (RectTransform) rawImage.transform;
 texture = RenderTexture.GetTemporary((int) rect.width, (int) rect.height);
 videoPlayer.targetTexture = _texture;
 rawImage.texture = _texture;
 
 
 var h = Addressables.LoadAssetAsync<TextAsset>(videoPaty);
 yield return h;
 
 
 var path = Path.Combine(Application.persitentDataPath, "LocalVideo.mp4");
 if (!File.Exists(path))
 {
 File.WriteAllBytes(path, h.Result.bytes);
 }
 
 Addressables.Release(h);
 
 
 videoPlayer.url = url;
 videoPlayer.isLooping = false;
 videoPlayer.loopPointReached += OnFinish;
 videoPlayer.Play();
 }
 
 private void OnFinish()
 {
 RenderTexture.ReleaseTemporary(texture);
 }
 
 | 
(完)