【问题标题】:Set Video playback speed in Unity在 Unity 中设置视频播放速度
【发布时间】:2017-03-22 14:29:51
【问题描述】:

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

我需要在 Unity 中播放投影在材质上的视频,并且我需要在运行时控制该视频的速度。由于我以移动设备为目标,因此无法使用 MovieTexture。还有其他选择,例如Easy Movie Texture,但它们不允许我控制视频的速度。

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

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

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

视频格式还没有确定,所以最简单的。

【问题讨论】:

    标签: c# performance video unity3d video-encoding


    【解决方案1】:

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

    使用新的 Unity VideoPlayer API,您可以检查是否可以使用VideoPlayer.canSetPlaybackSpeed 在平台上设置播放速度。如果返回true,则可以通过更改videoPlayer.playbackSpeed 属性来设置视频播放速度。

    您可以使用answer中的代码在RawImage上播放视频,然后添加下面的代码来设置播放速度。

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

    就这么简单。


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

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

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

    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;
            }
        }
    }
    

    【讨论】:

    • 感谢您详细而准确的回答。我目前有一个紧急的其他错误修复要做,但我会回来测试它并标记它是否有效:) 它适用于移动设备尤其重要(例如,Movietexture 不起作用),即使你编辑了它我的问题标题和标签的一部分...
    • 当人们说他们想在 Unity 中播放视频时,您应该自动假设视频应该在每个平台上都可以播放。阅读问题后,我认为无需标记 mobile 。这个答案也应该适用于移动设备。
    • 感谢您的回答。它确实回答了这里的问题,遗憾的是它并不完全适合我。但我想我会对此提出一个新问题。那个视频播放器已经让我更接近了谢谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多