Unity使用RawImage播放视频带有播放暂停功能滑动条可控制快进后退
作者:互联网
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
public class VideoController : MonoBehaviour
{
//设置VideoPlayer、RawImage和当前播放视频索引参数
private VideoPlayer videoPlayer;
private RawImage rawImage;
[SerializeField]
Image myImage;
// private int currentClipIndex;
//设置相关文本和按钮参数以及视频列表
public Text text_PlayOrPause;
public Button button_PlayOrPause;
private void Awake()
{
text_PlayOrPause.text = "暂停";
// Image/pic 在 Assets/Resources/目录下
myImage.sprite = Resources.Load("zanting", typeof(Sprite)) as Sprite;
}
void Start()
{
//获取VideoPlayer和RawImage组件,以及初始化当前视频索引
videoPlayer = this.GetComponent<VideoPlayer>();
rawImage = this.GetComponent<RawImage>();
// currentClipIndex = 0;
//设置相关按钮监听事件
button_PlayOrPause.onClick.AddListener(OnPlayOrPauseVideo);
}
void Update()
{
//没有视频则返回,不播放
if (videoPlayer.texture == null)
{
return;
}
//渲染视频到UGUI上
rawImage.texture = videoPlayer.texture;
}
private void OnPlayOrPauseVideo()
{
//判断视频播放情况,播放则暂停,暂停就播放,并更新相关文本
if (videoPlayer.isPlaying == true)
{
videoPlayer.Pause();
text_PlayOrPause.text = "播放";
// Image/pic 在 Assets/Resources/目录下
myImage.sprite = Resources.Load("bofang", typeof(Sprite)) as Sprite;
}
else
{
videoPlayer.Play();
text_PlayOrPause.text = "暂停";
// Image/pic 在 Assets/Resources/目录下
myImage.sprite = Resources.Load("zanting", typeof(Sprite)) as Sprite;
}
}
}
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;
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();
}
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("{1:D2}:{2: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;
}
目录结构:
RawImage挂载组件:
以下是上图组件具体内容:
滑动条Slider的具体:
脚本中拖入RawImage
using UnityEngine;
using UnityEngine.EventSystems;
/// <summary>
/// 继承 拖拽接口
/// </summary>
public class SliderEvent : MonoBehaviour, IDragHandler
{
[SerializeField]
private ToPlayVideo toPlayVideo; // 视频播放的脚本
/// <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;
}
}
Button按钮控制暂停/播放:
这个我用的是动态加载的img,首先播放的时候是出来“暂停”的模样,当按下后动态加载“播放”样子的img
资源放在
一步步按着来,最终就成功啦。
标签:视频,int,void,videoPlayer,private,RawImage,条可,播放 来源: https://blog.csdn.net/weixin_41814169/article/details/89209106