1、导入AVPro Video插件
链接:https://pan.baidu.com/s/1Dsf_mO3vSvdLvU-gNX2ulQ
提取码:967i
2、创建一个视频管理和一块电影布
3、相关设置
4、视频播放案例
4.1、效果演示
4.2、总体预览
4.3、代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RenderHeads.Media.AVProVideo; //引入命名空间
using UnityEngine.UI;
public class VideoControl : MonoBehaviour
{
private MediaPlayer m_MediaPlayer;
public Slider timeSlider; //进度条
public Text timeText; //显示时长
public Toggle isMuteToggle; //是否静音
public Toggle isLoopToggle; //是否循环
public Slider volumeSlider; //音量
public Dropdown rateDropdown; //速率
public string[] videoFiles = { "One.mp4", "Two.mp4", "Three.mp4" };
private int videoIndex = 0;
private void Awake()
{
m_MediaPlayer = GameObject.Find("MediaPlayer").GetComponent<MediaPlayer>(); //获取组件
LoadMovie(videoFiles[videoIndex]);
timeSlider.onValueChanged.AddListener(TimeSliderValueChange); //当进度条值改变时,给进度条注册事件
isMuteToggle.isOn = false; //初始设置不静音
isLoopToggle.isOn = false; //初始设置不循环
volumeSlider.value = 1; //初始音量为1
volumeSlider.onValueChanged.AddListener(VolumeSliderValueChange); //音量事件
rateDropdown.value = 2; //初始速率为1
rateDropdown.onValueChanged.AddListener(RateDropdownValueChange); //速率事件
m_MediaPlayer.Events.AddListener(EventHandle); //事件系统
}
private void Update()
{
timeText.text = ConverTime(GetMovieCurrentTime()) + " / " + ConverTime(GetMovieAllTime()); //显示时长
//滑动条不显示,坐标值出现NaN,是由于Slider的Value计算不能除于0,除数为0会造成这种问题
if (m_MediaPlayer && m_MediaPlayer.Info != null && m_MediaPlayer.Info.GetDurationMs() > 0f)
{
float time = m_MediaPlayer.Control.GetCurrentTimeMs();
float duration = m_MediaPlayer.Info.GetDurationMs();
float d = Mathf.Clamp(time / duration, 0.0f, 1.0f);
timeSlider.value = d;
}
if (isMuteToggle.isOn)
MuteVolume(true);
else
MuteVolume(false);
if (isLoopToggle.isOn)
SetLoop(true);
else
SetLoop(false);
AutoPlayNext();
}
public void LoadMovie(string path) //加载视频
{
m_MediaPlayer.OpenVideoFromFile(MediaPlayer.FileLocation.RelativeToStreamingAssetsFolder, path, false); //最后一个参数是设置开头不自动播放
}
public void PlayMovie() //播放视频
{
m_MediaPlayer.Control.Play();
}
public void PauseMovie() //暂停视频
{
m_MediaPlayer.Control.Pause();
}
public void RewindMovie() //重播视频
{
m_MediaPlayer.Control.Rewind();
PlayMovie();
rateDropdown.value = 2; //重置后速率为1
}
public void SetLoop(bool isLoop) //设置循环
{
m_MediaPlayer.Control.SetLooping(isLoop);
}
public void SetPlayBackRate(float playRate) //设置播放速率,-1为倒放
{
m_MediaPlayer.Control.SetPlaybackRate(playRate);
}
float[] rates = new float[] { 0.5f, 0.75f, 1.0f, 1.25f, 1.5f, 2.0f };
public void RateDropdownValueChange(int index) //选择速率
{
SetPlayBackRate(rates[index]);
}
public float GetMovieAllTime() //获取视频总时长
{
return m_MediaPlayer.Info.GetDurationMs(); //以毫秒为单位,1秒=1000毫秒
}
public float GetMovieCurrentTime() //获取已经播放了的视频时间
{
return m_MediaPlayer.Control.GetCurrentTimeMs();
}
public void SeekMoviePos(float value) //定位视频播放位置
{
float toTime = value * GetMovieAllTime();
m_MediaPlayer.Control.Seek(toTime);
}
public void TimeSliderValueChange(float value) //进度条事件
{
SeekMoviePos(value);
}
public string ConverTime(float time) //时间转换
{
int s = (int)(time / 1000f); //秒
int m = s / 60; //分
int h = m / 60; //小时
s = s % 60;
if (h == 0)
return m.ToString("D2") + ":" + s.ToString("D2");
else
return h.ToString("D2") + ":" + m.ToString("D2") + ":" + s.ToString("D2");
}
public void SetVolume(float value) //设置音量
{
m_MediaPlayer.Control.SetVolume(value);
}
public void VolumeSliderValueChange(float value) //音量调节
{
SetVolume(value);
}
public void MuteVolume(bool isMute) //是否静音
{
m_MediaPlayer.Control.MuteAudio(isMute);
}
public void OnUpButtonClick() //上一首
{
videoIndex--;
if (videoIndex < 0)
videoIndex = videoFiles.Length - 1;
LoadMovie(videoFiles[videoIndex]);
PlayMovie();
}
public void OnNextButtonClick() //下一首
{
videoIndex++;
if (videoIndex >= videoFiles.Length)
videoIndex = 0;
LoadMovie(videoFiles[videoIndex]);
PlayMovie();
}
public void AutoPlayNext() //播放完自动下一首
{
if (m_MediaPlayer.Control.IsFinished())
{
OnNextButtonClick();
}
}
public void EventHandle(MediaPlayer arg0, MediaPlayerEvent.EventType eventType, ErrorCode arg1) //事件
{
switch (eventType)
{
case MediaPlayerEvent.EventType.MetaDataReady:
break;
case MediaPlayerEvent.EventType.ReadyToPlay:
break;
case MediaPlayerEvent.EventType.Started:
Debug.Log("视频开始播放!");
break;
case MediaPlayerEvent.EventType.FirstFrameReady:
break;
case MediaPlayerEvent.EventType.FinishedPlaying:
Debug.Log("视频播放完毕!");
break;
case MediaPlayerEvent.EventType.Closing:
break;
case MediaPlayerEvent.EventType.Error:
break;
case MediaPlayerEvent.EventType.SubtitleChange:
break;
case MediaPlayerEvent.EventType.Stalled:
break;
case MediaPlayerEvent.EventType.Unstalled:
break;
case MediaPlayerEvent.EventType.ResolutionChanged:
break;
case MediaPlayerEvent.EventType.StartedSeeking:
break;
case MediaPlayerEvent.EventType.FinishedSeeking:
break;
case MediaPlayerEvent.EventType.StartedBuffering:
break;
case MediaPlayerEvent.EventType.FinishedBuffering:
break;
case MediaPlayerEvent.EventType.PropertiesChanged:
break;
case MediaPlayerEvent.EventType.PlaylistItemChanged:
break;
case MediaPlayerEvent.EventType.PlaylistFinished:
break;
default:
break;
}
}
}
4.4、优化拖动进度条产生的杂音
4.5、遇到的问题
滑动条不显示,坐标值出现NaN,是由于Slider的Value计算不能除于0,除数为0会造成这种问题文章来源:https://www.toymoban.com/news/detail-503530.html
4.6、项目链接
链接:https://pan.baidu.com/s/13PnJNk7h8CWg5Z_zK2gi2Q
提取码:1ua1文章来源地址https://www.toymoban.com/news/detail-503530.html
到了这里,关于Unity插件-AVPro Video 使用案例之视频播放的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!