Unity实现播放Ogg格式视频功能实现

Unity实现播放Ogg格式视频功能实现

前言

我在之前做过的项目中,涉及到视频播放这一块功能的开发上,一般我就直接用Unity自己封装的VideoPlayer开发就可以了,VideoPlayer的功能比较强大,同时也比较稳定。但是在有的比较老的项目中,有时候还会使用播放ogg视频格式的开发功能。我在之前的几个项目中也做过类似的功能,所以就在这里记录一下使用Unity开发播放Ogg视频的功能。

步骤

一、在场景中新建一个RawImage组件,用来当做显示视频纹理的画布,如下图所示:
Unity实现播放Ogg格式视频功能实现_第1张图片
二、新建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());
	}
    /// 
    /// 加载影片的协程
    /// 
    /// 返回www
    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;
            //Debug.Log("影片加载用时:" + startTime);
        }
    }

三、新建MoviePlayer.cs脚本,实现视频播放功能,代码如下所示:

using System.Collections;
//using System.Collections.Generic;
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>();
    }
    // Use this for initialization
    void Start ()
    {
        instance = this;
        Play0();
	}
	// Update is called once per frame
	void Update ()
    {
        MoiveTimeEnd();
#if !UNITY_EDITOR && (UNITY_IOS || UNITY_ANDROID)
        //ShowButton0();
#else
        //ShowButton();
#endif
    }
    /// 
    /// 初始化播放影片
    /// 
    void Play0()
    {
        ri.texture = spriteInfo.currentMov;
        audio0.clip = spriteInfo.currentMov.audioClip;
        movieTexture = (MovieTexture)ri.texture;
        isPlaying = true;
        movieTexture.Play();
        audio0.Play();
        //Debug.Log("Play");
    }
    /// 
    /// 播放暂停
    /// 
    public void Pause()
    {
        boFangManager.ShowThis();
        isPlaying = false;
        audio0.Pause();
        movieTexture.Pause();
        //Debug.Log("暂停");
    }
    /// 
    /// 继续播放
    /// 
    public void Play()
    {
        isPlaying = true;
        audio0.Play();
        movieTexture.Play();
        //Debug.Log("播放");
    }
    /// 
    /// 停止播放
    /// 
    public void Stop()
    {
        isPlaying = false;
        audio0.Stop();
        movieTexture.Stop();
        SceneManager.LoadScene("Main");
        Destroy(spriteInfo.gameObject);
        //Debug.Log("Stop");
    }
    /// 
    /// 影片播放完之后的逻辑
    /// 
    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());
        }
    }
    /// 
    /// 设置isPlaying为true,否则会冲突
    /// 
    /// 返回一帧的时间
    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");
        }
    }
}

四、将以上两个脚本挂载到场景中的物体上,并且为其赋值,运行项目,已经实现了该功能,如下图所示:

你可能感兴趣的:(Untiy小技巧,unity,音视频,游戏引擎)