Unity3D开发之VideoPlayer

    最近老徒弟要毕业了,然后毕设里unity不能播放视频声音。当时我自己测试的时候也是不能正常播放声音。翻了好久官方API文档,还是很迷惑。最后Google了下,找到了一个外国友人提供的解决方案。代码如下:

一.播放指定视频文件

public class Test : MonoBehaviour {

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


        //Google到的解决方案
        //这里一定要让以上工作完成后才能开始准备videoPlayer  并且赋值视频输出Texture
        videoPlayer.Prepare();

        //Wait until video is prepared
        while (!videoPlayer.isPrepared)
        {
            Debug.Log("Preparing Video");
            yield return null;
        }

        //Set Raw Image to Show Video Images
        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");
    }
}

   查了下管网API,这个Prepare()很重要。只有在准备完成后,视频的每一帧才会被立即正确的播放。并且,我们要在播放器属性设置完之后在调用,不然语音不能被正确播放。


二.从网站地址播放视频

    就是将上面代码的

//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.source = VideoSource.Url;
        videoPlayer.url = "http://www.JayW.handsomeboy.mp4";

三.播放本地文件视频

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;
四.视频播放支持的格式

所有支持的视频格式:

  • ogv
  • vp8
  • webm
  • mov
  • dv
  • mp4
  • m4v
  • mpg
  • mpeg

部分Windows上支持的视频格式:

  • avi
  • asf
  • wmf

Unity的VideoPlayer还支持其他输出模式,


可根据自身需求参考官方API进行选择。以上就是VideoPlayer播放视频的大体内容。希望对你有帮助。

猜你喜欢

转载自blog.csdn.net/qq_33994566/article/details/80220885