当前位置:网站首页>How to play video on unityui
How to play video on unityui
2022-07-07 10:48:00 【HCC2017】
Realize in unity There are many kinds of videos playing on :
The first is to create a new Cube Put the video file directly into Cube You can play it by running on
The second is to use plug-ins EasyMovieTexturet Or is it AVProVideo
The third kind of
Play a movie on the mobile terminal , When you pack, you will find Unity It will remind you that it is similar to “ Not found Movie Texture type ” Such a mistake , This is because mobile platforms do not support this class . So we need to use that Handheld.PlayFullScreenMovie Method to realize video playback on mobile platform . Mobile platform supports .mov, .mp4, .mpv, and .3gp Video in four formats .
Handheld.PlayFullScreenMovie The method is dedicated to mobile platforms (IOS,Android) Play the video , When the video is playing ,Unity It will stop running , Until the end of the video . That is, when the video is over , Will start executing the next line of code in this line .
Handheld.PlayFullScreenMovie Method has four overloaded methods , Here we introduce each parameter :
path:
Represents the path to the file . The movie to be loaded must be placed in Assets/ StreamingAssets/ Under the path , This path is the root directory of the movie file , If there is no subdirectory , Directly through “Movie.mp4” File name to indicate Movie Path to file .
bgColor:
Represents the background color . Usually specified as Color.black.
controlMode:
Indicates the control mode of the video , This is a FullScreenMovieControlMode Type of structure , Four members in total :
FullScreenMovieControlMode.Full Means to apply a complete video controller when playing video , Such as progress bar 、 Pause and wait ;
FullScreenMovieControlMode.Minimal It means to apply the simplest video controller when playing video ;
FullScreenMovieControlMode.CancelOnInput It means that when playing the video, click the screen to close the video ;
FullScreenMovieControlMode.Hidden It means that it does not accept any control , Until the video is played .
scalingMode:
Represents the zoom mode of the video , This is a FullScreenMovieScalingMode Type of structure , There are four members :
FullScreenMovieScalingMode.None Don't zoom the video ;
FullScreenMovieScalingMode.AspectFit Zoom the video until it fits the screen ;
FullScreenMovieScalingMode.AspectFill Zoom the video until it fills the entire screen ;
FullScreenMovieScalingMode.Fill Zoom the video until the length and width fit the screen .
A fourth On the first code
// Images
public RawImage image;
// player
public VideoPlayer vPlayer;
// Play
public Button btn_Play;
// Pause
public Button btn_Pause;
// Video controller
public Slider sliderVideo;
// Current video time
public Text text_Time;
// Current video time delivery
public int textTime=0;
// Total video duration
public Text text_Count;
// Total video delivery time
public int textCount=0;
// Audio components
//public AudioSource source;
// Need to add player objects
public GameObject obj;
// Whether to get the total length of video
public bool isShow=false;
// The size of forward and backward
public float numBer = 20f;
// when Conversion of points
private int hour, mint;
private float time;
private float time_Count;
private float time_Current;
// Whether the video playback is complete
public bool isVideo;
private bool isBoFangfang = true;
// Use this for initialization
void Start () {
image = obj.GetComponent<RawImage>();
vPlayer = obj.AddComponent<VideoPlayer>();
// this 3 There will be no sound without setting parameters Play off when you wake up
vPlayer.playOnAwake = false;
// source.playOnAwake = false;
// source.Pause();
isShow = true;
// initialization string url = Application.streamingAssetsPath + "/EasyMovieTexture.mp4";
//Init(Application.streamingAssetsPath + "/GuangChangWu.mp4");
Init(Application.streamingAssetsPath + "/1.mp4");
btn_Play.onClick.AddListener(delegate { OnClick(0); });
btn_Pause.onClick.AddListener(delegate { OnClick(1); });
// sliderVideo.onValueChanged.AddListener(delegate { ChangeVideo(sliderVideo.value); });
}
/// <summary>
/// initialization VideoPlayer
/// </summary>
/// <param name="url"></param>
private void Init(string url) {
isVideo = true;
isShow = true;
time_Count = 0;
time_Current = 0;
sliderVideo.value = 0;
// Set to URL Pattern
vPlayer.source = VideoSource.Url;
// Set playback path
vPlayer.url = url;
// Audio type embedded in video
vPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
// Assign the sound component to VideoPlayer
// vPlayer.SetTargetAudioSource(0, source);
// When VideoPlayer Call when all settings are set
// vPlayer.prepareCompleted += Prepared;
// Start the player
vPlayer.Prepare();
}
private void OnClick(int num) {
switch (num)
{
case 0:
isShow = true;
isBoFangfang = true;
vPlayer.Play();
isVideo = true;
break;
case 1:
isBoFangfang = false;
isShow = false;
vPlayer.Pause();
break;
default:
break;
}
}
void Update ()
{
if (!isBoFangfang)
{
vPlayer.Pause();
}
if (vPlayer.isPlaying && isShow)
{
// Assign the image to RawImage
image.texture = vPlayer.texture;
// frames / Frame rate = Total duration If it is a video with local direct assignment , We can go through VideoClip.length Get the total duration
sliderVideo.maxValue = vPlayer.frameCount/vPlayer.frameRate;
time = sliderVideo.maxValue;
hour = (int)time / 60;
mint = (int)time % 60;
textCount = hour * 60 + mint;
text_Count.text = string.Format("/ {0:D2}:{1:D2}", hour.ToString(), mint.ToString());
}
if (Mathf.Abs((int)vPlayer.time - (int)sliderVideo.maxValue) == 0)
{
vPlayer.frame = (long)vPlayer.frameCount;
vPlayer.Stop();
Debug.Log(" The playback is finished !");
isVideo = false;
sliderVideo.value = 0;
hour = 0;
mint = 0;
text_Time.text = string.Format("{0:D2}:{1:D2}", hour.ToString(), mint.ToString());
return;
}
else if (isVideo && vPlayer.isPlaying)
{
time_Count += Time.deltaTime;
if ((time_Count - time_Current) >=1)
{
sliderVideo.value += 1f;
time_Current = time_Count;
time = (float)vPlayer.time;
hour = (int)time / 60;
mint = (int)time % 60;
textTime = hour * 60 + mint;
text_Time.text = string.Format("{0:D2}:{1:D2}", hour.ToString(), mint.ToString());
}
}
}
The principle is to use unity Medium VideoPlayer Component assignment and control, etc
newly build canves newly build RawImage object , Just hang the script on the object
边栏推荐
- ADB utility commands (network package, log, tuning related)
- Simple and easy to modify spring frame components
- 如何顺利通过下半年的高级系统架构设计师?
- Is the soft test intermediate useful??
- 无法打开内核设备“\\.\VMCIDev\VMX”: 操作成功完成。是否在安装 VMware Workstation 后重新引导? 模块“DevicePowerOn”启动失败。 未能启动虚拟机。
- When do you usually get grades in the soft exam? Online pedaling?
- [recommendation system 02] deepfm, youtubednn, DSSM, MMOE
- [installation system] U disk installation system tutorial, using UltraISO to make U disk startup disk
- 555电路详解
- 多线程-异步编排
猜你喜欢

