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

__在Unity

5.6.0b1发布之后,

MovieTexture

最终被弃用,并且现在发布了可在台式机和移动设备上播放视频的新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");

}

回答:

找到了问题。下面是播放视频和音频的 代码:

//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”标题的错误,因为这是一个错误。

有些人还通过更改来解决此问题:

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;这些都不再需要。

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;


  • v
  • vp8
  • Webm
  • mov
  • dv
  • mp4
  • m4v
  • mpg
  • mpeg

  • avi
  • asf
  • WMF

其中某些格式在某些平台上不起作用。有关支持的视频格式的更多信息,请参见这篇文章。

以上是 使用新的Unity VideoPlayer和VideoClip API播放视频 的全部内容, 来源链接: utcz.com/qa/435140.html

回到顶部