Unity实现视频播放功能,制作视频播放器

目前先发布相关脚本代码,后续会对内容进行补充

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;

namespace FrameWork
{
    public interface IVideo
    {
        VideoPlayer GetVideoPlayer();

        RawImage GetVideoImage();

        void OnPlayVideo();

        void OnPlayVideo(int index);

        void OnPlayVideo(string url);

        void OnPauseVideo();

        void OnStopVideo();

        void AddVideoUrl(string url);

        void AddVideoUrls(string[] url);

        void RemoveAllUrls();

        void RemoveUrl(int index);
    }

}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace FrameWork
{
    public class VideoManager : FrameworkComponent
    {
        public override void Init()
        {
            sceneVideos = new Dictionary<string, IVideo>();
        }
        private Dictionary<string, IVideo> sceneVideos;
        public void RegistMySelf_Video(string name, IVideo video)
        {
            if (sceneVideos == null)
                return;
            if (!sceneVideos.ContainsKey(name))
            {
                sceneVideos.Add(name, video);
            }
            else
            {
                Debug.Log("已添加video:" + name + " ,请检查是否重名");
            }
        }
        public void UnRegistMySelf_Video(string name)
        {
            if (sceneVideos == null)
                return;
            if (sceneVideos.ContainsKey(name))
            {
                sceneVideos.Remove(name);
            }
            else
            {
                Debug.Log("未找到video:" + name);
            }
        }
        public IVideo GetVideoComponent(string name)
        {
            if (sceneVideos.ContainsKey(name))
            {
                return sceneVideos[name];
            }
            Debug.Log("未找到IVideo:" + name);
            return null;
        }
    }
}

接下来是核心代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Video;
using UnityEngine.UI;
namespace FrameWork
{
    [RequireComponent(typeof(RawImage))]
    public class VideoComponent : MonoBehaviour, IVideo
    {
        #region 内部变量
        private VideoPlayer vPlayer//
            ;
        private AudioSource source;//

        private RawImage videoImage;//

        private bool playOnPrepareCompleted = false;

        private string videoUrlDefault;

        private bool isInDrag = false;
        #endregion

        #region SerializeField
        [SerializeField]
        private List<string> videoUrlPool;

        public bool playOnAwake = false;

        public bool prepareOnAwake = true;


        public bool isLooping = false;

        public bool waitForFirstFrame = false;

        public bool isStreamingAssetsPath = false;

        public Graphic loadingUI;

        public Graphic coverUI;

        public Button playBtn;

        public Button stopBtn;

        public Button pauseBtn;

        public Slider sliderVideo;

        public Text text_Time;

        public Slider sliderSource;

       
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值