Unity骚操作之:【注意这里只提供参考,还是希望你们用AvPro来播放】使用VideoPlayer组件播放视频【针对AvPro不支持unity2020 安卓移动端播放视频 只有声音 没有画面】

30 篇文章 0 订阅

 

 
			using System;
			using UnityEngine;
			using UnityEngine.Video;
			using UnityEngine.UI; 
			using Random = UnityEngine.Random;

			public class VideoPlayerTeach : UIBase
			{
				//图像
				public RawImage image;

				//播放器
				public VideoPlayer vPlayer;

				
				
				/// <summary>
				/// 默认是 Resource加载VideoClip的方式加载视频
				/// </summary>
				private bool isRes=true;
				/// <summary>
				/// 存放用Res加载到的视频剪辑【要求mp4文件 放到Resouces文件夹内】
				/// </summary>
				public VideoClip[] VideoClipsAry;
 

				/// <summary>
				/// 存放网络视频的地址 【需要用的时候 就要设置一下】
				/// </summary>
				public string urlNetWork = "http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4";  


				public Transform uiRoom;

				//播放
				public Button btn_Play;

				//暂停
				public Button btn_Pause;

				//前进
				public Button btn_Fornt;

				//后退
				public Button btn_Back;

				//下一个
				public Button btn_Next;

				//重新播放
				public Button btn_Again;

				//视频控制器
				public Slider sliderVideo;

				//音量控制器
				public Slider sliderSource;

				//音量大小
				public Text text;

				//当前视频时间
				public Text text_Time;

				//视频总时长
				public Text text_Count;

				//音频组件
				public AudioSource source;

				//需要添加播放器的物体
				public GameObject obj;

				//是否拿到视频总时长
				public bool isShow;

				//前进后退的大小
				public float numBer = 20f;

				//时 分的转换
				private int hour, mint;
				private float time;
				private float time_Count;

				private float time_Current;

				//视频是否播放完成
				private bool isVideo;



				private bool isStar = false;

		

				// Use this for initialization
				void LikeStart(Action callBack)
				{

					if (isStar == false)
					{

						uiRoom = transform.Find("uiRoom");

						if (isRes) //如果是 Res加载视频的方式
						{
							VideoClipsAry = Resources.LoadAll<VideoClip>("Video");
						}


						if (obj == null)
						{
							obj = transform.Find("RawImage").gameObject;
						}

						image = obj.GetComponent<RawImage>();

						if (image == null)
						{
							image.gameObject.AddComponent<RawImage>();
						}


						if (sliderVideo == null)
						{
							sliderVideo = uiRoom.transform.Find("sliderVideo").GetComponent<Slider>();
							sliderSource = uiRoom.transform.Find("sliderSource").GetComponent<Slider>();
						}

						text = uiRoom.transform.Find("txt_audioValue").GetComponent<Text>();
						text_Time = uiRoom.transform.Find("text_Time").GetComponent<Text>();
						text_Count = uiRoom.transform.Find("text_Count").GetComponent<Text>();



						if (btn_Play == null)
						{
							btn_Play = uiRoom.transform.Find("btn_Play").GetComponent<Button>();

							btn_Pause = uiRoom.transform.Find("btn_Pause").GetComponent<Button>();

							btn_Fornt = uiRoom.transform.Find("btn_Fornt").GetComponent<Button>();

							btn_Back = uiRoom.transform.Find("btn_Back").GetComponent<Button>();

							btn_Next = uiRoom.transform.Find("btn_Next").GetComponent<Button>();

							btn_Again = uiRoom.transform.Find("btn_Again").GetComponent<Button>();

						}

						//一定要动态添加这两个组件,要不然会没声音
						vPlayer = obj.AddComponent<VideoPlayer>();
						source = obj.AddComponent<AudioSource>();

						//这3个参数不设置也会没声音 唤醒时就播放关闭
						vPlayer.playOnAwake = false;
						source.playOnAwake = false;
						source.Pause();
				

						btn_Play.onClick.AddListener(delegate { OnClick(0); });
						btn_Pause.onClick.AddListener(delegate { OnClick(1); });
						btn_Fornt.onClick.AddListener(delegate { OnClick(2); });
						btn_Back.onClick.AddListener(delegate { OnClick(3); });
						btn_Next.onClick.AddListener(delegate { OnClick(4); });
						btn_Again.onClick.AddListener(delegate
						{
							//Init(urlNetWork);
							if (isRes)
							{
								Init(VideoClipsAry[Random.Range(0, VideoClipsAry.Length)]);
							}
							else
							{
								Init(urlNetWork);
							}

							vPlayer.Play();
						});

						sliderSource.value = source.volume;
						text.text = string.Format("{0:0}%", source.volume * 100);
						sliderSource.onValueChanged.AddListener(delegate { ChangeSource(sliderSource.value); });

						uiRoom.gameObject.SetActive(false);
						isStar = true;
					}
					
					
					
					if (callBack!=null)
					{
						callBack();
					}

				}

				/// <summary>
				/// 用URL的方式播放视频【并且初始化】
				/// </summary>
				/// <param name="url"></param>
				private void Init(string url)
				{
					
					isVideo = true;
					isShow = true;
					time_Count = 0;
					time_Current = 0;
					sliderVideo.value = 0;
					//设置为URL模式
					vPlayer.source = VideoSource.Url;
					//设置播放路径
					vPlayer.url = url;
					//在视频中嵌入的音频类型
					vPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;

					//把声音组件赋值给VideoPlayer
					vPlayer.SetTargetAudioSource(0, source);

					//当VideoPlayer全部设置好的时候调用
					vPlayer.prepareCompleted += Prepared;
					//启动播放器
					vPlayer.Prepare();
				}


				/// <summary>
				/// 用Res加载的方式 播放视频【并且初始化】
				/// </summary>
				/// <param name="clip"></param>
				private void Init(VideoClip clip)
				{
					isVideo = true;
					isShow = true;
					time_Count = 0;
					time_Current = 0;
					sliderVideo.value = 0;

					//设置为URL模式
					vPlayer.source = VideoSource.VideoClip;
					//设置播放路径
					vPlayer.clip = clip;
					//在视频中嵌入的音频类型
					vPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;

					//把声音组件赋值给VideoPlayer
					vPlayer.SetTargetAudioSource(0, source);

					//当VideoPlayer全部设置好的时候调用 
					vPlayer.prepareCompleted += Prepared;
					//启动播放器
					vPlayer.Prepare();


				}



				/// <summary>
				///     改变音量大小
				/// </summary>
				/// <param name="value"></param>
				public void ChangeSource(float value)
				{
					source.volume = value;
					text.text = string.Format("{0:0}%", value * 100);
				}

				/// <summary>
				///     改变视频进度
				/// </summary>
				/// <param name="value"></param>
				public void ChangeVideo(float value)
				{
					if (vPlayer.isPrepared)
					{
						vPlayer.time = (long) value;
						Debug.Log("VideoPlayer Time:" + vPlayer.time);
						time = (float) vPlayer.time;
						hour = (int) time / 60;
						mint = (int) time % 60;
						text_Time.text = string.Format("{0:D2}:{1:D2}", hour.ToString(), mint.ToString());
					}
				}

				private void OnClick(int num)
				{
					switch (num)
					{
						case 0:
							vPlayer.Play();
							Time.timeScale = 1;
							break;
						case 1:
							vPlayer.Pause();
							Time.timeScale = 0;
							break;
						case 2:
							sliderVideo.value = sliderVideo.value + numBer;
							break;
						case 3:
							sliderVideo.value = sliderVideo.value - numBer;
							break;
						case 4:
							vPlayer.Stop();
							Init(Application.streamingAssetsPath + "/EasyMovieTexture.mp4");
							break;
						default:
							break;
					}
				}

				// Update is called once per frame
				void Update()
				{
					if (vPlayer.isPlaying && isShow)
					{
						//把图像赋给RawImage
						image.texture = vPlayer.texture;
						//帧数/帧速率=总时长    如果是本地直接赋值的视频,我们可以通过VideoClip.length获取总时长
						sliderVideo.maxValue = vPlayer.frameCount / vPlayer.frameRate;

						time = sliderVideo.maxValue;
						hour = (int) time / 60;
						mint = (int) time % 60;
						text_Count.text = string.Format("/  {0:D2}:{1:D2}", hour.ToString(), mint.ToString());

						sliderVideo.onValueChanged.AddListener(delegate { ChangeVideo(sliderVideo.value); });
						isShow = !isShow;
					}

					if (Mathf.Abs((int) vPlayer.time - (int) sliderVideo.maxValue) == 0)
					{
						vPlayer.frame = (long) vPlayer.frameCount;
						vPlayer.Stop();
						Debug.Log("播放完成!");
						isVideo = false;
						return;
					}
					else if (isVideo && vPlayer.isPlaying)
					{
						time_Count += Time.deltaTime;
						if ((time_Count - time_Current) >= 1)
						{
							sliderVideo.value += 1;
							Debug.Log("value:" + sliderVideo.value);
							time_Current = time_Count;
						}
					}
				}

				private void FixedUpdate()
				{

				}

				void Prepared(VideoPlayer player)
				{
					player.Play();
				}


				public override void OnEnter()
				{
					 
				}

				public override void OnEnter(Action callBack = null)
				{
				 
				}

				public override void OnEnter(object obj, Action ballBack)
				{
					 
					LikeStart(delegate
					{
						//初始化
						if (isRes)
						{
							for (int i = 0; i < VideoClipsAry.Length; i++)
							{
								if (obj as string ==VideoClipsAry[i].name)
								{
									Init(VideoClipsAry[i]);
									return;
								}
							}
					
						}
						else
						{
							Init(urlNetWork);
						}
					});

					 
					gameObject.SetActive(true);
				}

				public override void OnClose()
				{
					 gameObject.SetActive(false);
				}
			}

 

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AD_喵了个咪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值