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;
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());
}
IEnumerator DownloadMovie()
{
startTime = (double)Time.deltaTime;
if (File.Exists(videoFileName))
{
WWW www = new WWW("file://" + videoFileName);
currentMov = (MovieTexture)www.GetMovieTexture();
while (!currentMov.isReadyToPlay)
yield return www;
}
}
三、新建MoviePlayer.cs脚本,实现视频播放功能,代码如下所示:
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
[RequireComponent(typeof(AudioSource))]
public class MoviePlayer : MonoBehaviour
{
public MovieTexture movieTexture;
private AudioSource audio0;
private string movieDir;
public float time;
private SpriteInfo spriteInfo;
private float movieTime;
private static MoviePlayer instance;
public bool isPlaying = false;
private RawImage ri;
private BoFangManager boFangManager;
public static MoviePlayer Instance
{
get
{
return instance;
}
}
private void Awake()
{
audio0 = GetComponent<AudioSource>();
ri = GetComponent<RawImage>();
spriteInfo = GameObject.Find("spriteInfo").GetComponent<SpriteInfo>();
boFangManager = GameObject.Find("pauseBackGround").GetComponent<BoFangManager>();
}
void Start ()
{
instance = this;
Play0();
}
void Update ()
{
MoiveTimeEnd();
#if !UNITY_EDITOR && (UNITY_IOS || UNITY_ANDROID)
#else
#endif
}
void Play0()
{
ri.texture = spriteInfo.currentMov;
audio0.clip = spriteInfo.currentMov.audioClip;
movieTexture = (MovieTexture)ri.texture;
isPlaying = true;
movieTexture.Play();
audio0.Play();
}
public void Pause()
{
boFangManager.ShowThis();
isPlaying = false;
audio0.Pause();
movieTexture.Pause();
}
public void Play()
{
isPlaying = true;
audio0.Play();
movieTexture.Play();
}
public void Stop()
{
isPlaying = false;
audio0.Stop();
movieTexture.Stop();
SceneManager.LoadScene("Main");
Destroy(spriteInfo.gameObject);
}
public void MoiveTimeEnd()
{
if (isPlaying)
{
if (!movieTexture.isPlaying)
{
Stop();
StartCoroutine(ToTakePhoto());
}
}
}
IEnumerator ToTakePhoto()
{
yield return new WaitForEndOfFrame();
SceneManager.LoadScene("Main");
}
private void ShowButton()
{
if (isPlaying)
{
if (Input.GetMouseButtonUp(0))
{
Pause();
}
}
}
private void ShowButton0()
{
if (isPlaying)
{
if (Input.touchCount <= 0)
{
return;
}
if (Input.touchCount == 1)
{
if (Input.touches[0].phase == TouchPhase.Ended)
{
Pause();
}
}
}
}
public void ContinuePlay()
{
if (!isPlaying)
{
boFangManager.HideThis();
audio0.Play();
movieTexture.Play();
StartCoroutine(SetIsPlay());
}
}
IEnumerator SetIsPlay()
{
yield return new WaitForEndOfFrame();
isPlaying = true;
}
public void ReStart()
{
if (!isPlaying)
{
boFangManager.HideThis();
audio0.Stop();
movieTexture.Stop();
StartCoroutine(ReStartPlay());
}
}
IEnumerator ReStartPlay()
{
yield return new WaitForEndOfFrame();
isPlaying = true;
audio0.Play();
movieTexture.Play();
}
public void StopPlay()
{
if (!isPlaying)
{
audio0.Stop();
movieTexture.Stop();
Destroy(spriteInfo.gameObject);
SceneManager.LoadScene("Main");
}
}
}
四、将以上两个脚本挂载到场景中的物体上,并且为其赋值,运行项目,已经实现了该功能,如下图所示:
|