2017-03-22 145 views
0

我的一般問題是我需要在Unity3D世界中播放和控制視頻(無聲音)的速度,並且可能需要自己控制解碼,我完全不知道該怎麼辦這是有效的。所以任何正確方向的提示都是值得歡迎的。在Unity中設置視頻播放速度

我需要播放投影在Unity中的材質上的視頻,並且我需要在運行時控制該視頻的速度。當我定位移動設備時,我無法使用MovieTexture。還有其他選擇,例如簡單的電影紋理,但他們不允許我控制視頻的速度。

我發現這個post有問題的解決方案。簡而言之,作者將視頻分解成幀,然後逐幀顯示。這樣我可以通過改變時間直到顯示下一幀來控制視頻速度。視頻沒有任何聲音,所以非常簡單。 問題是這是從記憶和性能角度來看的噩夢。該應用程序將是GB大而低效的。

所以據我所知,一個普通的視頻播放器通過不保存和顯示每一幀,而只是顯示它們之間的差值來解決這個問題。如果我可以自己做到這一點,並逐幀控制它,我會解決我的問題。

我想在運行時解碼它,然後顯示增量值。但我不知道該怎麼做。所以請給我指出正確的方向,或者甚至給我一個解決方案,如果你有。

視頻格式尚未修復,所以最簡單。

回答

1

你不需要Easy Movie Texture來做到這一點,你甚至不需要獲取視頻幀來做到這一點。

使用新的Unity VideoPlayer API,您可以檢查是否可以使用VideoPlayer.canSetPlaybackSpeed設置平臺上的播放速度。如果返回true,則可以通過更改videoPlayer.playbackSpeed屬性來設置視頻播放速度。

您可以使用此answer中的代碼在RawImage上播放視頻,然後添加以下代碼以設置播放速度。

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

就這麼簡單。


您提到要將圖像投影到材質上。在這種情況下,你應該設置VideoPlayer.renderModeVideoRenderMode.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; 
     } 
    } 
} 
+0

感謝您的長時間和確切答案。我目前有一個緊急的其他bug修復程序,但我會回來測試它,並將其標記爲正確,如果它有效:) 它在移動設備上工作尤其重要(例如,Movietexture不起作用),即使您已編輯那部分出於我的問題標題和標籤... – findusl

+0

當人們說他們想要在Unity中播放視頻時,您應該自動假定該視頻應該適用於每個平臺。在看完這個問題後,我發現無需標記* mobile *。這個答案也適用於移動設備。 – Programmer

+0

感謝您的回答。它確實回答了這個問題,但遺憾的是它並不完全適用於我。但我想我會對它做一個新的問題。該視頻播放器已經給我帶來了更多更近的謝謝:) – findusl