当前位置:网站首页>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 :
边栏推荐
- Former amd chip architect roast said that the cancellation of K12 processor project was because amd counseled!
- MIT 6.031 Reading5 : Version Control学习心得
- Common usage of OS (picture example)
- leetcode:42.接雨水
- [deeply understand tcapulusdb technology] one click installation of tmonitor background
- 微信小程序之input前加图标
- Digraph D and e
- DTU上报的数据值无法通过腾讯云规则引擎填入腾讯云数据库中
- Actual combat | how to make a slam track truth acquisition device?
- Basic use of stacks and queues
猜你喜欢

渗透测试-提权专题

通过 OpenVINO Model Server和 TensorFlow Serving简化部署

Stick to five things to get you out of your confusion!

Technology creates value and teaches you how to collect wool

Ks008 SSM based press release system

微信小程序之input前加图标

KS007基于JSP实现人个人博客系统

MySQL single database and table splitting using MYCAT

Quartus call & Design d Trigger - simulation & time sequence Wave Verification

Quarkus+saas multi tenant dynamic data source switching is simple and perfect
随机推荐
中断和轮询
Yyds dry inventory solution sword finger offer: judge whether it is a balanced binary tree
Instructions for laravel8 Beanstalk
Generics, generic defects and application scenarios that 90% of people do not understand
[deeply understand tcapulusdb technology] transaction execution of document acceptance
何小鹏:如果可以回到创业的时候 不会以自己的名字给产品命名
Js: get the maximum zindex (Z-index) value of the page
. Net how to use log framework NLog
PHP receiving and sending data
[deeply understand tcapulusdb technology] table management of document acceptance
白皮书丨英特尔携手知名RISC-V工具提供商Ashling,着力扩展多平台RISC-V支持
【深入理解TcaplusDB技术】如何实现Tmonitor单机安装
Technology creates value and teaches you how to collect wool
渗透测试-提权专题
Wechat applet pop up the optional menu from the bottom
Learning experiences on how to design reusable software from three aspects: class, API and framework
Simplify deployment with openvino model server and tensorflow serving
KS007基于JSP实现人个人博客系统
[deeply understand tcapulusdb technology] one click installation of tmonitor background
智能数字看板解决方案