最新解决办法:unityAVProVideo(Dome已做好)-C#文档类资源-CSDN文库
下面方法 已抛弃:
播放视频 视频资源放在:StreamingAssets 文件下
using RenderHeads.Media.AVProVideo;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
///
/// 控制 视频播放 类
///
public class UIVideo : MonoSingleton_
{
public GameObject MaskGo;
///
/// 姓名
///
public Text Title;
public Button Play;
public Button Pause;
public MediaPlayer mediaPlayer;
[HideInInspector]
public string videoname;
[HideInInspector]
public bool isplay;
public Slider m_processSlider;
public Text m_videoTimeTex;
///
/// 返回按钮
///
public Button ReturnBtn;
public virtual void Awake()
{
Test
//videoname = "AVProVideoSamples/AlphaLeftRight";
//GetVideoUrl();
}
void Start()
{
ReturnBtn.onClick.AddListener(OnReturnBtn);
Play.onClick.AddListener(OnPlayVideo);
Pause.onClick.AddListener(OnPauseVideo);
//注册视频播放触发事件
mediaPlayer.Events.AddListener(MediaEventHandler);
//注册视频播放进度条值改变触发事件
m_processSlider.onValueChanged.AddListener(OnProcessSliderChange);
}
void Update()
{
if (isplay)
{
//时刻更新播放进度方法
// DoUpdateVideoProcess();
//时刻更新播放时间显示方法
UpdateTimeText();
}
}
///
/// 返回上一级
///
public void OnReturnBtn()
{
UIBackManage.Instance.OnBack();
}
///
/// 获取视频资源(可更改参数进行切换视频操作)
///
public virtual void GetVideoUrl()
{
//通过插件中的方法加载(参数为:1.加载路径格式(与面板上相对应)2.加载的文件名 3.默认是否开始播放)
mediaPlayer.OpenVideoFromFile(MediaPlayer.FileLocation.RelativeToStreamingAssetsFolder, videoname + ".mp4", true);
mediaPlayer.Play();
isplay = true;
}
///
/// 视频播放时间触发
///
///
///
///
private void MediaEventHandler(MediaPlayer arg0, MediaPlayerEvent.EventType arg1, ErrorCode arg2)
{
switch (arg1)
{
case MediaPlayerEvent.EventType.Closing:
Debug.Log("关闭播放器触发");
break;
case MediaPlayerEvent.EventType.Error:
Debug.Log("报错误时触发");
break;
case MediaPlayerEvent.EventType.FinishedPlaying://注意:如果视频设置为循环播放模式,则不触发此项
Debug.Log("播放完成触发");
isplay = false;
Pause.gameObject.SetActive(false);//暂停播放
Play.gameObject.SetActive(true);//播放
break;
case MediaPlayerEvent.EventType.FirstFrameReady:
Debug.Log("准备完触发");
break;
case MediaPlayerEvent.EventType.MetaDataReady:
Debug.Log("媒体数据准备准备中触发");
break;
case MediaPlayerEvent.EventType.ReadyToPlay:
Debug.Log("准备去播放触发");
break;
case MediaPlayerEvent.EventType.Started://注意:每暂停之后的开始播放都会触发一次
Debug.Log("开始播放触发");
isplay = true;
Pause.gameObject.SetActive(true);//暂停播放
Play.gameObject.SetActive(false);//播放
break;
default:
// Debug.Assert(false);
break;
}
}
///
/// 播放
///
public virtual void OnPlayVideo()
{
isplay = true;
mediaPlayer.Control.Play();
}
///
/// 暂停
///
public virtual void OnPauseVideo()
{
isplay = false;
mediaPlayer.Control.Pause();
}
///
/// 时刻更新视频进度到滑动条上
///
void DoUpdateVideoProcess()
{
//获取当前播放时长
float tCurrentTime = mediaPlayer.Control.GetCurrentTimeMs();
//获取视频总长度
float tVideoTime = mediaPlayer.Info.GetDurationMs();
//计算出对应的播放进度赋值给显示播放进度的进度条
m_processSlider.value = tCurrentTime / tVideoTime;
}
///
/// 更新播放进度的时间显示
///
void UpdateTimeText()
{
//对当前播放时间转换时间格式
//转化为 秒
int tCurrentSeconds = (int)mediaPlayer.Control.GetCurrentTimeMs() / 1000;
//获取当前分数
int tCurrentMin = tCurrentSeconds / 60;
//重新赋值剩余多少秒
tCurrentSeconds = tCurrentSeconds % 60;
string tCurrentSecondStr = tCurrentSeconds < 10 ? "0" + tCurrentSeconds : tCurrentSeconds.ToString();
//对总时间转化时间格式
//转化为秒
int tVideoTimeSeconds = (int)mediaPlayer.Info.GetDurationMs() / 1000;
//获取总的分数
int tVideoTimeMin = tVideoTimeSeconds / 60;
//重新复制剩余多少秒
tVideoTimeSeconds = tVideoTimeSeconds % 60;
string tVideoTimeSecondStr = tVideoTimeSeconds < 10 ? "0" + tVideoTimeSeconds : tVideoTimeSeconds.ToString();
//拼接一下时间显示字符串
string tTime = string.Format("{0}:{1}/{2}:{3}", tCurrentMin, tCurrentSecondStr, tVideoTimeMin, tVideoTimeSecondStr);
//string tTime = string.Format("{0}:{1}/{2}:{3}", tCurrentMin, tCurrentSeconds, tVideoTimeMin, tVideoTimeSeconds);
//string tTime = string.Format("{0}:{1}/{2}:{3}", tCurrentMin, tCurrentSeconds, tVideoTimeMin, tVideoTimeSeconds);
m_videoTimeTex.text = tTime; ;
}
///
/// 视频播放进度条值改变触发
///
///
void OnProcessSliderChange(float value)
{
//获取视频总长度
float tVideoTime = mediaPlayer.Info.GetDurationMs();
//当前视频的时间
float tCurrentTime = m_processSlider.value * tVideoTime;
//将视频时间调到对应的节点
mediaPlayer.Control.Seek(tCurrentTime);
}
///
/// 设置状态
///
public void SetSteat(bool bo, string videoName_= "AVProVideoSamples/纯度假型酒店")
{
if (bo)
{
MaskGo.SetActive(bo);
videoname = videoName_;
GetVideoUrl();
}
else
{
OnPauseVideo();
MaskGo.SetActive(bo);
}
}
}