当前位置:网站首页>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
边栏推荐
- [actual combat] transformer architecture of the major medical segmentation challenges on the list --nnformer
- Prototype and prototype chain
- 软考信息处理技术员有哪些备考资料与方法?
- I'd rather say simple problems a hundred times than do complex problems once
- 【实战】霸榜各大医学分割挑战赛的Transformer架构--nnFormer
- 使用Tansformer分割三维腹部多器官--UNETR实战
- Pre knowledge reserve of TS type gymnastics to become an excellent TS gymnastics master
- CAS mechanism
- 【推荐系统 02】DeepFM、YoutubeDNN、DSSM、MMOE
- IIC基本知识
猜你喜欢
Prototype and prototype chain
Socket communication principle and Practice
南航 PA3.1
使用Tansformer分割三维腹部多器官--UNETR实战
Unable to open kernel device '\.\vmcidev\vmx': operation completed successfully. Reboot after installing vmware workstation? Module "devicepoweron" failed to start. Failed to start the virtual machine
Pre knowledge reserve of TS type gymnastics to become an excellent TS gymnastics master
“梦想杯”2017 年江苏省信息与未来小学生夏令营 IT 小能手 PK 之程序设计试题
[recommendation system 01] rechub
【安装系统】U盘安装系统教程,使用UltraISO制作U盘启动盘
Mendeley--免费的文献管理工具,给论文自动插入参考文献
随机推荐
宁愿把简单的问题说一百遍,也不把复杂的问题做一遍
JS实现链式调用
在线硬核工具
ThreadLocal is not enough
Typescript interface inheritance
[detailed explanation of Huawei machine test] tall and short people queue up
SQL Server 知识汇集11 : 约束
Applet jump to H5, configure business domain name experience tutorial
使用 load_decathlon_datalist (MONAI)快速加载JSON数据
香橙派OrangePi 4 LTS开发板通过Mini PCIE连接SATA硬盘的操作方法
施努卡:机器视觉定位技术 机器视觉定位原理
使用U2-Net深层网络实现——证件照生成程序
BigDecimal value comparison
How much review time does it usually take to take the intermediate soft exam?
Elegant controller layer code
Deep understanding of Apache Hudi asynchronous indexing mechanism
【实战】霸榜各大医学分割挑战赛的Transformer架构--nnFormer
无法打开内核设备“\\.\VMCIDev\VMX”: 操作成功完成。是否在安装 VMware Workstation 后重新引导? 模块“DevicePowerOn”启动失败。 未能启动虚拟机。
What does intermediate software evaluator test
多线程-异步编排