Unity实现播放Ogg格式视频功能实现
前言
我在之前做过的项目中,涉及到视频播放这一块功能的开发上,一般我就直接用Unity自己封装的VideoPlayer开发就可以了,VideoPlayer的功能比较强大,同时也比较稳定。但是在有的比较老的项目中,有时候还会使用播放ogg视频格式的开发功能。我在之前的几个项目中也做过类似的功能,所以就在这里记录一下使用Unity开发播放Ogg视频的功能。
步骤
一、在场景中新建一个RawImage组件,用来当做显示视频纹理的画布,如下图所示:
二、新建SpriteInfo.cs脚本,实现将Ogg视频传入的功能,代码如下所示:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class SpriteInfo : MonoBehaviour
{
public MovieTexture currentMov;//电影
private string videoFileName;
private string videoName;
public double startTime;
//配置文件类
private ConfigTest configTest;
// Use this for initialization
private void Awake()
{
configTest = GameObject.Find("configTest").GetComponent<ConfigTest>();
DontDestroyOnLoad(this.gameObject);
}
void Start ()
{
videoName = configTest.dic["MoiveName"]["Name0"];
videoFileName = Application.streamingAssetsPath + "/moive/" + videoName;
StartCoroutine(DownloadMovie());
}
/// <summary>
/// 加载影片的协程
/// </summary>
/// <returns>返回www</returns>
IEnumerator DownloadMovie()
{
startTime = (double)Time.deltaTime;
if (File.Exists(videoFileName))
{
WWW www = new WWW("file://" + videoFileName);
currentMov = (MovieTexture)www.GetMovieTexture();
while (!currentMov