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();
    }
相关推荐
future_studio1 小时前
聊聊 Unity(小白专享、熟悉基础编程 ... ...)
unity·游戏引擎
ellis197013 小时前
toLua[六] Examples 05_LuaCoroutine分析
unity
程序员正茂1 天前
Unity3d中Tab控件的实现
ui·unity·tab·控件
三掌柜6662 天前
突破AR视觉交互边界:Unity赋能Rokid AR眼镜实现高精度图像识别与实时跟踪
unity·ar·交互
Brianna Home2 天前
Godot4.3开发2D游戏全记录
游戏·游戏引擎·godot·游戏程序·动画
王维志2 天前
使用Asp.Net WebApi(.net 8)托管Unity WebGL
unity·游戏引擎·webgl
开发游戏的老王2 天前
虚幻引擎入门教程:虚幻引擎的安装
游戏引擎·虚幻
muyouking112 天前
Unreal Engine 中的旋转表示:FQuat 与 FRotator 全面解析
游戏引擎·虚幻
lrh30253 天前
Custom SRP 12 - HDR
3d·unity·srp·render pipeline
霜绛3 天前
Unity:Json笔记——Json文件格式、JsonUtlity序列化和反序列化
学习·unity·json·游戏引擎