using System; using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices; using UnityEngine; namespace WeChatWASM { public class WXVideo { #if UNITY_WEBGL [DllImport("__Internal")] #endif private static extern void WXVideoPlay(string id); #if UNITY_WEBGL [DllImport("__Internal")] #endif private static extern void WXVideoAddListener(string id, string key); #if UNITY_WEBGL [DllImport("__Internal")] #endif private static extern void WXVideoDestroy(string id); #if UNITY_WEBGL [DllImport("__Internal")] #endif private static extern void WXVideoExitFullScreen(string id); #if UNITY_WEBGL [DllImport("__Internal")] #endif private static extern void WXVideoPause(string id); #if UNITY_WEBGL [DllImport("__Internal")] #endif private static extern void WXVideoRequestFullScreen(string id,int direction); #if UNITY_WEBGL [DllImport("__Internal")] #endif private static extern void WXVideoSeek(string id, int time); #if UNITY_WEBGL [DllImport("__Internal")] #endif private static extern void WXVideoStop(string id); #if UNITY_WEBGL [DllImport("__Internal")] #endif private static extern void WXVideoRemoveListener(string id, string key); #if UNITY_WEBGL && !UNITY_EDITOR private static readonly bool isWebGLPlayer = true; #else private static readonly bool isWebGLPlayer = false; #endif private bool isWaitingPlay = false; private bool isWaitingStop = false; private bool isWaitingPause = false; private bool _isPlaying = false; private Action _onPlay = null; private Action _onPause = null; private Action _onEnded = null; private Action _onTimeUpdate = null; private Action _onProgress = null; private Action _onError = null; private Action _onWaiting = null; private Action _onSeeking = null; private Action _onSeeked = null; private WXCreateVideoParam param; public string instanceId; //内部使用,请不要访问这个 public static Dictionary _Dict = new Dictionary(); public WXVideo(string id, WXCreateVideoParam param) { instanceId = id; this.param = param; _Dict.Add(id, this); OnPlay(() => { _isPlaying = true; }); OnEnded(() => { _isPlaying = false; }); OnPause(() => { _isPlaying = false; }); #if UNITY_EDITOR _isPlaying = param.autoplay; #endif } /// /// 内部函数,请不要调用 /// /// public void _HandleCallBack(WXVideoCallback res) { switch (res.errMsg) { case "onPlay": _onPlay?.Invoke(); break; case "onPause": _onPause?.Invoke(); break; case "onEnded": _onEnded?.Invoke(); break; case "onTimeUpdate": _onTimeUpdate?.Invoke(new WXVideoTimeUpdate() { position = res.position, duration = res.duration }); break; case "onProgress": _onProgress?.Invoke(new WXVideoProgress() { buffered = res.buffered, duration = res.duration }); break; case "onError": _onError?.Invoke(); break; case "onWaiting": _onWaiting?.Invoke(); break; case "onSeeking": _onSeeking?.Invoke(); break; case "onSeeked": _onSeeked?.Invoke(); break; } } /// /// 是否正在播放 /// public bool isPlaying { get { return _isPlaying; } } /// /// 播放视频 /// public void Play() { if (isWebGLPlayer) { if (!isWaitingPlay) { isWaitingPlay = true; WXSDKManagerHandler.Instance.StartCoroutine(DoPlay()); } return; } Debug.Log(param.src + " 视频播放了,这里就不真的播放了。"); _HandleCallBack(new WXVideoCallback() { errMsg= "onPlay" }); } private IEnumerator DoPlay() { //这里unity音频调用太频繁,延迟0.1秒后再执行 yield return new WaitForSeconds(0.1f); WXVideoPlay(instanceId); isWaitingPlay = false; } /// /// 监听视频播放事件 /// public void OnPlay(Action action) { if (_onPlay == null) { if (isWebGLPlayer) { WXVideoAddListener(instanceId, "onPlay"); } } _onPlay += action; } /// /// 取消监听视频播放事件,action 为空表示移除全部事件监听 /// public void OffPlay(Action action = null) { if (action == null) { _onPlay = null; } else { _onPlay -= action; } if (_onPlay == null) { if (isWebGLPlayer) { WXVideoRemoveListener(instanceId, "offPlay"); } } } /// /// 监听视频播放到末尾事件 /// public void OnEnded(Action action) { if (_onEnded == null) { if (isWebGLPlayer) { WXVideoAddListener(instanceId, "onEnded"); } } _onEnded += action; } /// /// 取消监听视频播放到末尾事件 /// public void OffEnded(Action action = null) { if (action == null) { _onEnded = null; } else { _onEnded -= action; } if (_onEnded == null) { if (isWebGLPlayer) { WXVideoRemoveListener(instanceId, "offEnded"); } } } /// /// 监听视频错误事件 /// public void OnError(Action action) { if (_onError == null) { if (isWebGLPlayer) { WXVideoAddListener(instanceId, "onError"); } } _onError += action; } /// /// 取消监听视频错误事件,action 为空表示移除全部事件监听 /// public void OffError(Action action = null) { if (action == null) { _onError = null; } else { _onError -= action; } if (_onError == null) { if (isWebGLPlayer) { WXVideoRemoveListener(instanceId, "offError"); } } } /// /// 监听视频暂停事件 /// public void OnPause(Action action) { if (_onPause == null) { if (isWebGLPlayer) { WXVideoAddListener(instanceId, "onPause"); } } _onPause += action; } /// /// 取消监听视频暂停事件,action 为空表示移除全部事件监听 /// public void OffPause(Action action = null) { if (action == null) { _onPause = null; } else { _onPause -= action; } if (_onPause == null) { if (isWebGLPlayer) { WXVideoRemoveListener(instanceId, "offPause"); } } } /// /// 监听视频由于需要缓冲下一帧而停止时触发 /// public void OnWaiting(Action action) { if (_onWaiting == null) { if (isWebGLPlayer) { WXVideoAddListener(instanceId, "onWaiting"); } } _onWaiting += action; } /// /// 取消监听视频由于需要缓冲下一帧而停止时触发,action 为空表示移除全部事件监听 /// public void OffWaiting(Action action = null) { if (action == null) { _onWaiting = null; } else { _onWaiting -= action; } if (_onWaiting == null) { if (isWebGLPlayer) { WXVideoRemoveListener(instanceId, "offWaiting"); } } } /// /// 销毁当前实例 /// public void Destroy() { if (isWebGLPlayer) { WXVideoDestroy(instanceId); } else { Debug.Log(param.src + " 视频销毁!"); } _Dict.Remove(instanceId); } /// /// 视频退出全屏 /// public void ExitFullScreen() { if (isWebGLPlayer) { WXVideoExitFullScreen(instanceId); } else { Debug.Log(param.src + " 视频退出全屏!"); } } /// /// 暂停 /// public void Pause() { if (isWebGLPlayer) { if (!isWaitingPause) { isWaitingPause = true; WXSDKManagerHandler.Instance.StartCoroutine(DoPause()); } return; } Debug.Log(param.src + " 视频暂停了"); _HandleCallBack(new WXVideoCallback() { errMsg = "onPause" }); } IEnumerator DoPause() { //这里调用太频繁,延迟0.1秒后再执行 yield return new WaitForSeconds(0.1f); WXVideoPause(instanceId); isWaitingPause = false; } /// /// 视频全屏 /// /// 设置全屏时视频的方向 0: 正常竖向, 90: 屏幕逆时针90度 . -90: 屏幕顺时针90度 public void RequestFullScreen(int direction) { if (isWebGLPlayer) { WXVideoRequestFullScreen(instanceId, direction); } else { Debug.Log(param.src + " 视频全屏!"); } } /// /// 视频跳转 /// /// 视频跳转到指定位置,单位为 s 秒 public void Seek(int time) { WXVideoSeek(instanceId, time); } /// /// 停止视频 /// public void Stop() { if (isWebGLPlayer) { if (!isWaitingStop) { isWaitingStop = true; WXSDKManagerHandler.Instance.StartCoroutine(DoStop()); } return; } Debug.Log(param.src + " 音频停止了"); } IEnumerator DoStop() { //这里unity调用太频繁,延迟0.1秒后再执行 yield return new WaitForSeconds(0.1f); WXVideoStop(instanceId); isWaitingStop = false; } /// /// 监听视频播放进度更新事件 /// /// public void OnTimeUpdate(Action action) { if (_onTimeUpdate == null) { if (isWebGLPlayer) { WXVideoAddListener(instanceId, "onTimeUpdate"); } } _onTimeUpdate += action; } /// /// 取消监听视频播放进度更新事件,action 为空表示移除全部事件监听 /// public void OffTimeUpdate(Action action = null) { if (action == null) { _onTimeUpdate = null; } else { _onTimeUpdate -= action; } if (_onTimeUpdate == null) { if (isWebGLPlayer) { WXVideoRemoveListener(instanceId, "offTimeUpdate"); } } } /// /// 监听视频下载(缓冲)事件 /// /// public void OnProgress(Action action) { if (_onTimeUpdate == null) { if (isWebGLPlayer) { WXVideoAddListener(instanceId, "onProgress"); } } _onProgress += action; } /// /// 取消监听视频下载(缓冲)事件 ,action 为空表示移除全部事件监听 /// public void OffProgress(Action action = null) { if (action == null) { _onProgress = null; } else { _onProgress -= action; } if (_onProgress == null) { if (isWebGLPlayer) { WXVideoRemoveListener(instanceId, "offProgress"); } } } } }