快速开始
更新时间:2023-04-17
设置LicenseID(AppId)并初始化播放器
用户在使用SDK之前需要去百度智能云控制台申请并下载.license文件放到自己工程下,并将LicenseID设置给播放器。
LicenseID、包名、证书文件一一对应,所以可能需要按平台设置对应的ID。
C#
1using BDCloud.MetaMediaSDK;
2
3#if UNITY_ANDROID
4 // android
5 private static string appId = "your-android-license-id";
6#elif UNITY_IOS
7 // ios
8 private static string appId = "your-ios-license-id";
9#endif
10private UnityXplayer xplayer;
11xplayer = new UnityXplayer(appId);
创建Shader和Material
针对Android和iOS平台,需要选择不同的Shader。
C#
1private Shader playerShader = null;
2private Material playerMaterial = null;
3
4#if UNITY_ANDROID
5 playerShader = Shader.Find("Xplayer/AndroidOESShader");
6#else
7 playerShader = Shader.Find("Xplayer/YUVShader");
8#endif
9
10playerMaterial = new Material(playerShader);
11
12// 将material设置给你想要显示视频画面的GameObject
13private GameObject cube0 = GameObject.Find("Cube0");
14if (cube0 != null) {
15 cube0.GetComponent<Renderer>().material = playerMaterial;
16}
设置监听回调
C#
1// SEI信息回调
2class MyUnityXplayerSEIListener : UnityXplayerSEIListener
3{
4 public override int OnSEI(byte[] buf)
5 {
6 Debug.Log("OnSEI");
7 seiBuf = buf;
8 return 0;
9 }
10}
11
12// 错误码回调,具体错误码含义参考接口与错误码速查页面
13private static int errorCode = 0;
14class MyUnityXplayerErrorListener : UnityXplayerErrorListener
15{
16 private UnityXplayer player = null;
17 public MyUnityXplayerErrorListener(UnityXplayer player)
18 {
19 this.player = player;
20 }
21
22 public override int OnError(int _errorCode)
23 {
24 Debug.Log("OnError: " + _errorCode);
25 errorCode = _errorCode;
26
27 // 在错误回调中可以自定义处理逻辑,如重新播放
28 this.player.Stop();
29 this.player.OpenUrl(this.player.GetUrl());
30
31 return 0;
32 }
33}
34
35// 信息码回调,具体信息码含义参考接口与错误码速查页面
36class MyUnityXplayerInfoListener : UnityXplayerInfoListener
37{
38 public override int OnInfo(int infoCode)
39 {
40 Debug.Log("OnInfo: " + infoCode);
41 return 0;
42 }
43}
44
45private UnityXplayerSEIListener seiListener = new MyUnityXplayerSEIListener();
46private UnityXplayerErrorListener errorListener = new MyUnityXplayerInfoListener();
47private UnityXplayerInfoListener infoListener = new MyUnityXplayerErrorListener(xplayer);
创建Android平台的显示Surface
对于Android平台,需要提前在渲染线程创建Surface用于绘制视频内容
C#
1#if UNITY_ANDROID
2 GL.IssuePluginEvent(RenderThreadHandlePtr, CREATE_SURFACE_INTERNAL);
3#endif
传入URL并开始播放
C#
1xplayer.OpenUrl(url);
在Update事件函数中渲染上屏并且响应播放监听回调
因为Android平台和iOS平台的渲染机制不同,所以在Update事件函数中也要根据平台做不同的处理。
C#
1void Update()
2{
3// 在iOS平台上,渲染基于Y纹理和UV纹理
4#if UNITY_IOS || UNITY_STANDALONE_OSX
5 GL.IssuePluginEvent(UnityXplayerCPP.UnityRenderEvent(xplayer.GetPlayer(), METAL_LOCK));
6 if (texY == null) {
7 IntPtr ytex = (IntPtr)xplayer.GetYTex(yTexWidth, yTexHeight);
8 if (ytex != IntPtr.Zero && ytex != null) {
9 texY = Texture2D.CreateExternalTexture (Marshal.ReadInt32(yTexWidth), Marshal.ReadInt32(yTexHeight), TextureFormat.R8, false, false, ytex);
10 }
11 }
12
13 if (texUV == null) {
14 IntPtr uvtex = (IntPtr)xplayer.GetUVTex(uvTexWidth, uvTexHeight);
15 if (uvtex != IntPtr.Zero && uvtex != null) {
16 texUV = Texture2D.CreateExternalTexture (Marshal.ReadInt32(uvTexWidth), Marshal.ReadInt32(uvTexHeight), TextureFormat.RG16, false, false, uvtex);
17 }
18 }
19
20 if (texY != null && texUV != null) {
21 IntPtr ytex = (IntPtr)xplayer.GetYTex(yTexWidth, yTexHeight);
22 IntPtr uvtex = (IntPtr)xplayer.GetUVTex(uvTexWidth, uvTexHeight);
23 if (ytex != IntPtr.Zero && ytex != null && uvtex != IntPtr.Zero && uvtex != null) {
24
25 texY.UpdateExternalTexture (ytex);
26 texUV.UpdateExternalTexture (uvtex);
27
28 foreach (Renderer renderer in rendererList)
29 {
30 if (renderer != null) {
31 renderer.sharedMaterial.SetTexture("_YTex", texY);
32 renderer.sharedMaterial.SetTexture("_UVTex", texUV);
33 renderer.sharedMaterial.SetInt("_Type", 3);
34 }
35 }
36
37 int width = Marshal.ReadInt32(yTexWidth);
38 int height = Marshal.ReadInt32(yTexHeight);
39
40 if (renderTexture != null && playerMaterial != null)
41 {
42 playerMaterial.SetTexture("_YTex", texY);
43 playerMaterial.SetTexture("_UVTex", texUV);
44 playerMaterial.SetInt("_Type", 3);
45
46 // 判断尺寸发生变化
47 if (renderTexture.width != width || renderTexture.height != height)
48 {
49 renderTexture.Release();
50 renderTexture.width = width;
51 renderTexture.height = height;
52 }
53
54 Graphics.Blit(null, renderTexture, playerMaterial);
55 }
56
57 }
58 }
59#elif UNITY_ANDROID
60 // 在Android平台上渲染基于OES纹理
61 GL.IssuePluginEvent(RenderThreadHandlePtr, UPDATE_TEX);
62 if (texOES == null) {
63 int oestex = xplayer.GetOesTex();
64 if (oestex > 0) {
65 yTexWidth = xplayer.GetVideoWidth();
66 yTexHeight = xplayer.GetVideoHeight();
67 texOES = Texture2D.CreateExternalTexture (yTexWidth, yTexHeight,
68 TextureFormat.RGBA32, false, false, new System.IntPtr(oestex));
69 }
70 }
71
72 if (texOES != null) {
73 int oestex = xplayer.GetOesTex();
74 if (oestex > 0) {
75 yTexWidth = xplayer.GetVideoWidth();
76 yTexHeight = xplayer.GetVideoHeight();
77 if (texOES.width != yTexWidth || texOES.height != yTexHeight) {
78 texOES = Texture2D.CreateExternalTexture (yTexWidth, yTexHeight,
79 TextureFormat.RGBA32, false, false, new System.IntPtr(oestex));
80 }
81
82 texOES.UpdateExternalTexture (new System.IntPtr(oestex));
83
84 foreach (Renderer renderer in rendererList)
85 {
86 if (renderer != null) {
87 renderer.sharedMaterial.SetTexture("_MainTex", texOES);
88 }
89 }
90
91 if (renderTexture != null && playerMaterial != null)
92 {
93 playerMaterial.SetTexture("_MainTex", texOES);
94
95 // 判断尺寸发生变化
96 if (renderTexture.width != yTexWidth || renderTexture.height != yTexHeight)
97 {
98 renderTexture.Release();
99 renderTexture.width = yTexWidth;
100 renderTexture.height = yTexHeight;
101 }
102
103 Graphics.Blit(null, renderTexture, playerMaterial);
104 }
105
106 }
107 }
108#endif
109
110 // 处理SEI回调
111 byte[] seiBuf = xplayer.GetSEI();
112 if (seiBuf != null) {
113 if(seiListener != null){
114 seiListener.OnSEI(seiBuf);
115 }
116 }
117
118 // 处理错误回调
119 int errorCode = xplayer.GetErrorCode();
120 if (errorCode != 0)
121 {
122 if (errorListener != null)
123 {
124 errorListener.OnError(errorCode);
125 }
126 }
127
128 // 处理事件回调
129 int infoCode = xplayer.GetInfo();
130 if (infoCode != 0)
131 {
132 if (infoListener != null)
133 {
134 infoListener.OnInfo(infoCode);
135 }
136 }
137}
播放控制
暂停播放
C#
1xplayer.Pause();
恢复播放
C#
1xplayer.Resume();
进度SEEK
C#
1xplayer.SeekTo(time);
倍速播放
C#
1xplayer.SetSpeed(speed);
循环播放
C#
1xplayer.SetLooping(looping);
音量设置
C#
1xplayer.SetVolume(volume);
停止播放并释放资源
C#
1xplayer.Stop()
以上流程在Demo工程的DebugUI.cs
和Player.cs
中进行了详细的展示,可以参考。
Player.cs
对常见的播放功能进行了封装,适用于不需要太多自定义功能的开发者直接使用。