在 Unity 中设置视频播放速度 [英] Set Video playback speed in Unity

查看:74
本文介绍了在 Unity 中设置视频播放速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一般问题是我需要在 Unity3D 世界中播放和控制视频(没有声音)的速度,并且可能需要自己控制解码,我完全不知道如何有效地做到这一点.因此,欢迎任何有关正确方向的提示.

My general problem is that I need to play and control the speed of a video (no sound) inside a Unity3D world and probably need to control the decoding myself and I have absolutely no idea how to do that efficiently. So any hints in the right direction are welcome.

我需要在 Unity 中播放投影到材质上的视频,并且需要在运行时控制该视频的速度.因为我的目标是移动设备,所以我不能使用 MovieTexture.有替代品,例如简单的电影纹理,但它们不允许我控制视频的速度.

I need to play a video projected on a Material in Unity and I need to control the speed of that video at runtime. As I target mobile devices I cannot use MovieTexture. There are alternatives like e.g. Easy Movie Texture but they do not allow me to control the speed of the video.

我在这篇帖子中找到了一个有问题的解决方案.简而言之,作者将视频分解成帧,然后逐帧显示.这样我就可以通过简单地改变时间来控制视频速度,直到显示下一帧.视频没有任何声音,所以很简单.问题是,从内存和性能的角度来看,这是一场噩梦.该应用程序将是 GB 大且效率低下.

I found a problematic solution in this post. In short, the author breaks the video into its frames and then displays it frame by frame. This way I can control the video speed by simply changing the time until displaying the next frame. The video does not have any sound, so it's that easy. The problem is that this is a nightmare from a memory and performace point of view. The app would be GB big and inefficient.

据我所知,普通视频播放器通过不保存和显示每一帧而只是在它们之间进行增量来解决这个问题.如果我能自己做到这一点并逐帧控制它,我就能解决我的问题.

So as far as I know a normal video player solves that by not saving and displaying every frame but just delta between them. If I could do that myself and control it frame by frame I'd solve my problem.

我想在运行时对其进行解码,然后显示增量.但我不知道该怎么做.所以请给我指出正确的方向,或者如果你有的话,甚至可以给我一个解决方案.

I imagine to decode it at runtime and then display the delta's. But I have no idea how to do that. So please point me in the right direction or maybe even give me a solution if you have.

视频格式尚未确定,所以选择最简单的.

The video format is not yet fixed, so whatever is easiest.

推荐答案

您不需要 Easy Movie Texture 来执行此操作,您甚至不需要获取视频帧来执行此操作.

You don't need Easy Movie Texture to do this and you don't even need to get the video frames to do this.

使用新的 Unity VideoPlayer API,您可以使用 VideoPlayer.canSetPlaybackSpeed.如果它返回 true,您可以通过简单地更改 videoPlayer.playbackSpeed 属性.

With the new Unity VideoPlayer API, you can check if you can set the playback speed on the platform with VideoPlayer.canSetPlaybackSpeed. If it returns true, you can then set the video playback speed by simply changing the videoPlayer.playbackSpeed property.

您可以使用此答案中的代码RawImage 上播放视频,然后添加下面的代码来设置播放速度.

You can use the code in this answer to play video on RawImage then add the code below to set the playback speed.

if (videoPlayer.canSetPlaybackSpeed)
{
    videoPlayer.playbackSpeed = 1f;
}

就是这么简单.

您提到您希望将图像投影到材料上.在这种情况下,您应该设置 VideoPlayer.renderModeVideoRenderMode.MaterialOverride;

You mentioned that you want the image to be projected on a material. In this case, you should set the VideoPlayer.renderMode to VideoRenderMode.MaterialOverride;

下面的代码应该将视频投影到任何名为DisplayObject"的游戏对象上.您还可以在 outputRendereroutputRenderer.material 变量中访问来自视频的素材.

The code below should project the video on any GameObject called "DisplayObject". You can also access the material from the video in the outputRenderer or outputRenderer.material variable.

它具有用于测试目的的音频,如果您不想要您在帖子中提到的音频,可以将其删除.

using UnityEngine;
using UnityEngine.Video;

public class VideoSpeedControl : MonoBehaviour
{
    //The material that Video will output to
    Renderer outputRenderer;

    private VideoPlayer videoPlayer;

    //Audio
    private AudioSource audioSource;

    void Start()
    {

        outputRenderer = gameObject.AddComponent<MeshRenderer>();

        //Add VideoPlayer to the GameObject
        videoPlayer = gameObject.AddComponent<VideoPlayer>();

        //Add AudioSource
        audioSource = gameObject.AddComponent<AudioSource>();

        //Disable Play on Awake for both Video and Audio
        videoPlayer.playOnAwake = false;
        audioSource.playOnAwake = false;

        // We want to play from url
        videoPlayer.source = VideoSource.Url;
        videoPlayer.url = "http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4";

        //Set Audio Output to AudioSource
        videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;

        //Assign the Audio from Video to AudioSource to be played
        videoPlayer.EnableAudioTrack(0, true);
        videoPlayer.SetTargetAudioSource(0, audioSource);

        //Set the mode of output
        videoPlayer.renderMode = VideoRenderMode.MaterialOverride;

        //Set the renderer to store the images to
        //outputRenderer = videoPlayer.targetMaterialRenderer;
        videoPlayer.targetMaterialProperty = "_MainTex";

        //Prepare Video to prevent Buffering
        videoPlayer.Prepare();

        //Subscribe to prepareCompleted event
        videoPlayer.prepareCompleted += OnVideoPrepared;
    }

    void OnVideoPrepared(VideoPlayer source)
    {
        Debug.Log("Done Preparing Video");

        //Play Video
        videoPlayer.Play();

        //Play Sound
        audioSource.Play();

        //Change Play Speed
        if (videoPlayer.canSetPlaybackSpeed)
        {
            videoPlayer.playbackSpeed = 1f;
        }
    }

    bool firsrRun = true;
    void Update()
    {
        if (firsrRun)
        {
            GameObject.Find("DisplayObject").GetComponent<MeshRenderer>().material = outputRenderer.material;
            firsrRun = false;
        }
    }
}

这篇关于在 Unity 中设置视频播放速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