使用新的Unity VideoPlayer和VideoClip API播放视频

使用新的Unity VideoPlayer和VideoClip API播放视频,第1张

使用新的Unity VideoPlayer和VideoClip API播放视频

找到了问题。下面是播放视频和音频的 固定 代码:

//Raw Image to Show Video Images [Assign from the Editor]public RawImage image;//Video To Play [Assign from the Editor]public VideoClip videoToPlay;private VideoPlayer videoPlayer;private VideoSource videoSource;//Audioprivate AudioSource audioSource;// Use this for initializationvoid Start(){    Application.runInBackground = true;    StartCoroutine(playVideo());}IEnumerator playVideo(){    //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 video clip not from url    videoPlayer.source = VideoSource.VideoClip;    //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 video To Play then prepare Audio to prevent Buffering    videoPlayer.clip = videoToPlay;    videoPlayer.Prepare();    //Wait until video is prepared    while (!videoPlayer.isPrepared)    {        Debug.Log("Preparing Video");        yield return null;    }    Debug.Log("Done Preparing Video");    //Assign the Texture from Video to RawImage to be displayed    image.texture = videoPlayer.texture;    //Play Video    videoPlayer.Play();    //Play Sound    audioSource.Play();    Debug.Log("Playing Video");    while (videoPlayer.isPlaying)    {        Debug.LogWarning("Video Time: " + Mathf.FloorToInt((float)videoPlayer.time));        yield return null;    }    Debug.Log("Done Playing Video");}

为什么音频没有播放:

//Set Audio Output to AudioSourcevideoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;//Assign the Audio from Video to AudioSource to be playedvideoPlayer.EnableAudioTrack(0, true);videoPlayer.SetTargetAudioSource(0, audioSource);

必须在

videoPlayer.Prepare();
不之后调用。经过数小时的实验,发现这是我遇到的问题。


停留在“准备视频”?

等待

videoPlayer.Prepare();
调用后等待5秒钟,然后退出while循环。

更换:

while (!videoPlayer.isPrepared){    Debug.Log("Preparing Video");    yield return null;}

与:

//Wait until video is preparedWaitForSeconds waitTime = new WaitForSeconds(5);while (!videoPlayer.isPrepared){    Debug.Log("Preparing Video");    //Prepare/Wait for 5 sceonds only    yield return waitTime;    //Break out of the while loop after 5 seconds wait    break;}

这应该可以,但是在视频开始播放时您可能会遇到缓冲。使用此临时修复程序时,我的建议是提交带有“ videoPlayer.isPrepared always
true”标题的错误,因为这是一个错误。

有些人还通过更改来解决此问题:

videoPlayer.playonAwake = false; audioSource.playonAwake = false;

videoPlayer.playonAwake = true; audioSource.playonAwake = true;

从URL播放视频:

更换:

//We want to play from video clip not from urlvideoPlayer.source = VideoSource.VideoClip;

与:

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

然后删除

public VideoClip videoToPlay;
videoPlayer.clip = videoToPlay;
这些都不再需要。

从StreamingAssets文件夹播放视频:

string url = "file://" + Application.streamingAssetsPath + "/" + "VideoName.mp4";if !UNITY_EDITOR && UNITY_ANDROID    url = Application.streamingAssetsPath + "/" + "VideoName.mp4";#endif//We want to play from urlvideoPlayer.source = VideoSource.Url;videoPlayer.url = url;

所有支持的视频格式

  • v
  • vp8
  • Webm
  • mov
  • dv
  • mp4
  • m4v
  • mpg
  • mpeg

Windows上额外支持的视频格式

  • avi
  • asf
  • WMF

其中某些格式在某些平台上不起作用。有关支持的视频格式的更多信息,请参见这篇文章。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5602300.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存