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

Q:

MovieTexture is finally deprecated after Unity 5.6.0b1 release and new API that plays video on both Desktop and Mobile devices is now released.
在 Unity 5.6.0b1 发布后, MovieTexture最终被弃用,现在发布了在桌面和移动设备上播放视频的新 API。

VideoPlayer and VideoClip can be used to play video and retrieve texture for each frame if needed.
如果需要, VideoPlayerVideoClip可用于播放视频并检索每帧的纹理。

I’ve managed to get the video working but coduldn’t get the audio to play as-well from the Editor on Windows 10. Anyone know why audio is not playing?
我已经设法使视频正常工作,但无法从 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");
}

A:

Found the problem. Below is the FIXED code that plays Video and Audio:
发现问题了。以下是播放视频和音频的固定代码:

//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");
}

Why Audio was not playing:
为什么音频未播放:

//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);

must be called before videoPlayer.Prepare(); not after it. This is took hours of experiment to find this this was the problem I was having.
必须在videoPlayer.Prepare();不是在它之后。经过几个小时的实验才发现这就是我遇到的问题。


Stuck at “Preparing Video”?
停留在“准备视频”?

Wait 5 seconds after videoPlayer.Prepare(); is called then exit the while loop.
videoPlayer.Prepare();后等待 5 秒;被调用然后退出 while 循环。

Replace:  代替:

while (!videoPlayer.isPrepared)
{
    Debug.Log("Preparing Video");
    yield return null;
}

with:  和:

//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;
}

This should work but you may experience buffering when the video starts playing. While using this temporary fix, my suggestion is to file for bug with the title of “videoPlayer.isPrepared always true” because this is a bug.
这应该可行,但当视频开始播放时您可能会遇到缓冲。在使用此临时修复程序时,我的建议是提交标题为“videoPlayer.isPrepared 始终为真”的错误,因为这是一个错误。

Some people also fixed it by changing:
有些还通过更改来修复它:

videoPlayer.playOnAwake = false; 
audioSource.playOnAwake = false;

to

videoPlayer.playOnAwake = true; 
audioSource.playOnAwake = true;

Play Video From URL:  从 URL 播放视频:

Replace:  代替:

//We want to play from video clip not from url
videoPlayer.source = VideoSource.VideoClip;

with:  和:

//We want to play from url
videoPlayer.source = VideoSource.Url;
videoPlayer.url = "http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4";

then Remove:
然后删除

public VideoClip videoToPlay; and videoPlayer.clip = videoToPlay; as these are not needed anymore.
public VideoClip videoToPlay;和 videoPlayer.clip = videoToPlay; 因为不再需要这些了。

Play Video From StreamingAssets folder:
从 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;

All supported video formats:
所有支持的视频格式

  • ogv
  • vp8
  • webm  网络管理
  • mov
  • dv
  • mp4
  • m4v
  • mpg
  • mpeg

Extra supported video formats on Windows:
Windows 上额外支持的视频格式

  • avi
  • asf
  • wmf

Some of these formats don’t work on some platforms. See this post for more information on supported video formats.
其中一些格式不适用于某些平台。有关支持的视频格式的更多信息,请参阅这篇文章。

912sy.com下载资源均来源于网络,仅供学习和参考使用,版权归原作者所有,勿作商业用途,请在下载后24小时之内自觉删除。
本站发布的内容若无意中侵犯到您的权益,请联系我们,本站将在一个工作日内删除。如遇到任何问题请联系客服QQ:2385367137
912sy » 使用新的 Unity VideoPlayer 和 VideoClip API 播放视频