Unity实现视频拖动条,,暂停按钮,开始按钮,当前时间控制

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

public class VideoController1 : MonoBehaviour
{
    public VideoPlayer videoPlayer;
    public Slider sliderVideo;
    public Text currentTimeText;
    public Text totalTimeText;
    public Button playButton;
    public Button pauseButton;

    private bool isDraggingSlider = false;

    private void Start()
    {
        if (videoPlayer == null)
        {
            Debug.LogError("VideoPlayer component is missing!");
            return;
        }

        videoPlayer.prepareCompleted += VideoPrepareCompleted;
        videoPlayer.loopPointReached += OnVideoEnd;
        sliderVideo.onValueChanged.AddListener(OnSliderValueChange);

        playButton.onClick.AddListener(OnClickPlay);
        pauseButton.onClick.AddListener(OnClickPause);

        videoPlayer.Prepare();

    }

    public void OpenVideo() {

    }


    private void VideoPrepareCompleted(VideoPlayer source)
    {
        sliderVideo.maxValue = (float)videoPlayer.length;
        UpdateTimeTexts(0f);
    }

    private void OnSliderValueChange(float value)
    {
        if (!isDraggingSlider)
        {
            videoPlayer.time = value;
            UpdateTimeTexts(value);
        }
    }

    private void UpdateTimeTexts(float time)
    {
        int minutes = Mathf.FloorToInt(time / 60f);
        int seconds = Mathf.FloorToInt(time % 60f);
        currentTimeText.text = string.Format("{0:00}:{1:00}", minutes, seconds);

        int totalMinutes = Mathf.FloorToInt((float)videoPlayer.length / 60f);
        int totalSeconds = Mathf.FloorToInt((float)videoPlayer.length % 60f);
        totalTimeText.text = string.Format("{0:00}:{1:00}", totalMinutes, totalSeconds);
    }

    private void OnClickPlay()
    {
        videoPlayer.Play();
        playButton.gameObject.SetActive(false);
        pauseButton.gameObject.SetActive(true);
    }

    private void OnClickPause()
    {
        videoPlayer.Pause();
        playButton.gameObject.SetActive(true);
        pauseButton.gameObject.SetActive(false);
    }

    private void OnVideoEnd(VideoPlayer vp)
    {
        videoPlayer.Stop();
        playButton.gameObject.SetActive(true);
        pauseButton.gameObject.SetActive(false);
        UpdateTimeTexts(0f);
    }

    private void Update()
    {
        if (videoPlayer.isPlaying && !isDraggingSlider)
        {
            sliderVideo.value = (float)videoPlayer.time;
            UpdateTimeTexts((float)videoPlayer.time);
        }
    }

    public void BeginDraggingSlider()
    {
        isDraggingSlider = true;
    }

    public void EndDraggingSlider()
    {
        isDraggingSlider = false;
        videoPlayer.time = sliderVideo.value;
        UpdateTimeTexts((float)videoPlayer.time);
    }

    public void PlayVideo(VideoPlayer videoPlayer, VideoClip videoClip, Action videoStartCallback = null, Action videoEndCallback = null)
    {
        if (videoClip == null)
        {
            Debug.LogError("VideoClip is null. Please assign a valid VideoClip.");
            return;
        }

        if (videoPlayer == null)
        {
            videoPlayer = gameObject.AddComponent<VideoPlayer>();
            videoPlayer.playOnAwake = false;
            videoPlayer.waitForFirstFrame = true;
        }

        videoPlayer.clip = videoClip;
        videoPlayer.Play();

        videoStartCallback?.Invoke();
        StartCoroutine(WaitForVideoEnd(videoPlayer, videoEndCallback));
    }


    private IEnumerator WaitForVideoEnd(VideoPlayer videoPlayer, Action action = null)
    {
        while (videoPlayer.isPlaying)
        {
            yield return null;
        }

        yield return new WaitForSeconds((float)videoPlayer.clip.length);

        action?.Invoke();

    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一丁目赠我

谢谢你的打赏,感谢

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值