【发布时间】:2016-12-16 16:43:37
【问题描述】:
Unity 在 5.6.0b1 版本中引入了新功能,现在可以在 iPhone 和 Android 上播放视频。
我设法让它在 iphone 和 Windows 上运行,但在 Android 上它卡在“准备”视频。
这是我使用的代码
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.url = Application.streamingAssetsPath + "/TestVideo.MP4";
videoPlayer.source = VideoSource.Url;
VideoClip clip = new 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);
videoPlayer.Prepare();
//Wait until video is prepared
while (!videoPlayer.isPrepared)
{
Debug.Log("Preparing Video");
yield return null;
}
Debug.Log("Done Preparing Video");
//Play Video
videoPlayer.Play();
//Play Sound
audioSource.Play();
Debug.Log("Playing Video");
while (videoPlayer.isPlaying)
{
yield return null;
}
Debug.Log("Done Playing Video");
}
附言
在 Unity 文档中我发现在使用 android 时,我需要使用 WWW 类来检索文件。问题是只有 MovieTexture 可用,并且没有来自 WWW 对象的 VideoClip。有办法转换吗?
【问题讨论】: