资源预览内容
第1页 / 共4页
第2页 / 共4页
第3页 / 共4页
第4页 / 共4页
亲,该文档总共4页全部预览完了,如果喜欢就下载吧!
资源描述
VideoPlayer的使用方法(Unity5.6以后才能使用)经测试之后,暂时仅支持的视频格式(格式工厂里面的)有avi,mp4,mov,wmv,webm(格式有问题,暂不支持音频,无声)。使用步骤:(无需脚本即可播放)1. 创建一个用于显示视频的“屏幕”,Hierarchy视图里面创建一个Plane,添加渲染纹理(在Assets里面创建,右键CreateRender Texture,拖给Plane。)2. Hierarchy视图里面右键VideoVideoPlayer,在Video Player组件里面有个属性Source,有两个值VideoClip和URL,分别表示视频文件、视频文件路径。直接拖进去。完成复制。同理选择URL(外部文件,本地和网络),把视频的路径赋值进去。3. 赋值完成后,播放会没有声音,需要添加一个Audio Source组件,拖到指定位置进行赋值。4. 以上即完成了视频播放的流程。VideoPlayer可以使用一系列事件来监听播放的各个动作: errorReceived: 错误监听到时被执行。 frameDropped :有丢帧发生时被执行。 frameReady :新的一帧准备好时被执行。 loopPointReached :播放结束或播放到循环的点时被执行。 prepareCompleted :视频准备完成时被执行。 seekCompleted :查询帧操作完成时被执行。 started:在Play方法调用之后立刻调用。创建一个Plane,将Test挂上去。即可播放代码控制:public class Test : MonoBehaviour /public VideoClip videoToPlay; private VideoPlayer videoPlayer; private VideoSource videoSource; private AudioSource audioSource; / Use this for initialization void Start() Application.runInBackground = true; StartCoroutine(playVideo(); IEnumerator playVideo() videoPlayer = gameObject.AddComponent(); /用于播放音频 audioSource = gameObject.AddComponent(); videoPlayer.playOnAwake = false; audioSource.playOnAwake = false; /We want to play from video clip not from url videoPlayer.source = VideoSource.VideoClip; /将视频设置为播放,然后准备音频以防止缓冲 / videoPlayer.clip = videoToPlay; videoPlayer.url = Application.dataPath + /videos/2.mp4; videoPlayer.source = VideoSource.Url; VideoClip clip = new VideoClip(); /设置音频输出模式为AudioSource videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource; /将视频中的音频分配给要播放的音频源 videoPlayer.EnableAudioTrack(0, true); videoPlayer.SetTargetAudioSource(0, audioSource); videoPlayer.Prepare(); while (!videoPlayer.isPrepared) Debug.Log(正在准备); yield return null; Debug.Log(准备完成); videoPlayer.loopPointReached += EndReached; videoPlayer.Play(); audioSource.Play(); Debug.Log(播放中); while (videoPlayer.isPlaying) yield return null; /会先打印End reached 再打印播放结束 Debug.Log(播放结束); void EndReached(VideoPlayer vPlayer) Debug.Log(End reached!);
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号