unity Video Player视频播放

 发布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;
    }

 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值