使用新的 Unity VideoPlayer 和 VideoClip API 播放视频
Using new Unity VideoPlayer and VideoClip API to play video
MovieTexture 在 Unity 5.6.0b1 发布后最终被弃用,现在发布了可在桌面和移动设备上播放视频的新 API。
VideoPlayer and VideoClip 可用于播放视频并在需要时检索每一帧的纹理。
我已经设法使视频正常工作,但无法通过 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();
之前而不是之后调用。这花了几个小时的实验才发现这是我遇到的问题。
停留在 "Preparing Video"?
调用 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;
所有支持的视频格式:
- ogv
- vp8
- webm
- mov
- dv
- mp4
- m4v
- mpg
- mpeg
Windows 额外支持的视频格式:
- 阿维
- asf
- wmf
其中一些格式在某些平台上不起作用。有关支持的视频格式的详细信息,请参阅 this post。
与其他答案所说的类似。您可以在准备和结束视频状态时使用回调。而不是使用协程和 yield return.
videoPlayer.loopPointReached += EndReached;
videoPlayer.prepareCompleted += PrepareCompleted;
void PrepareCompleted(VideoPlayer vp) {
vp.Play();
}
void EndReached(VideoPlayer vp) {
// do something
}
到现在为止,VideoPlayer 应该已足够更新,您无需编写代码即可正常工作。以下是我发现效果最理想的设置:
这些设置是:
视频播放器:
- 醒着玩:正确
- 等待第一帧:False
- 音频输出模式:None
音频源:
- 醒着玩:正确
不要忘记为 VideoPlayer 准备一个 VideoClip,为 AudioSource 准备一个 AudioClip。我发现效果最好的文件格式是 .ogv 视频和 .wav 音频。
我使用@Programmer 的回答来播放来自 URL 的视频,但我无法播放任何声音。最终我在the comments of a YouTube tutorial.
中找到了答案
要播放通过 URL 加载的电影的音频,您需要在调用 EnableAudioTrack
之前添加以下行:
videoPlayer.controlledAudioTrackCount = 1;
MovieTexture 在 Unity 5.6.0b1 发布后最终被弃用,现在发布了可在桌面和移动设备上播放视频的新 API。
VideoPlayer and VideoClip 可用于播放视频并在需要时检索每一帧的纹理。
我已经设法使视频正常工作,但无法通过 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();
之前而不是之后调用。这花了几个小时的实验才发现这是我遇到的问题。
停留在 "Preparing Video"?
调用 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;
所有支持的视频格式:
- ogv
- vp8
- webm
- mov
- dv
- mp4
- m4v
- mpg
- mpeg
Windows 额外支持的视频格式:
- 阿维
- asf
- wmf
其中一些格式在某些平台上不起作用。有关支持的视频格式的详细信息,请参阅 this post。
与其他答案所说的类似。您可以在准备和结束视频状态时使用回调。而不是使用协程和 yield return.
videoPlayer.loopPointReached += EndReached;
videoPlayer.prepareCompleted += PrepareCompleted;
void PrepareCompleted(VideoPlayer vp) {
vp.Play();
}
void EndReached(VideoPlayer vp) {
// do something
}
到现在为止,VideoPlayer 应该已足够更新,您无需编写代码即可正常工作。以下是我发现效果最理想的设置:
这些设置是:
视频播放器:
- 醒着玩:正确
- 等待第一帧:False
- 音频输出模式:None
音频源:
- 醒着玩:正确
不要忘记为 VideoPlayer 准备一个 VideoClip,为 AudioSource 准备一个 AudioClip。我发现效果最好的文件格式是 .ogv 视频和 .wav 音频。
我使用@Programmer 的回答来播放来自 URL 的视频,但我无法播放任何声音。最终我在the comments of a YouTube tutorial.
中找到了答案要播放通过 URL 加载的电影的音频,您需要在调用 EnableAudioTrack
之前添加以下行:
videoPlayer.controlledAudioTrackCount = 1;