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();
    }
相关推荐
小贺儿开发4 小时前
Unity3D 心理沙盘互动演示
unity·ai·pdf·人机交互·工具·互动·心理沙盘
CuPhoenix7 小时前
【沧海拾昧】Unity 导入中文字体文字缺失的解决方法
unity
南無忘码至尊7 小时前
Unity学习90天-第1天-认识Transform + 坐标系
学习·unity·游戏引擎
南無忘码至尊8 小时前
Unity学习90天-第1天-认识Unity并书写我们的第一个脚本
学习·unity·游戏引擎
风酥糖8 小时前
Godot游戏练习01-第26节-轮次结束后弹出升级选项
游戏·游戏引擎·godot
雪域迷影9 小时前
Hazel游戏引擎结构分析
c++·游戏引擎·hazel
Nuopiane11 小时前
C#基础(1)堆栈、GC与Marshal
unity·c#
weixin_4093831217 小时前
godot创建两种敌人僵尸 一种吐舌头 一种在角色脚下生成圆形伤害圈 两种僵尸均继承enemy脚本 理解继承
游戏引擎·godot
mxwin1 天前
Unity Shader 跨平台兼容性:处理纹理坐标翻转与精度差异
unity·游戏引擎
王家视频教程图书馆1 天前
godot 下载地址
游戏引擎·godot