Unity 用UniversalMediaPlayer 2.0.3版本视频流插件 只用视频流地址 不播放当前视频 获取第一帧显示在UI上
时间: 2024-05-12 20:20:22 浏览: 134
Unity 视频播放插件
以下是使用UniversalMediaPlayer 2.0.3版本视频流插件获取第一帧并在UI上显示的示例代码:
```csharp
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;
public class VideoStreamController : MonoBehaviour
{
public RawImage image; //UI上的RawImage组件
public string videoStreamUrl; //视频流地址
private UniversalMediaPlayer mediaPlayer; //视频流播放器
private bool isReadyToGetFirstFrame = false; //是否准备获取第一帧
void Start()
{
//初始化视频流播放器
mediaPlayer = new UniversalMediaPlayer();
mediaPlayer.EventManager.OnPlayerReady += OnPlayerReady;
mediaPlayer.EventManager.OnPlayerOpening += OnPlayerOpening;
mediaPlayer.EventManager.OnPlayerBuffering += OnPlayerBuffering;
mediaPlayer.EventManager.OnPlayerPlaying += OnPlayerPlaying;
mediaPlayer.EventManager.OnPlayerPaused += OnPlayerPaused;
mediaPlayer.EventManager.OnPlayerStopped += OnPlayerStopped;
mediaPlayer.EventManager.OnPlayerEndReached += OnPlayerEndReached;
mediaPlayer.EventManager.OnPlayerEncounteredError += OnPlayerEncounteredError;
mediaPlayer.InitPlayer();
}
void Update()
{
//如果准备获取第一帧并且视频流播放器已经准备好,就获取第一帧并显示在UI上
if (isReadyToGetFirstFrame && mediaPlayer != null && mediaPlayer.IsReady)
{
Texture2D frameTexture = mediaPlayer.GetTexture(0);
if (frameTexture != null)
{
image.texture = frameTexture;
isReadyToGetFirstFrame = false;
}
}
}
void OnDestroy()
{
//销毁视频流播放器
if (mediaPlayer != null)
{
mediaPlayer.Stop();
mediaPlayer = null;
}
}
void OnPlayerReady(UniversalMediaPlayer mediaPlayer, IntPtr userdata)
{
//视频流播放器准备好后开始播放
mediaPlayer.Play(videoStreamUrl);
}
void OnPlayerOpening(UniversalMediaPlayer mediaPlayer, IntPtr userdata)
{
//视频流开始打开时设置准备获取第一帧标志位
isReadyToGetFirstFrame = true;
}
void OnPlayerBuffering(UniversalMediaPlayer mediaPlayer, float bufferPercent, IntPtr userdata)
{
//视频流缓冲进度变化时不做任何处理
}
void OnPlayerPlaying(UniversalMediaPlayer mediaPlayer, IntPtr userdata)
{
//视频流播放时不做任何处理
}
void OnPlayerPaused(UniversalMediaPlayer mediaPlayer, IntPtr userdata)
{
//视频流暂停时不做任何处理
}
void OnPlayerStopped(UniversalMediaPlayer mediaPlayer, IntPtr userdata)
{
//视频流停止时不做任何处理
}
void OnPlayerEndReached(UniversalMediaPlayer mediaPlayer, IntPtr userdata)
{
//视频流播放结束时不做任何处理
}
void OnPlayerEncounteredError(UniversalMediaPlayer mediaPlayer, IntPtr userdata)
{
//视频流播放出错时不做任何处理
}
}
```
在上述代码中,我们使用UniversalMediaPlayer的GetTexture方法来获取第一帧,并将其显示在UI上的RawImage组件中。注意,在OnPlayerOpening回调中设置isReadyToGetFirstFrame标志位为true,是为了在视频流开始打开时获取第一帧,避免等待太长时间。
阅读全文