VideoPlayer

糜凯泽
2023-12-01

//接口

public interface IVideo

{
    event Action OnVideoEnd;
    string URL { get; }
    double Time { get; }
    double TotalTime { get; }
    long Frame { get; }
    ulong FrameCount { get; }
    float FrameRate { get; }
    bool IsLoop { get; }
    float Progress { get; }
    void SetProgress(float progress);
    void SetPlaybackSpeed(float speed);
    void PlayVideo();
    void PauseVideo();
    void OpenVideo(string url);
    void CloseVideo();
    void RestartVideo();

}


//具体实现

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
[RequireComponent(typeof(VideoPlayer))]
public class Video : MonoBehaviour, IVideo
{
    protected void SelectPlayMode()
    {
        videoPlayer.renderMode = VideoRenderMode.CameraNearPlane;
        videoPlayer.targetCameraAlpha = 1;
        videoPlayer.aspectRatio = VideoAspectRatio.FitOutside;
    }

    private string url = string.Empty;
    public string URL { get { return url; } }

    private VideoPlayer videoPlayer;
    public double Time { get { return videoPlayer.time; } }
    public double TotalTime { get { return 0f == videoPlayer.frameRate ? 0f : videoPlayer.frameCount / videoPlayer.frameRate; } }
    public long Frame { get { return videoPlayer.frame; } }
    public ulong FrameCount { get { return videoPlayer.frameCount; } }
    public float FrameRate { get { return videoPlayer.frameRate; } }
    public bool IsLoop { get { return videoPlayer.isLooping; } }
    public float Progress { get { return 0f == videoPlayer.frameCount ? 0f : (float)videoPlayer.frame / (long)videoPlayer.frameCount; } }
    public event Action OnVideoEnd;

    void Awake()
    {
        videoPlayer = GetComponent<VideoPlayer>();
        videoPlayer.loopPointReached += (source) => { if (OnVideoEnd != null) OnVideoEnd(); };
    }

    public void CloseVideo()
    {
        videoPlayer.Stop();
        if (OnVideoEnd != null) OnVideoEnd();
    }

    public void OpenVideo(string url)
    {
        SelectPlayMode();


        this.url = url;
        videoPlayer.url = url;
        videoPlayer.Prepare();
    }

    public void PauseVideo()
    {
        videoPlayer.Pause();
    }

    public void PlayVideo()
    {
        videoPlayer.Play();
    }

    public void RestartVideo()
    {
        OpenVideo(url);
    }

    public void SetPlaybackSpeed(float speed)
    {
        videoPlayer.playbackSpeed = speed;
    }

    public void SetProgress(float progress)
    {
        if (progress >= 0 && progress <= 1)
            videoPlayer.frame = (long)(progress * videoPlayer.frameCount);
    }
}


//应用

using UnityEngine;
using UnityEngine.UI;
public class VideoPanel : BaseUIPanel {
    public string url;
    public Video video;
    public string key;
    public GameObject LoginCanvas;
    public bool isTestVideo = true;

    public Slider speedSlider;
    public Slider progressSlider;
    public GameObject skipButton;

    void Awake() {
        InitData();
        if (!PlayerPrefs.HasKey(key))
            PlayVideo();
        else
            gameObject.SetActive(false);
    }

    void Start()
    {
        speedSlider.onValueChanged.AddListener((value) => {
            value = speedSlider.value * 10;
            SetPlaySpeed(value);
        });

        progressSlider.onValueChanged.AddListener((value) => {
            SetVideoProgress(progressSlider.value);
        });

        RegisterButtonClickEvent(skipButton, SkipVideo);
    }

    protected void PlayVideo()
    {
        video.OpenVideo(url);
        video.OnVideoEnd += OnVideoEnd;
    }

    protected void SkipVideo(GameObject go)
    {
        video.CloseVideo();
    }

    protected void SetPlaySpeed(float speed)
    {
        video.SetPlaybackSpeed(speed);
    }

    protected void SetVideoProgress(float progress)
    {
        video.SetProgress(progress);
    }

    protected void InitData(){
        url = Application.streamingAssetsPath + "/25d705200a4eab4.mp4";
        video = Camera.main.gameObject.AddComponent<Video>();
        key = SystemInfo.processorType;
        if (isTestVideo && PlayerPrefs.HasKey(key))
            PlayerPrefs.DeleteKey(key);
        if (!PlayerPrefs.HasKey(key) && LoginCanvas)
            LoginCanvas.SetActive(false);

        speedSlider.value = 0.1f;
    }

    protected void OnVideoEnd(){
        if (!PlayerPrefs.HasKey(key))
            PlayerPrefs.SetInt(key, 1);

        if (LoginCanvas)
            LoginCanvas.SetActive(true);
    }

}


 类似资料:

相关阅读

相关文章

相关问答