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();
    }
相关推荐
Doc.S9 小时前
多无人机任务自定义(基于ZJU-FAST-Lab / EGO-Planner-v2)
游戏引擎·无人机·cocos2d
那个村的李富贵10 小时前
Unity打包Webgl后 本地运行测试
unity·webgl
nnsix11 小时前
Unity OpenXR开发HTC Vive Cosmos
unity·游戏引擎
nnsix11 小时前
Unity OpenXR,扳机键交互UI时,必须按下扳机才触发
unity·游戏引擎
nnsix12 小时前
Unity XR 编辑器VR设备模拟功能
unity·编辑器·xr
老朱佩琪!12 小时前
Unity访问者模式
unity·游戏引擎·访问者模式
不定时总结的那啥12 小时前
Unity实现点击Console消息自动选中预制体的方法
unity·游戏引擎
nnsix13 小时前
Unity OpenXR 关闭手柄的震动
unity·游戏引擎
CreasyChan13 小时前
Unity 中的反射使用详解
unity·c#·游戏引擎·游戏开发
Jessica巨人13 小时前
Shader显示为黑色
unity·shader