Unity播放服务器端视频 发布到Android移动端

一、AVProVideo播视频插件

Unity版本 :Unity2020.3.21f1
AVProVido版本:AVPro Video 1.10.1.unitypackage
(AVProVido链接:https://pan.baidu.com/s/1gEbU1KMaRM8p9AjGHZJdLw?pwd=2022 提取码:2022)

二、制作过程

重点总结

第一阶段,建“播放UI”、“媒体播放器”。
第二阶段,把媒体播放器拖给播放UI。
第三阶段,设置媒体播放器,所播放服务器视频的URL。

具体步骤

准备:
导入AVProVideo1.10.1后,会有Bug

原因是与Unity版本不太适配——Bug显示与VR相关,点击控制台的报错,在VS中把相关代码注释掉或删掉,即可解决。
(注意,在注释代码的时候,它前后会有#开头的编辑器语句,要么保留完整的# #end组合,要么把这个组合完整的注释掉:因如果只注释掉开头的#语句,留着结束#end,#end处就会报错)
#开头的编辑器语句例子,如图
在这里插入图片描述
我这里注释掉后的代码,如下,以做参考

#if UNITY_5_4_OR_NEWER || (UNITY_5 && !UNITY_5_0 && !UNITY_5_1 && !UNITY_5_2 && !UNITY_5_3_0 && !UNITY_5_3_1 && !UNITY_5_3_2)
	#define UNITY_HAS_VRCLASS
#endif

using UnityEngine;

//-----------------------------------------------------------------------------
// Copyright 2015-2018 RenderHeads Ltd.  All rights reserverd.
//-----------------------------------------------------------------------------

namespace RenderHeads.Media.AVProVideo.Demos
{
    
    
	/// <summary>
	/// A demo for playing back 360 video on a mesh, handles rotation of the main camera
	/// Supports rotation by VR device, gyroscope or mouse
	/// </summary>
	public class SphereDemo : MonoBehaviour
	{
    
    
		[SerializeField]
		private bool _zeroCameraPosition = true;

		#pragma warning disable 0414    // suppress value not used warning
		[SerializeField]
		private bool _allowRecenter = false;

		[SerializeField]
		private bool _allowVrToggle = false;

		[SerializeField]
		private bool _lockPitch = false;

		#pragma warning restore 0414    // restore value not used warning

		private float _spinX;
		private float _spinY;

		private void Start()
		{
    
    
#if UNITY_HAS_VRCLASS
			//if (UnityEngine.VR.VRDevice.isPresent)
			//{
    
    
			//	return;
			//}
#endif
			if (SystemInfo.supportsGyroscope)
			{
    
    
				Input.gyro.enabled = true;
				this.transform.parent.Rotate(new Vector3(90f, 0f, 0f));
			}
		}

		private void OnDestroy()
		{
    
    
			if (SystemInfo.supportsGyroscope)
			{
    
    
				Input.gyro.enabled = false;
			}
		}

		void Update()
		{
    
    
#if UNITY_HAS_VRCLASS
			//if (UnityEngine.VR.VRDevice.isPresent)
			//{
    
    
			//	// Mouse click translates to gear VR touch to reset view
			//	if (_allowRecenter && (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space)))
			//	{
    
    
			//		UnityEngine.VR.InputTracking.Recenter();
			//	}
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
				//if (_allowVrToggle && Input.GetKeyDown(KeyCode.V))
				//{
    
    
				//	UnityEngine.VR.VRSettings.enabled = !UnityEngine.VR.VRSettings.enabled;
				//}
#endif
			//}
			//else
#endif
			//{
    
    
			//	if (SystemInfo.supportsGyroscope)
			//	{
    
    
			//		// Invert the z and w of the gyro attitude
			//		this.transform.localRotation = new Quaternion(Input.gyro.attitude.x, Input.gyro.attitude.y, -Input.gyro.attitude.z, -Input.gyro.attitude.w);
			//	}
			//	// Also rotate from mouse / touch input
			//	else 
			//	{
    
    
			//		if (Input.GetMouseButton(0))
			//		{
    
    
			//			float h = 40.0f * -Input.GetAxis("Mouse X") * Time.deltaTime;
			//			float v = 0f;
			//			if (!_lockPitch)
			//			{
    
    
			//				v = 40.0f * Input.GetAxis("Mouse Y") * Time.deltaTime;
			//			}						
			//			h = Mathf.Clamp(h, -0.5f, 0.5f);
			//			v = Mathf.Clamp(v, -0.5f, 0.5f);
			//			_spinX += h;
			//			_spinY += v;
			//		}
			//		if (!Mathf.Approximately(_spinX, 0f) || !Mathf.Approximately(_spinY, 0f))
			//		{
    
    
			//			this.transform.Rotate(Vector3.up, _spinX);
			//			this.transform.Rotate(Vector3.right, _spinY);

			//			_spinX = Mathf.MoveTowards(_spinX, 0f, 5f * Time.deltaTime);
			//			_spinY = Mathf.MoveTowards(_spinY, 0f, 5f * Time.deltaTime);
			//		}
			//	}
			//}
		}

		void LateUpdate()
		{
    
    
			if (_zeroCameraPosition)
			{
    
    
				Camera.main.transform.position = Vector3.zero;
			}
		}
	}
}
(1)因AVProVideo也是在UGUI的Canvas下播放,得先建立Canvas,然后在Canvas组件上,右键——>UI——>AVPro Video uGUI

在这里插入图片描述

(2)在Hierarchy层级视图空白处,右键——>AVPro Video——>Media Player,即建立媒体播放器

在这里插入图片描述

(3)把Media Player,拖给第1步建立的AVPro Video uGUI

在这里插入图片描述

(4)给上步建立的MediaPlayer,设置播放网址,依次设置如下

在这里插入图片描述

三、发布

发布到Android端如下(如果发布到IOS苹果端,注意MediaPlayer的Inspector视图,也是根据其中的报错对Player Settings进行设置)
1、注意下面两个图中的设置
图一、去除Vulkan
请添加图片描述
图二、改网络接入
请添加图片描述
2、关于部分机型,视频播放不了的问题
Unity使用AVProVideo安卓端无法播放http视频解决办法
注意:
(1)如果己方工程没有AndroidManifest.xml文件,需勾选Edit->Project Settings->Publishing Setting->Custom Main Manufest ,U3D 自动生成一个。
(2)里面第二个Xml文件,需自己创建——创建方法是:先建一个寻常的C#脚本,然后右键Show In Explorer,改文件后缀名为xml。

猜你喜欢

转载自blog.csdn.net/weixin_42935398/article/details/123735833
今日推荐