发布Webgl时,视频加载方式需要使用URL,StreamingAssets相对路径(去掉file://)
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using UnityEngine.Video;
public class VideoControl : MonoBehaviour
//IDragHandler,IEndDragHandler
{
private float videotime;
public RenderTexture rt;
public VideoPlayer vp;
public Text timetxt;
public Slider process;
private AudioSource au;
public Image bau;
public Image play;
public Sprite Splay;
public Sprite Spause;
public Sprite Aplay;
public Sprite Apause;
private bool isPlayVideo;
// Start is called before the first frame update
void Start()
{
//process = this.GetComponent<Slider>();
au = this.GetComponent<AudioSource>();
process.OnEventTriggerEvent(EventTriggerType.Drag, OnSliderDrage);
process.OnEventTriggerEvent(EventTriggerType.EndDrag, Onslider);
isPlayVideo = false;
}
// Update is called once per frame
void Update()
{
if (isPlayVideo)
{
timetxt.text = string.Format("{0} / {1}", ToTimeFormat((float)vp.time), ToTimeFormat((float)vp.length));
process.value = (float)(vp.time / vp.length);
}
}
/// <summary>
/// 根据按钮id加载不同视频,图片,文字
/// </summary>
/// <param name="id"></param>
public void LoadVideo(int id)
{
isPlayVideo = false;
StopVideo();
switch (id)
{
case 1:
vp.url = Path.Combine(Application.streamingAssetsPath, "Video/视频1.mp4");
break;
case 2:
vp.url = Path.Combine(Application.streamingAssetsPath, "Video/视频2.mp4");
break;
}
vp.Prepare(); // 播放引擎准备(提高开始播放时的速度)
timetxt.text = string.Format("{0} / {1}", ToTimeFormat((float)vp.time), ToTimeFormat((float)vp.length));
process.value = 0;
vp.Pause();
play.overrideSprite = Splay;
}
public void StopVideo()
{
rt.Release();
}
private string ToTimeFormat(float time)
{
int seconds = (int)time;
int minutes = seconds / 60;
seconds %= 60;
return string.Format("{0}:{1}", minutes, seconds < 10 ? "0" + seconds.ToString() : seconds.ToString());
}
/// <summary>
/// 播放和暂停当前视频
/// </summary>
public void OnPlayOrPauseVideo()
{
if (vp.isPlaying == true)
{
vp.Pause();
play.overrideSprite = Splay;
isPlayVideo = false;
}
else
{
vp.Play();
play.overrideSprite = Spause;
isPlayVideo = true;
}
}
public void PlayorPause()
{
if (au.mute == true)
{
au.Play();
bau.overrideSprite = Aplay;
au.mute = false;
}
else
{
au.Pause();
bau.overrideSprite = Apause;
au.mute = true;
}
}
private void Onslider(BaseEventData arg0)
{
StartCoroutine("OnSliderEndDrage");
}
private IEnumerator OnSliderEndDrage()
{
float desiredTime = process.value * (float)vp.length;
vp.Play();
vp.time = desiredTime;
for (int i = 0; i < 30; i++)
{
float curVideoTime = (float)vp.time;
if (Mathf.CeilToInt(curVideoTime) == Mathf.CeilToInt(desiredTime))
{
break;
}
yield return null;
}
isPlayVideo = true;
}
public void OnSliderDrage(BaseEventData arg0)
{
vp.Pause();
isPlayVideo = false;
}
}
public static class UIBehaviourExtension
{
/// <summary>
/// 添加监听事件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="self"></param>
/// <param name="type"></param>
/// <param name="action"></param>
/// <returns></returns>
public static T OnEventTriggerEvent<T>(this T self,EventTriggerType type,UnityAction<BaseEventData> action)where T:UIBehaviour
{
var eventTrigger = self.GetComponent<EventTrigger>()??self.gameObject.AddComponent<EventTrigger>();
EventTrigger.Entry entry = new EventTrigger.Entry { eventID = type };
entry.callback.AddListener(action);
eventTrigger.triggers.Add(entry);
return self;
}
}