当前位置:网站首页>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
边栏推荐
- BUUCTF---Reverse---reverse1
- Différences entre les contraintes monotones et anti - monotones
- Summary of router development knowledge
- [actual combat] transformer architecture of the major medical segmentation challenges on the list --nnformer
- P2788 数学1(math1)- 加减算式
- CSAPP Bomb Lab 解析
- leetcode-560:和为 K 的子数组
- 【PyTorch 07】 动手学深度学习——chapter_preliminaries/ndarray 习题动手版
- The mobile terminal automatically adjusts the page content and font size by setting rem
- Applet jump to H5, configure business domain name experience tutorial
猜你喜欢
BUUCTF---Reverse---reverse1
1324: [example 6.6] integer interval
南航 PA3.1
[installation system] U disk installation system tutorial, using UltraISO to make U disk startup disk
使用 load_decathlon_datalist (MONAI)快速加载JSON数据
Some superficial understanding of word2vec
如何顺利通过下半年的高级系统架构设计师?
I plan to take part in security work. How about information security engineers and how to prepare for the soft exam?
Simple and easy to modify spring frame components
Trajectory planning for multi-robot systems: Methods and applications 综述阅读笔记
随机推荐
Some online academic report websites and machine learning videos
小程序跳转H5,配置业务域名经验教程
Elegant controller layer code
[牛客网刷题 Day6] JZ27 二叉树的镜像
openinstall与虎扑达成合作,挖掘体育文化产业数据价值
[installation system] U disk installation system tutorial, using UltraISO to make U disk startup disk
软考一般什么时候出成绩呢?在线蹬?
BUUCTF---Reverse---reverse1
Basic introduction of yarn and job submission process
Hdu-2196 tree DP learning notes
MONAI版本更新到 0.9 啦,看看有什么新功能
长列表性能优化方案 memo
The difference between monotonicity constraint and anti monotonicity constraint
PHP \ newline cannot be output
香橙派OrangePi 4 LTS开发板通过Mini PCIE连接SATA硬盘的操作方法
【推薦系統 01】Rechub
What are the test preparation materials and methods for soft exam information processing technicians?
CAS机制
IIC Basics
The gun startles the dragon, and the crowd "locks" Zhou Zhi