当前位置:网站首页>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
边栏推荐
- SQL Server knowledge gathering 9: modifying data
- 软考一般什么时候出成绩呢?在线蹬?
- Using tansformer to segment three-dimensional abdominal multiple organs -- actual battle of unetr
- MySQL insert data create trigger fill UUID field value
- MONAI版本更新到 0.9 啦,看看有什么新功能
- Cluster task scheduling system lsf/sge/slurm/pbs based on HPC scenario
- Prototype and prototype chain
- Simple and easy to modify spring frame components
- Find the root of equation ax^2+bx+c=0 (C language)
- leetcode-560:和为 K 的子数组
猜你喜欢

Socket communication principle and Practice

中级软件评测师考什么

优雅的 Controller 层代码

Find the greatest common divisor and the least common multiple (C language)

CSAPP bomb lab parsing

IIC Basics

对word2vec的一些浅层理解

MySQL insert data create trigger fill UUID field value
![1324: [example 6.6] integer interval](/img/5d/29db4d51ec7a2685f8aeffe5be68da.png)
1324: [example 6.6] integer interval

Is the soft test intermediate useful??
随机推荐
SQL Server knowledge gathering 9: modifying data
【安装系统】U盘安装系统教程,使用UltraISO制作U盘启动盘
1323: [example 6.5] activity selection
About hzero resource error (groovy.lang.missingpropertyexception: no such property: weight for class)
Installation and configuration of slurm resource management and job scheduling system
The mobile terminal automatically adjusts the page content and font size by setting rem
ArrayList线程不安全和解决方案
Kotlin realizes wechat interface switching (fragment exercise)
P1031 [NOIP2002 提高组] 均分纸牌
1324: [example 6.6] integer interval
[actual combat] transformer architecture of the major medical segmentation challenges on the list --nnformer
MONAI版本更新到 0.9 啦,看看有什么新功能
2022年7月10日“五心公益”活动通知+报名入口(二维码)
OpenGL glLightfv 函数的应用以及光源的相关知识
【PyTorch 07】 动手学深度学习——chapter_preliminaries/ndarray 习题动手版
Find the greatest common divisor and the least common multiple (C language)
How to prepare for the advanced soft test (network planning designer)?
[installation system] U disk installation system tutorial, using UltraISO to make U disk startup disk
Summary of router development knowledge
中级软件评测师考什么