Unity VideoPlayer 指定位置开始播放

如果 source是 videoclip(以下两种方式都可以):

复制代码
  _videoPlayer.Play();
        Debug.Log("time: " + _videoPlayer.clip.length);
        _videoPlayer.time = 10;

[SerializeField] VideoPlayer videoPlayer;

public void SetClipWithTime(VideoClip clip, float time) 
{
    StartCoroutine (SetTimeRoutine(clip, time));
}

IEnumerator SetTimeRoutine (VideoClip clip, float time)
{
    if(videoPlayer.isPlaying)
    {
        videoPlayer.Stop();
    }

    videoPlayer.clip = clip;

    videoPlayer.Prepare();
    yield return new WaitUntil (() => videoPlayer.isPrepared);  
    yield return new WaitUntil (() => videoPlayer.canSetTime);

    videoPlayer.Play();
    videoPlayer.time = time;
}

如果是source是URL:

复制代码
IEnumerator VideoPlay()
    {
        _videoPlayer.Prepare();

        yield return new WaitUntil(() => _videoPlayer.isPrepared);
        yield return new WaitUntil(() => _videoPlayer.canSetTime);
        // 检查是否完成初始化
        //while (!_videoPlayer.isPrepared)
        //{
        //    yield return new WaitForSeconds(1f);
        //    break;
        //}
        // 设置开头是第100帧
        _videoPlayer.frame += 100;
        // 开始播放
        _videoPlayer.Play();
        yield return new WaitForSeconds(0.01f);
        _videoPlayer.Pause();
    }

 IEnumerator VideoPlay()
    {
        _videoPlayer.Prepare();
        // 检查是否完成初始化
        while (!_videoPlayer.isPrepared)
        {
            yield return new WaitForSeconds(1f);
            break;
        }
        // 设置开头是第100帧
        _videoPlayer.frame += 100;
        // 开始播放
        _videoPlayer.Play();
        //yield return new WaitForSeconds(0.01f);
        //_videoPlayer.Pause();
    }
相关推荐
CreasyChan9 小时前
C# 反射详解
开发语言·前端·windows·unity·c#·游戏开发
IMPYLH11 小时前
Lua 的 Coroutine(协程)模块
开发语言·笔记·后端·中间件·游戏引擎·lua
世洋Blog21 小时前
SiYangUnityEventSystem,一个Unity中的事件系统
观察者模式·unity·c#·游戏引擎·事件系统
呆呆敲代码的小Y21 小时前
【Unity实战篇】| 游戏滑动框添加特殊效果,如实时高亮显示、曲线滑动等
游戏·unity·游戏引擎·实战·u3d·免费游戏·unity实战技巧
Tatalaluola1 天前
【Quest开发】用unity UI快速实现交互
unity·游戏引擎
技术小甜甜1 天前
[Godot] 在 Godot 3.1 中配置 ADB 可执行文件的实用指南
游戏·adb·游戏引擎·godot
技术小甜甜1 天前
【Godot】【入门】Godot 是什么?适合做哪些类型的游戏(附路线图+避坑清单)
游戏·游戏引擎·godot
码界奇点1 天前
Unity WebGL输入支持终极指南解决浏览器输入难题的完整方案
unity·容器·游戏引擎·鸿蒙系统·webgl
90后小陈老师1 天前
Unity动画控制
unity·游戏引擎
Miss_SQ1 天前
Webgl打包后删除StreamingAssets文件夹下多余资源
unity·c#·webgl