MovieTexture最终被弃用,现在已发布在桌面和移动设备上播放视频的新API。
如果需要,VideoPlayer和VideoClip可用于播放视频并检索每个帧的纹理。
我已经设法让视频正常工作,但是却没有从Windows 10的编辑器那里得到音频。任何人都知道为什么没有播放音频?
{{1}}
答案 0 :(得分:55)
发现问题。以下是播放视频和音频的 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”的bug,因为这是一个错误。
有些people也通过更改:
来修复它videoPlayer.playOnAwake = false;
audioSource.playOnAwake = false;
到
videoPlayer.playOnAwake = true;
audioSource.playOnAwake = true;
从网址播放视频:
替换:
//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;
所有支持的视频格式:
Windows上额外支持的视频格式:
其中一些格式在某些平台上不起作用。有关支持的视频格式的详情,请参阅this帖子。
答案 1 :(得分:7)
与其他答案一样。您可以在准备和结束视频状态时使用回调。而不是使用协同程序和收益率。
videoPlayer.loopPointReached += EndReached;
videoPlayer.prepareCompleted += PrepareCompleted;
void PrepareCompleted(VideoPlayer vp) {
vp.Play();
}
void EndReached(VideoPlayer vp) {
// do something
}
答案 2 :(得分:1)
我使用@Programmer的答案从URL播放视频,但无法播放任何声音。最终,我在the comments of a YouTube tutorial.
中找到了答案要使音频播放通过URL加载的电影,您需要在调用EnableAudioTrack
之前添加以下行:
videoPlayer.controlledAudioTrackCount = 1;
答案 3 :(得分:-2)