当前位置:网站首页>Unity realizes the function of playing Ogg format video
Unity realizes the function of playing Ogg format video
2022-06-23 14:09:00 【Poposwich】
Unity To play Ogg Format video function implementation
Preface
I have worked on projects before , It involves the development of the video playing function , I usually use it directly Unity Self encapsulated VideoPlayer Just develop ,VideoPlayer It's more powerful , At the same time, it is relatively stable . But in some older projects , Sometimes you can use play ogg Video format development function . I have done similar functions in several previous projects , So here is a record of the use Unity Develop and play Ogg Video features .
step
One 、 Create a new... In the scene RawImage Components , Used as a canvas for displaying video textures , As shown in the figure below :
Two 、 newly build SpriteInfo.cs Script , The implementation will Ogg Video incoming function , The code is as follows :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class SpriteInfo : MonoBehaviour
{
public MovieTexture currentMov;// The movie
private string videoFileName;
private string videoName;
public double startTime;
// Profile class
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>
/// Loading the movie's coroutine
/// </summary>
/// <returns> return www</returns>
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(" Movie loading time :" + startTime);
}
}
3、 ... and 、 newly build MoviePlayer.cs Script , Realize video playback function , The code is as follows :
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;
/// <summary>
/// Control the playback of the movie
/// </summary>
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
}
/// <summary>
/// Initialize playback of movie
/// </summary>
void Play0()
{
ri.texture = spriteInfo.currentMov;
audio0.clip = spriteInfo.currentMov.audioClip;
movieTexture = (MovieTexture)ri.texture;
isPlaying = true;
movieTexture.Play();
audio0.Play();
//Debug.Log("Play");
}
/// <summary>
/// Play pause
/// </summary>
public void Pause()
{
boFangManager.ShowThis();
isPlaying = false;
audio0.Pause();
movieTexture.Pause();
//Debug.Log(" Pause ");
}
/// <summary>
/// Continue playing
/// </summary>
public void Play()
{
isPlaying = true;
audio0.Play();
movieTexture.Play();
//Debug.Log(" Play ");
}
/// <summary>
/// Stop playing
/// </summary>
public void Stop()
{
isPlaying = false;
audio0.Stop();
movieTexture.Stop();
SceneManager.LoadScene("Main");
Destroy(spriteInfo.gameObject);
//Debug.Log("Stop");
}
/// <summary>
/// The logic after the movie is played
/// </summary>
public void MoiveTimeEnd()
{
if (isPlaying)
{
if (!movieTexture.isPlaying)
{
Stop();
StartCoroutine(ToTakePhoto());
}
}
}
/// <summary>
/// Jump to the cooperation process of the photographing interface
/// </summary>
/// <returns></returns>
IEnumerator ToTakePhoto()
{
yield return new WaitForEndOfFrame();
SceneManager.LoadScene("Main");
}
/// <summary>
/// Computer side pause
/// </summary>
private void ShowButton()
{
if (isPlaying)
{
if (Input.GetMouseButtonUp(0))
{
Pause();
}
}
}
/// <summary>
/// Android mobile pause
/// </summary>
private void ShowButton0()
{
if (isPlaying)
{
if (Input.touchCount <= 0)
{
return;
}
if (Input.touchCount == 1)// One finger touches the screen
{
if (Input.touches[0].phase == TouchPhase.Ended)
{
Pause();
}
}
}
}
/// <summary>
/// Continue playing
/// </summary>
public void ContinuePlay()
{
if (!isPlaying)
{
boFangManager.HideThis();
audio0.Play();
movieTexture.Play();
StartCoroutine(SetIsPlay());
}
}
/// <summary>
/// Set up isPlaying by true, Otherwise it will conflict
/// </summary>
/// <returns> Return the time of one frame </returns>
IEnumerator SetIsPlay()
{
yield return new WaitForEndOfFrame();
isPlaying = true;
}
/// <summary>
/// Replay function
/// </summary>
public void ReStart()
{
if (!isPlaying)
{
boFangManager.HideThis();
audio0.Stop();
movieTexture.Stop();
StartCoroutine(ReStartPlay());
}
}
/// <summary>
/// Replay the executed process
/// </summary>
/// <returns> Returns a frame time </returns>
IEnumerator ReStartPlay()
{
yield return new WaitForEndOfFrame();
isPlaying = true;
audio0.Play();
movieTexture.Play();
}
/// <summary>
/// Stop playing the movie and enter the scene of taking pictures
/// </summary>
public void StopPlay()
{
if (!isPlaying)
{
audio0.Stop();
movieTexture.Stop();
Destroy(spriteInfo.gameObject);
SceneManager.LoadScene("Main");
}
}
}
Four 、 Mount the above two scripts to the objects in the scene , And assign it a value , Run the project , This function has been implemented , As shown in the figure below :
边栏推荐
- White paper - Intel and Ashling, a well-known risc-v tool provider, strive to expand multi platform risc-v support
- Crmeb second open SMS function tutorial
- One way linked list implementation -- counting
- 4-way telephone +1-way Gigabit Ethernet 4-way PCM telephone optical transceiver
- 2022软科大学专业排名出炉!西电AI专业排名超清北,南大蝉联全国第一 !
- Detailed explanation of kubernetes log monitoring system architecture
- Web technology sharing | [Gaode map] to realize customized track playback
- [deeply understand tcapulusdb technology] tmonitor background one click installation
- 2022 年以后,对AI开发人员意味着什么
- Quarkus+saas multi tenant dynamic data source switching is simple and perfect
猜你喜欢

Shutter clip clipping component

How to solve the task cache compilation problem caused by gradle build cache

深入剖析MobileNet和它的变种

面向 PyTorch* 的英特尔 扩展助力加速 PyTorch

How to correctly calculate the number of rows imported into EXCEL (poi/npoi)

Add Icon before input of wechat applet

微信小程序之获取php后台数据库转化的json
Detailed explanation of kubernetes log monitoring system architecture
![[deeply understand tcapulusdb technology] table management of document acceptance](/img/59/e435623ab9cc7d2aa164c2f08e135f.png)
[deeply understand tcapulusdb technology] table management of document acceptance

Shell process control - 39. Special process control statements
随机推荐
人脸注册,解锁,响应,一网打尽
KDD 2022 | epileptic wave prediction based on hierarchical graph diffusion learning
微信小程序之从底部弹出可选菜单
实战 | 如何制作一个SLAM轨迹真值获取装置?
使用OpenVINOTM预处理API进一步提升YOLOv5推理性能
How to solve the task cache compilation problem caused by gradle build cache
Multi-Camera Detection of Social Distancing Reference Implementation
Input adjustment of wechat applet
MySQL single database and table splitting using MYCAT
quartus調用&設計D觸發器——仿真&時序波驗證
How to use sed -i command
Understand ADT and OOP
kali使用
【深入理解TcaplusDB技术】 Tmonitor模块架构
2022 soft science university professional ranking released! Xi'an electric AI ranked higher than Qingbei, and Nantah ranked first in the country!
Vulnhub target os-hacknos-1
栈和队列的基本使用
SQLserver2008r2安装dts组件不成功
Scope of groovy
互联网技术发展内卷后的出路——iVX的诞生