Untiy的Webgl端网页端视频播放,又不想直接mp4格式等格式的。

针对Untiy的Webgl端网页端视频播放,又不想直接mp4格式等格式的一些小技巧方式。

1.安装AVPro Video插件。

2.场景ui布局类似如下图:

3.UI中新建image,改名VideoDisplay,把image组件删除,添加Displayugui组件,关联mediaplayer。

4.UI中新建gameobject,添加mediapalyer组件,并关联你要播放的某个视频。

5.新建gameobject,添加HTML5VideoPlayer组件,并设置如下,videofilename的名称就是你的视频名称,视频位置webgl网页端主要是放在StreamingAssets中,视频的后缀名可以改为我这个名称。然后你同步设置也要同名称。

6.这个组件可以播放多个视频,你只要有多个panel拷贝下就行了。

相关的HTML5VideoPlayer代码如下:

csharp 复制代码
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using System.Collections;
using RenderHeads.Media.AVProVideo;

[System.Serializable]
public class VideoGroup
{
    public UnityEngine.UI.Button playButton;
    public UnityEngine.UI.Button pauseButton;
    public UnityEngine.UI.Button stopButton;
    public DisplayUGUI displayUGUI;
    public MediaPlayer mediaPlayer;  
    public string videoFileName = "11.longyugxq";
    
    [HideInInspector] public bool isLoaded = false;
}

public class HTML5VideoPlayer : MonoBehaviour
{
    [SerializeField] private List<VideoGroup> videoGroups = new List<VideoGroup>();

    private void Start()
    {
        StartCoroutine(InitializeAsync());
    }

    private IEnumerator InitializeAsync()
    {
        for (int i = 0; i < videoGroups.Count; i++)
        {
            yield return InitializeVideoGroupAsync(videoGroups[i], i);
        }
    }

    private IEnumerator InitializeVideoGroupAsync(VideoGroup group, int groupIndex)
    {
        // 检查MediaPlayer是否已在Inspector中设置
        if (group.mediaPlayer == null)
        {
            Debug.LogError($"VideoGroup {groupIndex} 未配置 MediaPlayer");
            yield break;
        }

        // 打开视频文件
        bool opened = group.mediaPlayer.OpenMedia(
            new MediaPath(group.videoFileName, MediaPathType.RelativeToStreamingAssetsFolder),
            autoPlay: false
        );

        if (!opened)
        {
            Debug.LogError($"打开视频失败: {group.videoFileName}");
            yield break;
        }

        // 等待加载完成
        yield return new WaitForSeconds(0.01f);

        Debug.Log($"VideoGroup {groupIndex} - {group.videoFileName} 加载完成");

        // 绑定按钮事件
        if (group.playButton != null)
            group.playButton.onClick.AddListener(() => PlayVideo(groupIndex));
        if (group.pauseButton != null)
            group.pauseButton.onClick.AddListener(() => PauseVideo(groupIndex));
        if (group.stopButton != null)
            group.stopButton.onClick.AddListener(() => StopVideo(groupIndex));

        group.isLoaded = true;
    }

    public void PlayVideo(int groupIndex)
    {
        if (groupIndex >= 0 && groupIndex < videoGroups.Count && videoGroups[groupIndex].isLoaded && videoGroups[groupIndex].mediaPlayer != null)
        {
            videoGroups[groupIndex].mediaPlayer.Play();
        }
    }

    public void PauseVideo(int groupIndex)
    {
        if (groupIndex >= 0 && groupIndex < videoGroups.Count && videoGroups[groupIndex].mediaPlayer != null)
        {
            videoGroups[groupIndex].mediaPlayer.Pause();
        }
    }

    public void StopVideo(int groupIndex)
    {
        if (groupIndex >= 0 && groupIndex < videoGroups.Count && videoGroups[groupIndex].mediaPlayer != null)
        {
            MediaPlayer mediaPlayer = videoGroups[groupIndex].mediaPlayer;
            mediaPlayer.Stop();
            mediaPlayer.Control.Seek(0.0);
        }
    }
}
相关推荐
叶智辽6 小时前
【Three.js与WebGPU】下一代3D技术到底强在哪?
webgl·three.js
波哥学开发1 天前
# Three.js 进阶:如何绘制"像素大小固定"的箭头?三种方案全解析
webgl·gpu
Panzer_Jack2 天前
如何用 WebGL 去实现一个选取色彩背景图片透明化小工具 - Pick Alpha
前端·webgl
烛阴3 天前
Three.js 零基础入门:手把手打造交互式 3D 几何体展示系统
javascript·webgl·three.js
叶智辽4 天前
【ThreeJS调试技巧】那些让 Bug 无所遁形的“脏套路”
webgl·three.js
REDcker5 天前
WebCodecs VideoDecoder 的 hardwareAcceleration 使用
前端·音视频·实时音视频·直播·webcodecs·videodecoder
gihigo19985 天前
基于TCP协议实现视频采集与通信
网络协议·tcp/ip·音视频
叶智辽5 天前
【ThreeJS急诊室】一个生产事故:我把客户的工厂渲染“透明”了
webgl·three.js
weixin_424294675 天前
Unity 调用Steamworks API 的 SteamUserStats.RequestCurrentStats()报错
unity·游戏引擎·steamwork
HoFunGames5 天前
Unity小地图,Easy Minimap System MT-GPS插件
unity·游戏引擎