赞
踩
1.创建如下所示unity界面
button1:上一个视频
button2:播放/暂停
button3:下一个视频
vidotime:是一个text,显示视频时间
videoname:视频名称
2.具体界面如下所示
1.在RawImage中导入脚本,实现对视频播放/暂停,及切换视频功能
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Video; public class Video_Controller : MonoBehaviour { private VideoPlayer videoplayer; private RawImage rawImage; private int currentClipIndex; public Text text_playorpause; public Button button_playorpause; public Button button_pre; public Button button_next; public VideoClip[] videoClips; void Start() { videoplayer = this.GetComponent<VideoPlayer>(); rawImage = this.GetComponent<RawImage>(); currentClipIndex = 0; button_playorpause.onClick.AddListener(OnplayorpauseVideo); button_pre.onClick.AddListener(OnpreVideo); button_next.onClick.AddListener(OnnextVideo); } // Update is called once per frame void Update() { if (videoplayer.texture == null) { return; } rawImage.texture = videoplayer.texture; } private void OnplayorpauseVideo() { if (videoplayer.enabled == true) { if (videoplayer.isPlaying) { videoplayer.Pause(); text_playorpause.text = "播放"; } else if (!videoplayer.isPlaying) { videoplayer.Play(); text_playorpause.text = "暂停"; } } } private void OnpreVideo() { currentClipIndex -= 1; if (currentClipIndex < 0) { currentClipIndex = videoClips.Length - 1; } videoplayer.clip = videoClips[currentClipIndex]; text_playorpause.text = "暂停"; } private void OnnextVideo() { currentClipIndex += 1; currentClipIndex = currentClipIndex % videoClips.Length; videoplayer.clip = videoClips[currentClipIndex]; text_playorpause.text = "暂停"; } }
2.针对不同的函数,选定对应的button
3.添加video player组件,选择要播放的视频
4.添加脚本,实现对视频的进度条及播放时长显示
using UnityEngine; using UnityEngine.UI; using UnityEngine.Video; public class ToPlayVideo : MonoBehaviour { public VideoClip videoClip; // 视频的文件 参数 public Text videoTimeText; // 视频的时间 Text public Text videoNameText; // 视频的名字 Text public Slider videoTimeSlider; // 视频的时间 Slider //定义参数获取VideoPlayer组件和RawImage组件 internal VideoPlayer videoPlayer; private RawImage rawImage; // Use this for initialization void Start() { //获取场景中对应的组件 videoPlayer = this.GetComponent<VideoPlayer>(); rawImage = this.GetComponent<RawImage>(); videoPlayer.clip = videoClip; videoNameText.text = videoClip.name; clipHour = (int)videoPlayer.clip.length / 3600; clipMinute = (int)(videoPlayer.clip.length - clipHour * 3600) / 60; clipSecond = (int)(videoPlayer.clip.length - clipHour * 3600 - clipMinute * 60); videoPlayer.Play(); } // Update is called once per frame void Update() { //如果videoPlayer没有对应的视频texture,则返回 if (videoPlayer.texture == null) { return; } //把VideoPlayerd的视频渲染到UGUI的RawImage rawImage.texture = videoPlayer.texture; ShowVideoTime(); } /// <summary> /// 显示当前视频的时间 /// </summary> private void ShowVideoTime() { // 当前的视频播放时间 currentHour = (int)videoPlayer.time / 3600; currentMinute = (int)(videoPlayer.time - currentHour * 3600) / 60; currentSecond = (int)(videoPlayer.time - currentHour * 3600 - currentMinute * 60); // 把当前视频播放的时间显示在 Text 上 videoTimeText.text = string.Format("{0:D2}:{1:D2}:{2:D2} / {3:D2}:{4:D2}:{5:D2}", currentHour, currentMinute, currentSecond, clipHour, clipMinute, clipSecond); // 把当前视频播放的时间比例赋值到 Slider 上 videoTimeSlider.value = (float)(videoPlayer.time / videoPlayer.clip.length); } /// <summary> /// 当前的 Slider 比例值转换为当前的视频播放时间 /// </summary> private void SetVideoTimeValueChange() { videoPlayer.time = videoTimeSlider.value * videoPlayer.clip.length; } // 当前视频的总时间值和当前播放时间值的参数 private int currentHour; private int currentMinute; private int currentSecond; private int clipHour; private int clipMinute; private int clipSecond; }
5.添加对应控件
6.为了实现进度条对视频播放的拖动快进,在slider控件添加如下脚本
using UnityEngine; using UnityEngine.EventSystems; /// <summary> /// 继承 拖拽接口 /// </summary> public class SliderEvent : MonoBehaviour, IDragHandler { [SerializeField] private ToPlayVideo toPlayVideo; // 视频播放的脚本 // Use this for initialization void Start() { } // Update is called once per frame void Update() { } /// <summary> /// 给 Slider 添加 拖拽事件 /// </summary> /// <param name="eventData"></param> public void OnDrag(PointerEventData eventData) { SetVideoTimeValueChange(); } /// <summary> /// 当前的 Slider 比例值转换为当前的视频播放时间 /// </summary> private void SetVideoTimeValueChange() { toPlayVideo.videoPlayer.time = toPlayVideo.videoTimeSlider.value * toPlayVideo.videoPlayer.clip.length; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。