gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:32:39 30 4
gpt4 key购买 nike

MovieTexture在 Unity 5.6.0b1 发布后最终被弃用,现在发布了在桌面和移动设备上播放视频的新 API。

VideoPlayerVideoClip可用于播放视频并在需要时检索每一帧的纹理。

我已经设法让视频正常工作,但无法让音频在 Windows 10 上的编辑器中播放。有人知道为什么音频不播放吗?

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

//Audio
private AudioSource audioSource;

// Use this for initialization
void 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 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");

//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);

//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");
}

最佳答案

发现问题。下面是播放视频和音频的FIXED代码:

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

//Audio
private AudioSource audioSource;

// Use this for initialization
void 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 AudioSource
videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;

//Assign the Audio from Video to AudioSource to be played
videoPlayer.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 prepared
WaitForSeconds 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”的错误,因为这是一个错误。

一些 people还通过更改修复了它:

videoPlayer.playOnAwake = false; 
audioSource.playOnAwake = false;

videoPlayer.playOnAwake = true; 
audioSource.playOnAwake = true;

从 URL 播放视频:

替换:

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

与:

//We want to play from url
videoPlayer.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 url
videoPlayer.source = VideoSource.Url;
videoPlayer.url = url;

所有支持的视频格式:

  • gv
  • vp8
  • webm
  • 移动
  • 视频
  • mp4
  • m4v
  • 每加仑
  • mpeg

Windows 上额外支持的视频格式:

  • 视频
  • asf
  • wmf

其中一些格式在某些平台上不起作用。参见 this发布有关支持的视频格式的更多信息。

关于c# - 使用新的 Unity VideoPlayer 和 VideoClip API 播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48780298/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com