創建視頻播放器?
- 在Hierarchy面板中右鍵創建:Video > AVPro Video - MediaPlayer
- 創建后會生成一個MediaPlayer對象,用于控制視頻播放
添加視頻資源
- 將視頻文件放入項目的StreamingAssets文件夾下
- 在MediaPlayer組件的設置中選擇要播放的視頻文件
在UI上顯示視頻?
- 創建Canvas對象
- 在Canvas下添加DisplayUGUI組件用于將視頻渲染到UI
- 將MediaPlayer拖拽到DisplayUGUI組件中
unity avpro切換視頻播放
核心思路,需要切換視頻時。將當前視頻A截一幀圖片pic顯示。播放器加載第二個視頻,等第二個視頻B事件MediaPlayerEvent.EventType.FirstFrameReady,延遲一幀將圖片pic隱藏即可。
實現簡單影游根據選項跳轉不同視頻播放
主要代碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using RenderHeads.Media.AVProVideo;
using TMPro;
using System;public class Level : MonoBehaviour
{public MediaPlayer mediaPlayer;public string levelID; // 關卡唯一標識public Button op_btn1; // 操作按鈕1public Button op_btn2;public TextMeshProUGUI op_btn1_text;public TextMeshProUGUI op_btn2_text;public LevelNode levelNode;public float targetTime = 0.0f;public bool hasTriggered = false;public UnityEngine.UI.RawImage displayImage;// Start is called before the first frame updatevoid Start(){mediaPlayer.AutoStart = false;mediaPlayer.Events.AddListener(OnVideoEvent);op_btn1.onClick.AddListener(OnButtonClick_op1);op_btn2.onClick.AddListener(OnButtonClick_op2);HideOptionsUI();Invoke("test", 1.0f); // 延遲1秒顯示操作按鈕displayImage.gameObject.SetActive(false);}// Update is called once per framevoid Update(){}void FixedUpdate(){CheckCurrentTime();}void OnButtonClick_op1(){Debug.Log("Button 1 clicked in level: " + levelID);mediaPlayer.Pause();// 在這里添加按鈕1的操作邏輯FinishLevel(levelNode.op1_level);}void OnButtonClick_op2(){Debug.Log("Button 2 clicked in level: " + levelID);// 在這里添加按鈕2的操作邏輯FinishLevel(levelNode.op2_level);}void ShowOptionsUI(){op_btn1_text.text = levelNode.op1;op_btn2_text.text = levelNode.op2;op_btn1.GetComponent<RectTransform>().localPosition = new Vector3(levelNode.op1_x, levelNode.op1_y, 0);op_btn2.GetComponent<RectTransform>().localPosition = new Vector3(levelNode.op2_x, levelNode.op2_y, 0);op_btn1.gameObject.SetActive(true);op_btn2.gameObject.SetActive(true);}void HideOptionsUI(){op_btn1.gameObject.SetActive(false);op_btn2.gameObject.SetActive(false);}void OnVideoEvent(MediaPlayer mp, MediaPlayerEvent.EventType et, ErrorCode errorCode){switch (et){case MediaPlayerEvent.EventType.ReadyToPlay:Debug.Log("視頻加載完成,準備播放");break;case MediaPlayerEvent.EventType.Started:Debug.Log($"當前時間: {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}視頻播放開始");break;case MediaPlayerEvent.EventType.FirstFrameReady:Debug.Log($"當前時間: {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}第一幀渲染完成");StartCoroutine(DelayFunction());break;case MediaPlayerEvent.EventType.FinishedPlaying:Debug.Log("視頻播放完畢");break;case MediaPlayerEvent.EventType.FinishedSeeking:Debug.Log("跳轉到指定時間完成");break;default://Debug.Log("未知視頻事件",et);break;}}void CheckCurrentTime(){float currentTime = (float)mediaPlayer.Control.GetCurrentTime();if (targetTime != 0 && currentTime >= targetTime && !hasTriggered){hasTriggered = true;Debug.Log($"視頻已播放到目標時間: {targetTime}秒");// 在這里執行你的回調邏輯ShowOptionsUI();}}void test(){StartLevel("1");}void StartLevel(string levelid){LevelNode n = NodeCfg.Instance.GetNode(levelid);levelNode = n;Debug.Log($"當前時間: {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}加載視頻開始");string fullpath = System.IO.Path.Combine(Application.streamingAssetsPath, "videos", levelNode.mp4);if (!mediaPlayer.OpenMedia(MediaPathType.RelativeToStreamingAssetsFolder, fullpath, false)){Debug.LogError("Video not found!");return;}if (n.op_time > 0){targetTime = n.op_time;}mediaPlayer.Play();Debug.Log($"當前時間: {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}加載視頻結束");}void FinishLevel(string nextlevelid){HideOptionsUI();hasTriggered = false; // 重置觸發狀態targetTime = 0;CaptureAndDisplayLastFrame();StartLevel(nextlevelid);}private void CaptureAndDisplayLastFrame(){Debug.Log($"當前時間: {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}截圖");// 創建Texture2D來保存截圖Texture2D texture = new Texture2D(mediaPlayer.Info.GetVideoWidth(), mediaPlayer.Info.GetVideoHeight(), TextureFormat.ARGB32, false);// 截取當前幀(最后一幀)mediaPlayer.ExtractFrame(texture);// 顯示截圖if (displayImage != null){displayImage.texture = texture;}displayImage.gameObject.SetActive(true);}IEnumerator DelayFunction(){yield return null; // 暫停一幀Debug.Log($"當前時間: {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}下一幀執行");displayImage.gameObject.SetActive(false);}
}