【发布时间】:2016-12-14 13:38:42
【问题描述】:
MovieTexture 在 Unity 5.6.0b1 发布后最终被弃用,现在发布了在桌面和移动设备上播放视频的新 API。
VideoPlayer 和 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");
}
【问题讨论】:
-
您自己转换为向社区发帖了吗?
-
@Lestat 我没有,但如果可以的话,我会的。看起来Martijn Pieters 这样做了,因为这个问题被新用户滥用,他们只发布问题作为答案。他不得不删除其中的多个答案/问题。
-
我创建了一个使用 VideoPlayer 的视频教程。这是链接。 youtu.be/nGA3jMBDjHk.
-
另外,如果您正在加载多个视频并面临应用程序的滞后,这可能会有所帮助。 stackoverflow.com/questions/42801468/…
-
看来 VideoPlayer.prepare() 在主线程上运行!尤其是在安卓上。结果是应用程序在 prepare() 工作时挂起!使这个新界面几乎无法使用。而且你也不能在 bckd 线程上运行它。有人找到解决办法吗?