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();
        au = this.GetComponent();
        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);
        }

    }
    /// 
    /// 根据按钮id加载不同视频,图片,文字
    /// 
    /// 
    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());
    }
    /// 
    /// 播放和暂停当前视频
    /// 
    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
{
    /// 
    /// 添加监听事件
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    public static T OnEventTriggerEvent(this T self,EventTriggerType type,UnityAction action)where T:UIBehaviour
    {
        var eventTrigger = self.GetComponent()??self.gameObject.AddComponent();
        EventTrigger.Entry entry = new EventTrigger.Entry { eventID = type };
        entry.callback.AddListener(action);
        eventTrigger.triggers.Add(entry);
        return self;
    }

 
}

你可能感兴趣的:(unity,音视频,游戏引擎)