当前位置:网站首页>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
边栏推荐
- Trajectory planning for multi-robot systems: Methods and applications 综述阅读笔记
- 多线程-异步编排
- Prototype and prototype chain
- 2022.7.3DAY595
- 施努卡:机器人视觉抓取工作原理 机器视觉抓取
- Operation method of Orange Pie orangepi 4 lts development board connecting SATA hard disk through mini PCIe
- 深入理解Apache Hudi异步索引机制
- 打算参加安全方面工作,信息安全工程师怎么样,软考考试需要怎么准备?
- TypeScript 接口继承
- SQL Server knowledge collection 11: Constraints
猜你喜欢
【机器学习 03】拉格朗日乘子法
Network engineer test questions and answers in May of the first half of 2022
[recommendation system 01] rechub
Using U2 net deep network to realize -- certificate photo generation program
Applet jump to H5, configure business domain name experience tutorial
Application of OpenGL gllightfv function and related knowledge of light source
Simple and easy to modify spring frame components
ADB utility commands (network package, log, tuning related)
Multithreaded asynchronous orchestration
IIC Basics
随机推荐
CSAPP bomb lab parsing
[homework] 2022.7.6 write your own cal function
CAS mechanism
长列表性能优化方案 memo
單調性約束與反單調性約束的區別 monotonicity and anti-monotonicity constraint
ArrayList thread insecurity and Solutions
About hzero resource error (groovy.lang.missingpropertyexception: no such property: weight for class)
简单易修改的弹框组件
OpenGL glLightfv 函数的应用以及光源的相关知识
深入分析ERC-4907协议的主要内容,思考此协议对NFT市场流动性意义!
Deeply analyze the main contents of erc-4907 agreement and think about the significance of this agreement to NFT market liquidity!
ADB utility commands (network package, log, tuning related)
IIC基本知识
南航 PA3.1
BigDecimal value comparison
Is the gold content of intermediate e-commerce division in the soft exam high?
[recommendation system 01] rechub
Leetcode-560: subarray with sum K
gym安装踩坑记录
Applet jump to H5, configure business domain name experience tutorial