VideoPlayer控制脚本,使用videoplayer组件控制视频频播放
/***********************
* Title: "VideoPlayer控制脚本"
* Func:
* - videoplayer组件播放视频
* UsedBy:
* Date: 2017.11.01
* Author: LHL.
* Version: 1.0
* Description:
***********************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class VideoCtrl : MonoBehaviour {
public static VideoCtrl instance;
private VideoPlayer vPlayer;
public GameObject videoImage;//播放Image
private Button BtnPlay,BtnPause, BtnReStart;//开始,暂停,重播
public Slider sliderVideo;//进度条
private Button BtnX;//关闭
private Image VideoPanel; //视频背景
private Text NowTime;//播放时间
private Text TotalTime;//总时间
private float tt; //视频总时长
private float Index_t; //进度条计时时间
private float hour,min,second;
private bool IsPlay = true;
// Use this for initialization
void Awake () {
instance = this;
vPlayer = videoImage.GetComponent<VideoPlayer>();
BtnPlay = GameObject.Find("Kaishi").GetComponent<Button>();
BtnPause = GameObject.Find("Zanting").GetComponent<Button>();
BtnReStart = GameObject.Find("ReWindButton").GetComponent<Button>();
sliderVideo = GameObject.Find("SliderVideo").GetComponent<Slider>();
VideoPanel = GameObject.Find("VideoPanel").GetComponent<Image>();
BtnX = GameObject.Find("CloseButton").GetComponent<Button>();
TotalTime= GameObject.Find("ZongTimeText").GetComponent<Text>();
NowTime = GameObject.Find("NowTimeText").GetComponent<Text>();
}
public void OnEnable()
{
BtnReStart.onClick.AddListener(ClickReStart);
BtnX.onClick.AddListener(ClickBtnX);
BtnPlay.onClick.AddListener(ClickKaishi);
BtnPause.onClick.AddListener(ClickZanting);
//改变进度条
//sliderVideo.onValueChanged.AddListener(delegate { ChangeVideo(sliderVideo.value); });
}
public void OnDisable()
{
BtnReStart.onClick.RemoveListener(ClickReStart);
BtnX.onClick.RemoveListener(ClickBtnX);
BtnPlay.onClick.RemoveListener(ClickKaishi);
BtnPause.onClick.RemoveListener(ClickZanting);
//sliderVideo.onValueChanged.RemoveListener(delegate { ChangeVideo(sliderVideo.value); });
}
void Start()
{
ClickKaishi();//是否自动播放
//初始化总时间
//获取视频总时长 帧数/帧速率=总时长 如果是本地直接赋值的视频,我们可以通过VideoClip.length获取总时长
//tt = vPlayer.frameCount / vPlayer.frameRate;
tt = (float)vPlayer.clip.length;
//赋值slider的MaxValue
sliderVideo.maxValue = tt;
min = (int)tt / 60;
second = (int)tt % 60;
TotalTime.text = string.Format("/ {0:D2} : {1:D2} ",
fen.ToString(), second.ToString());
}
// Update is called once per frame
void Update ()
{
//播放
if (IsPlay)
{
vPlayer.Play();
//Time.timeScale = 1;
//进度条前进
Index_t += Time.deltaTime;
if (Index_t >= 0.1f)
{
sliderVideo.value += 0.1f;
Index_t = 0;
}
}
else
{
vPlayer.Pause();
//Time.timeScale = 0;
}
//进度条到底停止播放
if (sliderVideo.maxValue - sliderVideo.value <= 0.1f)
{
// sliderVideo.value = sliderVideo.maxValue;//不循环1
//ClickZanting();//不循环2
ClickReStart();//loop
}
//播放时间显示,每帧执行
ChangeTime((float)vPlayer.time);
}
//改变视频进度
public void ChangeVideo(float value)
{
vPlayer.time = value;
}
//播放时间显示
void ChangeTime(float value)
{
min = (int)value / 60;
second = (int)value % 60;
NowTime.text = string.Format("{0:D2} : {1:D2} ",
fen.ToString(), second.ToString());
}
//重播按钮
public void ClickReStart()
{
sliderVideo.value = 0;
vPlayer.time = 0;
}
//关闭按钮
public void ClickBtnX()
{
VideoPanel.gameObject.SetActive(false);
}
//开始按钮
public void ClickKaishi()
{
if(sliderVideo.value == sliderVideo.maxValue){
sliderVideo.value = 0;
}
videoImage.SetActive(true);
IsPlay = true;
BtnPause.gameObject.SetActive(true);
BtnPlay.gameObject.SetActive(false);
}
//暂停按钮
public void ClickZanting()
{
IsPlay = false;
BtnPause.gameObject.SetActive(false);
BtnPlay.gameObject.SetActive(true);
}
}
Slider控制脚本,使用进度条控制视频进度,挂在给Slider
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class SliderChange : MonoBehaviour, IDragHandler,IPointerClickHandler
{
//拖动改变视频进度
public void OnDrag(PointerEventData eventData)
{
VideoCtrl.instance.ChangeVideo(VideoCtrl.instance.sliderVideo.value);
}
//点击改变视频进度
public void OnPointerClick(PointerEventData eventData)
{
VideoCtrl.instance.ChangeVideo(VideoCtrl.instance.sliderVideo.value);
}
}
在WebGL上如果不能播放,可能是让它开始时自动播放了,需要手动点击播放
还是Edge浏览器牛批