Unity的VideoPlayer在使用过程中,经常出现各种各样的BUG。在使用过程中,出现了视频衔接黑屏的现象,发布之后,播放黑屏等问题。经过优化后,有了我现在用的播放视频的脚本。
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
public class MyVCR : MonoBehaviour
{
public enum FileLocationType
{
DataPath, StreamingAssets, Resources, URL
}
private double videoLength = 0;
[SerializeField]
private VideoPlayer playerA;
[SerializeField]
private VideoPlayer playerB;
[SerializeField]
private RenderTexture textureA;
[SerializeField]
private RenderTexture textureB;
[SerializeField]
private RawImage RawImage;
private RenderTexture _loadingTexture;
private VideoPlayer _loadingPlayer;
private VideoPlayer LoadingPlayer
{
get
{
return _loadingPlayer;
}
}
private VideoPlayer _playingPlayer;
private VideoPlayer PlayingPlayer
{
get
{
if (LoadingPlayer == playerA)
{
_playingPlayer = playerB;
}
else
{
_playingPlayer = playerA;
}
videoLength = _playingPlayer.length;
return _playingPlayer;
}
}
/// <summary>
/// 初始化
/// </summary>
public void Init()
{
_loadingPlayer = playerB;
if (PlayingPlayer)
{
PlayingPlayer.prepareCompleted += PlayingPlayer_prepareCompleted;
if (LoadingPlayer)
{
LoadingPlayer.prepareCompleted += PlayingPlayer_prepareCompleted;
}
}
RawImage.texture = PlayingPlayer.targetTexture;
}
private void PlayingPlayer_prepareCompleted(VideoPlayer source)
{
SwapPlayers();
}
/// <summary>
/// 按文件名打开视频
/// </summary>
/// <param name="path"></param>
public void OnOpenVideoFile(FileLocationType type, string path)
{
LoadingPlayer.Stop();
switch (type)
{
case FileLocationType.DataPath:
LoadingPlayer.url = Application.dataPath + "/" + path + ".mp4";
break;
case FileLocationType.StreamingAssets:
LoadingPlayer.url = Application.streamingAssetsPath + "/" + path + ".mp4";
break;
case FileLocationType.Resources:
LoadingPlayer.clip = Resources.Load<VideoClip>(path + ".mp4");
break;
case FileLocationType.URL:
LoadingPlayer.url = path;
break;
}
videoLength = LoadingPlayer.length;
PlayVidoe();
}
/// <summary>
/// 按 clip 打开视频
/// </summary>
/// <param name="clip"></param>
public void OnOpenVideoFile(VideoClip clip)
{
LoadingPlayer.clip = clip;
PlayVidoe();
}
private void PlayVidoe()
{
PlayingPlayer.Play();
}
/// <summary>
/// 交换player
/// </summary>
private void SwapPlayers()
{
PlayingPlayer.Pause();
PlayingPlayer.targetTexture.Release();
if (LoadingPlayer == playerA)
{
_loadingPlayer = playerB;
}
else
{
_loadingPlayer = playerA;
}
RawImage.texture = PlayingPlayer.targetTexture;
}
/// <summary>
/// 播放
/// </summary>
public void Play()
{
if (PlayingPlayer.isPlaying == false)
{
PlayingPlayer.Play();
}
}
/// <summary>
/// 暂停
/// </summary>
public void Pause()
{
if (PlayingPlayer.isPlaying)
{
PlayingPlayer.Pause();
}
}
/// <summary>
/// 停止
/// </summary>
public void Stop()
{
PlayingPlayer.Stop();
}
public void Next(double length)
{
if (PlayingPlayer.isPlaying)
{
double l = PlayingPlayer.time;
l += length;
if (l > videoLength)
{
PlayingPlayer.time = videoLength;
}
else
{
PlayingPlayer.time += length;
}
}
}
public void Prev(double length)
{
if (PlayingPlayer.isPlaying)
{
double l = PlayingPlayer.time;
l -= length;
if (l < 0)
{
PlayingPlayer.time = 0;
}
else
{
PlayingPlayer.time -= length;
}
}
}
}