【机器学习 03】拉格朗日乘子法

Operation method of Orange Pie orangepi 4 lts development board connecting SATA hard disk through mini PCIe

Summary of router development knowledge

【实战】霸榜各大医学分割挑战赛的Transformer架构--nnFormer

Openinstall and Hupu have reached a cooperation to mine the data value of sports culture industry

“梦想杯”2017 年江苏省信息与未来小学生夏令营 IT 小能手 PK 之程序设计试题

中级软件评测师考什么

路由器开发知识汇总

I plan to take part in security work. How about information security engineers and how to prepare for the soft exam?
![1323: [example 6.5] activity selection](/img/2e/ba74f1c56b8a180399e5d3172c7b6d.png)
1323: [example 6.5] activity selection
随机推荐
[牛客网刷题 Day6] JZ27 二叉树的镜像
MySQL insert data create trigger fill UUID field value
What are the contents of the intermediate soft test, the software designer test, and the test outline?
南航 PA3.1
Jump to the mobile terminal page or PC terminal page according to the device information
【推荐系统 02】DeepFM、YoutubeDNN、DSSM、MMOE
【PyTorch 07】 动手学深度学习——chapter_preliminaries/ndarray 习题动手版
Those confusing concepts (3): function and class
想考中级软考,一般需要多少复习时间?
What does intermediate software evaluator test
Deep understanding of Apache Hudi asynchronous indexing mechanism
Installation and configuration of slurm resource management and job scheduling system
Pre knowledge reserve of TS type gymnastics to become an excellent TS gymnastics master
The difference between monotonicity constraint and anti monotonicity constraint
软考信息处理技术员有哪些备考资料与方法?
Socket communication principle and Practice
Five simple and practical daily development functions of chrome are explained in detail. Unlock quickly to improve your efficiency!
Basic introduction of yarn and job submission process
JS实现链式调用
如何顺利通过下半年的高级系统架构设计师?