添加了对待机视频的支持:通过设置isStandByVideo
参数来决定是否播放待机视频。
在OnEnable
方法中调用PlayVideo
方法,根据isStandByVideo
的值选择播放待机视频还是指定的视频。
private void OnEnable() => PlayVideo(isStandByVideo ? "Video/待机视频.mp4" : VideoPath, isStandByVideo);
public void PlayVideo(string path, bool isLoop = true)
{
this._firstPicPath = (null);
if (!this.gameObject.activeInHierarchy)
this.gameObject.SetActive(true);
if ((UnityEngine.Object)this._displayUGUI == (UnityEngine.Object)null)
this._displayUGUI = this.GetComponent<DisplayUGUI>();
if ((UnityEngine.Object)this._mediaPlayer == (UnityEngine.Object)null)
this._mediaPlayer = this.GetComponent<MediaPlayer>();
if (string.IsNullOrWhiteSpace(path))
{
this._mediaPlayer.enabled = false;
}
else
{
this.VideoPath = path;
_mediaPlayer.MediaPath.Path = path;
if (!this._mediaPlayer.enabled)
{
this._displayUGUI.Player = this._mediaPlayer;
this._mediaPlayer.enabled = true;
}
this._mediaPlayer.OpenMedia(MediaPathType.RelativeToStreamingAssetsFolder, path);
this._mediaPlayer.Control.SetLooping(isLoop);
}
}
添加了加载第一帧图片的功能:通过HasFirstPic
方法,在FirstFrameReady
事件触发时,提取视频的第一帧图像,并保存为PNG格式文件。在加载图片时,会先检查是否存在已保存的第一帧图像,如果存在,则直接加载,否则提取并保存。
private async Task LoadByFsAsync(string path)
{
if (string.IsNullOrWhiteSpace(path))
return;
Texture2D texture2D;
if (MediaPlayerGlobal.FirstPicDic.ContainsKey(path))
{
texture2D = MediaPlayerGlobal.FirstPicDic[path];
}
else
{
byte[] result;
using (FileStream SourceStream = File.Open(path, FileMode.Open))
{
result = new byte[SourceStream.Length];
int num = await SourceStream.ReadAsync(result, 0, (int)SourceStream.Length);
}
texture2D = new Texture2D(this._videoWidth, this._videoHeight);
ImageConversion.LoadImage(texture2D, result);
MediaPlayerGlobal.FirstPicDic.Add(path, texture2D);
result = (byte[])null;
}
this._displayUGUI.DefaultTexture = (Texture)texture2D;
this._displayUGUI.color = new Color(1f, 1f, 1f, 1f);
if (!this._mediaPlayer.AutoOpen)
return;
this._mediaPlayer.Play();
}
对视频事件进行了处理:在OnVideoEvent
方法中,根据不同的事件类型进行相应的处理。例如,在FinishedPlaying
事件中,执行PlayEndAc
委托,然后将游戏物体设置为不活动状态。
下面是详细代码:
using RenderHeads.Media.AVProVideo;
using System;
using System.IO;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Events;
[RequireComponent(typeof(DisplayUGUI))]
public class AvProVideoControl : MonoBehaviour
{
public MediaPlayer _mediaPlayer;
private DisplayUGUI _displayUGUI;
public string VideoPath;
private string _firstPicPath;
private int _videoWidth = 1920;
private int _videoHeight = 1080;
private RectTransform _rectTransform;
[SerializeField]
[Header("是否为待机视频")]
public bool isStandByVideo = true;
public Action PlayEndAc;
private void Reset()
{
this._displayUGUI = this.GetComponent<DisplayUGUI>();
this._mediaPlayer = this.GetComponent<MediaPlayer>() ?? this.gameObject.AddComponent<MediaPlayer>();
this._displayUGUI.NoDefaultDisplay = false;
this._displayUGUI.DisplayInEditor = false;
this._displayUGUI.color = new Color(1f, 1f, 1f, 0.0f);
this._displayUGUI.ScaleMode = ScaleMode.StretchToFill;
this._displayUGUI.Player = this._mediaPlayer;
}
private void Awake()
{
this._displayUGUI = this.GetComponent<DisplayUGUI>();
this._mediaPlayer = this.GetComponent<MediaPlayer>();
this._displayUGUI.Player = this._mediaPlayer;
_mediaPlayer.MediaPath.Path = VideoPath;
this._mediaPlayer.Events.AddListener(new UnityAction<MediaPlayer, MediaPlayerEvent.EventType, RenderHeads.Media.AVProVideo.ErrorCode>(this.OnVideoEvent));
_rectTransform = this.GetComponent<RectTransform>();
_rectTransform.sizeDelta = new Vector2(_videoWidth, _videoHeight);
}
private void OnEnable() => PlayVideo(isStandByVideo ? "Video/待机视频.mp4" : VideoPath, isStandByVideo);
public void PlayVideo(Vector2 pos, string path)
{
((Component)this._displayUGUI).transform.localPosition = (Vector3)pos;
this._firstPicPath = (string)null;
if (!this.gameObject.activeInHierarchy)
this.gameObject.SetActive(true);
if ((UnityEngine.Object)this._displayUGUI == (UnityEngine.Object)null)
this._displayUGUI = this.GetComponent<DisplayUGUI>();
if ((UnityEngine.Object)this._mediaPlayer == (UnityEngine.Object)null)
this._mediaPlayer = this.GetComponent<MediaPlayer>();
if (string.IsNullOrWhiteSpace(path))
{
this._mediaPlayer.enabled = false;
}
else
{
this.VideoPath = path;
_mediaPlayer.MediaPath.Path = path;
if (!this._mediaPlayer.enabled)
{
this._displayUGUI.Player = this._mediaPlayer;
this._mediaPlayer.enabled = true;
}
this._mediaPlayer.OpenMedia(MediaPathType.RelativeToStreamingAssetsFolder, path, false);
this._mediaPlayer.Control.SetLooping(true);
}
}
public void PlayVideo(string path, bool isLoop = true)
{
this._firstPicPath = (null);
if (!this.gameObject.activeInHierarchy)
this.gameObject.SetActive(true);
if ((UnityEngine.Object)this._displayUGUI == (UnityEngine.Object)null)
this._displayUGUI = this.GetComponent<DisplayUGUI>();
if ((UnityEngine.Object)this._mediaPlayer == (UnityEngine.Object)null)
this._mediaPlayer = this.GetComponent<MediaPlayer>();
if (string.IsNullOrWhiteSpace(path))
{
this._mediaPlayer.enabled = false;
}
else
{
this.VideoPath = path;
_mediaPlayer.MediaPath.Path = path;
if (!this._mediaPlayer.enabled)
{
this._displayUGUI.Player = this._mediaPlayer;
this._mediaPlayer.enabled = true;
}
this._mediaPlayer.OpenMedia(MediaPathType.RelativeToStreamingAssetsFolder, path);
this._mediaPlayer.Control.SetLooping(isLoop);
}
}
public void PlayVideo()
{
if (this.VideoPath == null)
return;
this._firstPicPath = (string)null;
if (!this.gameObject.activeInHierarchy)
this.gameObject.SetActive(true);
if (this._displayUGUI == null)
this._displayUGUI = this.GetComponent<DisplayUGUI>();
if (this._mediaPlayer == null)
this._mediaPlayer = this.GetComponent<MediaPlayer>();
if (string.IsNullOrWhiteSpace(this.VideoPath))
{
this._mediaPlayer.enabled = false;
}
else
{
if (!this._mediaPlayer.enabled)
{
this._displayUGUI.Player = this._mediaPlayer;
this._mediaPlayer.enabled = true;
}
this._mediaPlayer.OpenMedia(MediaPathType.RelativeToStreamingAssetsFolder, this.VideoPath);
this._mediaPlayer.Control.SetLooping(true);
}
}
public void StopVideo()
{
if (_mediaPlayer == null)
return;
this._mediaPlayer.Stop();
this._mediaPlayer.enabled = false;
if (this._displayUGUI == null)
return;
this._displayUGUI.Player = null;
this._displayUGUI.DefaultTexture = null;
this._displayUGUI.color = new Color(1f, 1f, 1f, 0.0f);
}
public async void SetShowInfo(string picPath, bool playVideo = false, string videoPath = null)
{
if (!this.transform.gameObject.activeInHierarchy)
this.transform.gameObject.SetActive(true);
await this.LoadByFsAsync(picPath);
if (!playVideo)
this._mediaPlayer.enabled = false;
else
this.PlayVideo(videoPath);
}
private async Task LoadByFsAsync(string path)
{
if (string.IsNullOrWhiteSpace(path))
return;
Texture2D texture2D;
if (MediaPlayerGlobal.FirstPicDic.ContainsKey(path))
{
texture2D = MediaPlayerGlobal.FirstPicDic[path];
}
else
{
byte[] result;
using (FileStream SourceStream = File.Open(path, FileMode.Open))
{
result = new byte[SourceStream.Length];
int num = await SourceStream.ReadAsync(result, 0, (int)SourceStream.Length);
}
texture2D = new Texture2D(this._videoWidth, this._videoHeight);
ImageConversion.LoadImage(texture2D, result);
MediaPlayerGlobal.FirstPicDic.Add(path, texture2D);
result = (byte[])null;
}
this._displayUGUI.DefaultTexture = (Texture)texture2D;
this._displayUGUI.color = new Color(1f, 1f, 1f, 1f);
if (!this._mediaPlayer.AutoOpen)
return;
this._mediaPlayer.Play();
}
private void LoadByFsAsync(Texture2D texture2d)
{
this._displayUGUI.DefaultTexture = (Texture)texture2d;
this._displayUGUI.color = new Color(1f, 1f, 1f, 1f);
if (!this._mediaPlayer.AutoStart)
return;
this._mediaPlayer.Play();
}
private async void HasFirstPic()
{
if (string.IsNullOrEmpty(this._firstPicPath))
{
string fileName = Path.GetFileName(this._mediaPlayer.MediaPath.Path);
string path2 = this.VideoPath.Replace(fileName, "");
string str1 = Path.Combine((Application.streamingAssetsPath), path2);
string extension = Path.GetExtension(this._mediaPlayer.MediaPath.Path);
string str2 = fileName.Replace(extension, "") + "第一帧.png";
this._firstPicPath = str1 + "/" + str2;
if (!File.Exists(this._firstPicPath))
{
Texture2D frame;
if (MediaPlayerGlobal.FirstPicDic.ContainsKey(this._firstPicPath))
{
frame = MediaPlayerGlobal.FirstPicDic[this._firstPicPath];
}
else
{
frame = this._mediaPlayer.ExtractFrame(new Texture2D(this._mediaPlayer.Info.GetVideoWidth(), this._mediaPlayer.Info.GetVideoHeight(), TextureFormat.RGBA32, false), Helper.ConvertFrameToTimeSeconds(1, this._mediaPlayer.Info.GetVideoFrameRate()));
MediaPlayerGlobal.FirstPicDic.Add(this._firstPicPath, frame);
File.WriteAllBytes(this._firstPicPath, frame.EncodeToPNG());
}
this.LoadByFsAsync(frame);
}
else
await this.LoadByFsAsync(this._firstPicPath);
}
else
await LoadByFsAsync(_firstPicPath);
}
public void OnVideoEvent(MediaPlayer mp, MediaPlayerEvent.EventType et, RenderHeads.Media.AVProVideo.ErrorCode er)
{
AvProVideoControl avProVideoControl = this;
switch (et)
{
case MediaPlayerEvent.EventType.FirstFrameReady:
avProVideoControl.HasFirstPic();
avProVideoControl._displayUGUI.color = new Color(1f, 1f, 1f, 1f);
break;
case MediaPlayerEvent.EventType.FinishedPlaying:
Debug.Log((object)"当非循环视频播放完毕时触发");
Action playEndAc1 = avProVideoControl.PlayEndAc;
if (playEndAc1 != null)
playEndAc1();
avProVideoControl.gameObject.SetActive(false);
break;
case MediaPlayerEvent.EventType.EndLoop:
Debug.Log((object)"当视频结束并处于循环模式时触发");
Action playEndAc2 = avProVideoControl.PlayEndAc;
if (playEndAc2 == null)
break;
playEndAc2();
break;
}
}
private void OnDisable()
{
this._displayUGUI.color = new Color(1f, 1f, 1f, 0.0f);
this._mediaPlayer.Stop();
}
}