视频播放器
- 首先先创建一个RawImage组件 然后在这下面创建一个Video Player
- 然后再新建一个Render Texture 把这个赋值到RawImage和Video Player上面
- 然后把视频放到Video Player上面这样就可以播放视频了
- 然后呢可以开始给视频添加播放暂停按钮和前后按钮了
这里创建三个按钮
给这三个按钮取好名字
然后再创建一个XiaYiGe的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
public class XiaYiGe : MonoBehaviour
{
//设置VideoPlayer、RawImage和当前播放视频索引参数
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;
private void Start()
{
//获取VideoPlayr和RawImage组件,以及初始化当前视频索引
videoPlayer = this.GetComponent<VideoPlayer>();
rawImage = this.GetComponent<RawImage>();
///<summary>
/// 初始化当前的视频索引,也不晓得干嘛的
/// </summary>
currentClipIndex = 0;
//设置相关按钮监听事件
button_PlayOrPause.onClick.AddListener(OnplayOrPauseVideo);
button_Next.onClick.AddListener(onNextVideo);
button_Pre.onClick.AddListener(OnPreVideo);
}
private void Update()
//这里是为了防止空指针异常 判断贴图是不是为空 空就跳出,然后重新把贴图给这个播放器
{
if(videoPlayer.texture == null)
{
return;
}
rawImage.texture = videoPlayer.texture;
}
private void OnplayOrPauseVideo()
{
//这里是判断视频的播放情况,播放的情况下就暂停,反之;
//然后更新相关文本
if(videoPlayer.isPlaying == true)
{
videoPlayer.Pause();
text_PlayOrPause.text = "播放";
}
else
{
videoPlayer.Play();
text_PlayOrPause.text = "暂停";
}
}
///<summary>
///切换上一个视频
///</summary>
private void OnPreVideo()
{
currentClipIndex -= 1;//视频索引自减,减一播放上一个视频,进行避免越界操作
if (currentClipIndex < 0)
{
currentClipIndex = videoClips.Length - 1;//报空的话就给他赋值当前播放的视频几减一
}
videoPlayer.clip = videoClips[currentClipIndex];
text_PlayOrPause.text = "暂停";
}
private void onNextVideo()
{//先自增 然后video的播放第几个就等于刚刚自增的数据加一 然后文本输出
currentClipIndex += 1;
currentClipIndex = currentClipIndex % videoClips.Length;//求余也是为了等下遍历完从头开始! 不然遍历完就会报错
videoPlayer.clip = videoClips[currentClipIndex];
text_PlayOrPause.text = "暂停";
}
然后再把这个代码加到RawImage中去 文字可以添加也可以不添加,这三个按钮赋值给这三个然后下面的Video Clips就是定义放几个视频,然后再把视频一个一个添加进去
然后按钮解决了 之后就是解决 滑动条的问题;
也是要先创建两个Text文本我这里是取的这两个名字
先创建两个脚本 附上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
public class ToPlayVideo : MonoBehaviour
{
public VideoClip videoClip; //视频的文件 参数
public Text videoTimeText; //视频的文件 txt文件
public Slider videoTimeSlider; //视频的时间 Slider
public Text videoNmaeText; //视频的名字
//定义参数获取VideoPlayer组件和RawImage组件
public VideoPlayer videoPlayer;
private RawImage rawImage;
void Start()
{
//开始获取场景中的组件
videoPlayer = this.GetComponent<VideoPlayer>();
rawImage = this.GetComponent<RawImage>();
videoPlayer.clip = videoClip;//这是当前播放的视频 去canvas个面板可以找到
videoNmaeText.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();
}
void Update()
{
//如果videoPlayer没有对应的视频texture,则返回
if(videoPlayer.texture == null)
{
return;
}
rawImage.texture = videoPlayer.texture;
ShowVideoTime();
}
/// <summary>
/// 显示当前视频时间
/// </summary>
private void ShowVideoTime()
{
//当前0视频的播放时间
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上 string.Format????
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;
}
然后是第二个脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class SliderEvent : MonoBehaviour,IDragHandler
{
[SerializeField] private ToPlayVideo toPlayVideo;//私有化定义变量 视频播放的脚本
/// <summary>
/// 给Slider添加拖拽事件
/// </summary>
/// <param name="eventData"></param>
public void OnDrag(PointerEventData eventData)
{
Debug.Log("sdfdsf");
SetVideoTimeValueChang();
}
/// <summary>
/// 当前的Slider比例值转换为当前播放视频的时间
/// </summary>
private void SetVideoTimeValueChang()
{
toPlayVideo.videoPlayer.time = toPlayVideo.videoTimeSlider.value * toPlayVideo.videoPlayer.clip.length;
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
然后就按照脚本放
第一个拖到RawImage里面去
然后放好里面的数据
第二个放到Slider中去 把刚刚的代码赋值到这里面
然后简单的视频滑动也完成了