using RenderHeads.Media.AVProVideo;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.Video;
public class AudioVideoMgr : MonoBehaviour
{
#region
public const string BLUE_SIDE_BOND = "蓝方羁绊.mp4";
public const string RED_SIDE_BOND = "红方羁绊.mp4";
public const string SAGITTARIUS_ATTACK = "射手攻击.mp4";
public const string NORMAL_ATTACK = "普通攻击.mp4";
public const string TREATMENT = "治疗.mp4";
public const string SHIELD= "保护盾.mp4";
public const string WEEKLY_LEADERBOARD_POINTS = "周榜视频.mp4";
#endregion
// 单例模式确保全局唯一访问点
public static AudioVideoMgr Instance { get; private set; }
// AVPro Video 核心组件
[Header("AVPro Video 配置")]
public MediaPlayer mediaPlayerAlpha; // 视频播放器预制体
public MediaPlayer mediaPlayerAlpha1; // 视频播放器预制体
public MediaPlayer mediaPlayerNoAlpha; // 视频播放器预制体
public MediaPlayer mediaPlayerNoAlpha1; // 视频播放器预制体
private MediaPlayer currentMediaPlayer; // 当前播放器
private int curruntIndex1 = 0;
private int curruntIndex2 = 0;
[SerializeField]
private DisplayUGUI displayUGUI; // 视频显示组件
private DisplayUGUI currentDisplayUGUI; // 当前显示的视频播放器
private DisplayUGUI oldDisplayUGUI; // 当前显示的视频播放器
// 音频管理部分(保留原有音频功能)
[Header("音频配置")]
public AudioSource bgmSource;
public AudioSource sfxSource;
private Action onFinish; // 视频播放结束回调
private Action onNoLoopFinish; // 不循环视频播放结束回调
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
private void Start()
{
mediaPlayerNoAlpha.Events.AddListener(OnVideoEvent);
mediaPlayerNoAlpha.Play();
mediaPlayerNoAlpha1.Events.AddListener(OnVideoEvent);
mediaPlayerNoAlpha1.Play();
mediaPlayerAlpha.Events.AddListener(OnVideoEvent);
mediaPlayerAlpha.Play();
mediaPlayerAlpha1.Events.AddListener(OnVideoEvent);
mediaPlayerAlpha1.Play();
}
/**************** 视频管理功能 ****************/
// 初始化视频播放器
//public MediaPlayer CreateVideoPlayer(string playerId)
//{
// 播放普通不透明视频
public void PlayNormalVideo(string path,DisplayUGUI displayUGUI=null,bool isLoop=false,bool isUrl = false)
{
if (displayUGUI)
{
oldDisplayUGUI = currentDisplayUGUI;
currentDisplayUGUI = displayUGUI;
}
else
{
currentDisplayUGUI = this.displayUGUI;
}
if (curruntIndex1 == 0)
{
PlayVideoInternal(path, mediaPlayerNoAlpha, isUrl);
mediaPlayerNoAlpha.Loop = isLoop;
curruntIndex1++;
}
else if(curruntIndex1 == 1)
{
PlayVideoInternal(path, mediaPlayerNoAlpha1, isUrl);
mediaPlayerNoAlpha1.Loop = isLoop;
curruntIndex1 =0;
}
}
// 播放透明视频(可选择左右/上下分离)
public void PlayTransparentVideo(string path, DisplayUGUI displayUGUI = null, bool isloop=false,bool isUrl = false )
{
if (displayUGUI)
{
oldDisplayUGUI = currentDisplayUGUI;
currentDisplayUGUI = displayUGUI;
}
else
{
currentDisplayUGUI = this.displayUGUI;
}
mediaPlayerAlpha.Loop = isloop;
if (curruntIndex2 == 0)
{
PlayVideoInternal(path, mediaPlayerAlpha, isUrl);
curruntIndex2++;
}
else if (curruntIndex2 == 1)
{
PlayVideoInternal(path, mediaPlayerAlpha1, isUrl);
curruntIndex2 = 0;
}
}
public void PlayTransparentVideo(string path, DisplayUGUI displayUGUI,Action action)
{
if (displayUGUI)
{
oldDisplayUGUI = currentDisplayUGUI;
currentDisplayUGUI = displayUGUI;
}
else
{
currentDisplayUGUI = this.displayUGUI;
}
onNoLoopFinish=action;
if (curruntIndex2 == 0)
{
PlayVideoInternal(path, mediaPlayerAlpha,false);
curruntIndex2++;
}
else if (curruntIndex2 == 1)
{
PlayVideoInternal(path, mediaPlayerAlpha1,false);
curruntIndex2 = 0;
}
}
private Coroutine playVideoCoroutine;
public void PlayTransparentVideo(string path, bool isplay ,DisplayUGUI displayUGUI = null, bool isloop = false, bool isUrl = false)
{
Debug.Log(mediaPlayerAlpha.Control.IsPlaying());
if (displayUGUI)
{
oldDisplayUGUI = currentDisplayUGUI;
currentDisplayUGUI = displayUGUI;
}
else
{
currentDisplayUGUI = this.displayUGUI;
}
if (isplay)
{
if (playVideoCoroutine != null)
{
StopCoroutine(playVideoCoroutine);
}
playVideoCoroutine=StartCoroutine(PlayVideo(path, isloop));
return;
}
mediaPlayerAlpha.Loop = isloop;
PlayVideoInternal(path, mediaPlayerAlpha, isUrl);
}
private void Update()
{
}
IEnumerator PlayVideo(string path, bool isloop)
{
yield return new WaitForSeconds(0.3f);
if (mediaPlayerAlpha.Control.IsPlaying())
{
onFinish = () =>
{
PlayerNextVideo(path, isloop);
};
}
else
{
PlayerNextVideo(path,isloop);
}
}
void PlayerNextVideo(string path,bool isloop)
{
mediaPlayerAlpha.Loop = isloop;
PlayVideoInternal(path, mediaPlayerAlpha,false);
}
// 内部播放逻辑
private void PlayVideoInternal(string path,MediaPlayer mediaPlayer, bool isUrl)
{
if (mediaPlayer == null)
{
Debug.LogError("视频播放器未初始化");
return;
}
StopVideo(); // 停止之前的视频
currentDisplayUGUI.CurrentMediaPlayer = null; // 清空当前播放器
currentDisplayUGUI.CurrentMediaPlayer= mediaPlayer; // 设置当前播放器
currentDisplayUGUI.gameObject.SetActive(false);
// 设置播放源
MediaPath mediaPath = isUrl
? new MediaPath(path, MediaPathType.AbsolutePathOrURL)
: new MediaPath(path, MediaPathType.RelativeToStreamingAssetsFolder);
mediaPlayer.OpenMedia(mediaPath);
mediaPlayer.Play();
currentMediaPlayer= mediaPlayer;
currentDisplayUGUI.gameObject.SetActive(true);
}
// 视频事件回调
private void OnVideoEvent(MediaPlayer mp, MediaPlayerEvent.EventType eventType, ErrorCode error)
{
//Debug.Log("重播1");
if (mp.Loop)
{
EventMvpOnLoop eventMvpOnLoop = new EventMvpOnLoop();
EventSystem.Instance.Trigger(eventMvpOnLoop);
}
onNoLoopFinish?.Invoke();
onNoLoopFinish = null;
switch (eventType)
{
case MediaPlayerEvent.EventType.FinishedPlaying:
//ResumeBGM(); // 视频结束恢复背景音乐
SkillHandler.OnFinsh?.Invoke();
//SkillHandler.OnFinsh?.Invoke();
//SkillHandler.OnFinsh?.Invoke();
//SkillHandler.OnFinsh?.Invoke();
//SkillHandler.OnFinsh?.Invoke();
if (currentMediaPlayer != null)
{
currentDisplayUGUI.CurrentMediaPlayer = null; // 清空当前播放器
currentDisplayUGUI.gameObject.SetActive(false); // 隐藏视频显示组件
}
onFinish?.Invoke();
onFinish = null;
break;
case MediaPlayerEvent.EventType.Error:
Debug.LogError($"视频播放错误: {error}");
break;
}
}
/**************** 音频管理功能 ****************/
// 保留原有音频功能并增强
public void SetVideoVolume(float volume, bool isAlphaVideo = true)
{
if (isAlphaVideo)
{
mediaPlayerAlpha.Control.SetVolume (Mathf.Clamp01(volume));
}
else
{
mediaPlayerNoAlpha.Control.SetVolume(0);
Debug.Log("设置普通视频音量" + volume);
}
}
public void PlayBGM(AudioClip clip, float volume = 0.8f)
{
bgmSource.clip = clip;
bgmSource.volume = volume;
bgmSource.loop = true;
bgmSource.Play();
}
public void PauseBGM() => bgmSource.Pause();
public void ResumeBGM() => bgmSource.UnPause();
public void PlaySFX(AudioClip clip, float volume = 1f)
{
sfxSource.PlayOneShot(clip, volume);
}
/**************** 资源管理 ****************/
public void StopVideo()
{
//mediaPlayerAlpha.Stop();
//mediaPlayerNoAlpha.Stop();
//mediaPlayerNoAlpha1.Stop();
if (currentMediaPlayer != null)
{
currentMediaPlayer.Stop();
}
if (currentDisplayUGUI != null)
{
currentDisplayUGUI.gameObject.SetActive(false);
}
if (oldDisplayUGUI != null)
{
oldDisplayUGUI.gameObject.SetActive(false);
}
displayUGUI.gameObject.SetActive(false);
}
public void StopVideoAll()
{
mediaPlayerAlpha.Stop();
mediaPlayerNoAlpha.Stop();
mediaPlayerNoAlpha1.Stop();
mediaPlayerAlpha1.Stop();
if (currentMediaPlayer != null)
{
currentMediaPlayer.Stop();
}
}
}
如何同时播放显示多个视频,有可能10个以上
最新发布