Closed szzber2022 closed 1 week ago
using System; using RenderHeads.Media.AVProVideo; using UnityEngine; using UnityEngine.UI;
namespace DJGame {
public class AVTool : MonoBehaviour
{
public string Video_Path;
public bool isOwnTexture = false;
public MediaPlayer avPlayer;
public ResolveToRenderTexture rtTexture;
public RawImage rawImage;
public int videoWidht;
public int videoHeight;
public bool isUseStreamingAssets = true;
private bool isPlaying = false;
private Action curCallBack = null;
private Action<GameObject> curCloseBack = null;
private RenderTexture _renderTexture = null;
void Awake()
{
if (isOwnTexture)
{
if (_renderTexture == null)
{
var rt = RenderTexture.GetTemporary(videoWidht, videoHeight, 0);
rtTexture.ExternalTexture = rt;
rawImage.texture = rt;
//释放
if (_renderTexture)
{
RenderTexture.ReleaseTemporary(_renderTexture);
_renderTexture = null;
}
_renderTexture = rt;
}
}
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnDestroy()
{
Debug.Log("AVTool ==== OnDestroy");
//关闭视频,释放内存
if (isPlaying)
{
isPlaying = false;
//关闭视频,释放内存
if (avPlayer != null)
{
Debug.Log("avPlayer.Control.Stop()");
if (avPlayer.Control != null)
{
avPlayer.Control.Stop();
avPlayer.Control.CloseMedia();
}
else
{
avPlayer.Stop();
avPlayer.CloseMedia();
}
avPlayer.Events.RemoveListener(Evts);
if (isOwnTexture)
{
Debug.Log("释放 RenderTexture1");
rtTexture.ExternalTexture = null;
rawImage.texture = null;
if (_renderTexture)
{
Debug.Log("释放 RenderTexture2");
RenderTexture.ReleaseTemporary(_renderTexture);
_renderTexture = null;
}
if (avPlayer != null)
{
Destroy(avPlayer);
avPlayer = null;
}
if (rawImage != null)
{
Destroy(rawImage);
rawImage = null;
}
if (rtTexture != null)
{
Destroy(rtTexture);
rtTexture = null;
}
Resources.UnloadUnusedAssets();
}
}
Debug.Log("关闭视频完成");
}
else
{
if (isOwnTexture)
{
Debug.Log("释放 RenderTexture1");
rtTexture.ExternalTexture = null;
rawImage.texture = null;
if (_renderTexture)
{
Debug.Log("释放 RenderTexture2");
RenderTexture.ReleaseTemporary(_renderTexture);
_renderTexture = null;
}
if (avPlayer != null)
{
Destroy(avPlayer);
avPlayer = null;
}
if (rawImage != null)
{
Destroy(rawImage);
rawImage = null;
}
if (rtTexture != null)
{
Destroy(rtTexture);
rtTexture = null;
}
Resources.UnloadUnusedAssets();
}
}
}
public bool getIsPlaying()
{
return isPlaying;
}
public void replayVideo()
{
if (isPlaying)
{
avPlayer.Control.Rewind();
}
}
public void setVideoPath(string path)
{
Video_Path = path;
}
public void showVideo(Action callBack = null)
{
Debug.Log("=======showVideo===== Video_Path == " + Video_Path);
this.curCallBack = callBack;
if (avPlayer)
{
if (!isPlaying)
{
//注册事件
avPlayer.Events.AddListener(Evts);
//加载背景视频,默认加载完自动播放
if (isUseStreamingAssets)
{
avPlayer.OpenMedia(MediaPathType.RelativeToStreamingAssetsFolder, Video_Path);
}
else
{
avPlayer.OpenMedia(MediaPathType.RelativeToDataFolder, Video_Path);
}
}
else
{
avPlayer.Control.Rewind();
}
}
}
public void hideVideo(Action<GameObject> callBack = null)
{
Debug.Log("AVTool ======= hideVideo");
this.curCloseBack = callBack;
if (isPlaying)
{
isPlaying = false;
//关闭视频,释放内存
if (avPlayer != null)
{
Debug.Log("avPlayer.Control.Stop()");
if (avPlayer.Control != null)
{
avPlayer.Control.Stop();
avPlayer.Control.CloseMedia();
}
else
{
avPlayer.Stop();
avPlayer.CloseMedia();
}
avPlayer.Events.RemoveListener(Evts);
}
Debug.Log("关闭视频完成");
}
}
private void Evts(MediaPlayer mp, MediaPlayerEvent.EventType mediaPlayerEvent, ErrorCode errorCode)
{
switch (mediaPlayerEvent)
{
case MediaPlayerEvent.EventType.MetaDataReady:
{
Debug.Log("视频数据准备完成。当元数据(宽度,持续时间等)可用时触发");
// 获取视频的宽度和高度
int width = mp.Info.GetVideoWidth();
int height = mp.Info.GetVideoHeight();
Debug.Log("width =111111= " + width);
Debug.Log("height =111111= " + height);
}
break;
case MediaPlayerEvent.EventType.ReadyToPlay:
{
Debug.Log("加载视频并准备播放时触发");
}
break;
case MediaPlayerEvent.EventType.Started:
{
Debug.Log("播放开始时触发");
isPlaying = true;
}
break;
case MediaPlayerEvent.EventType.FirstFrameReady:
{
Debug.Log("渲染第一帧时触发");
}
break;
case MediaPlayerEvent.EventType.FinishedPlaying:
{
Debug.Log("当非循环视频播放完毕时触发" + (this.curCallBack != null));
Debug.Log("=======showVideo===== Video_Path == " + Video_Path);
if (this.curCallBack != null)
{
Debug.Log("当非循环视频播放完毕时触发" + this.curCallBack);
this.curCallBack();
}
}
break;
case MediaPlayerEvent.EventType.Closing:
{
Debug.Log("媒体关闭时触发");
Debug.Log(mp);
if (this.curCloseBack != null)
{
this.curCloseBack(this.gameObject);
}
}
break;
default:
break;
}
}
}
}
Hi @szzber2022,
AVPro Video version 2 has been depreciated and will therefore not be receiving any further fixes or updates. I suggest that you update to the last version, 2.9.3., as there have been a lot of fixes since the one you are using.
Unity version
2022.3.47
Unity editor platform
Windows
AVPro Video edition
Ultra
AVPro Video version
2.2.8
Device hardware
i5-13400F, 32G, NVIDIA GeForce RTX 3050
Which Windows version are you using?
10
Graphics API
D3D 11
Video API
Media Foundation
Audio output
System Direct
Any other Media Player component configuration required to reproduce the issue.
no
Which output component(s) are you using?
Display uGUI, Playlist MediaPlayer, Resolve to RenderTexture
Any other component configuration required to reproduce the issue.
no
The issue
When upgrading a Unity project from 2021 to 2022, after AVPro Video finishes playing a video, the VRAM continues to increase. Even after the video playback is completed and Stop and CloseMedia are called, switching to using an external RenderTexture and explicitly releasing it afterwards does not resolve the issue. Each time a video is played, the VRAM grows incrementally.
Media information
Any video can cause this issue.
Log output