wxr
2021-12-15 e71c57108e6dd407c2c6f0361f68150f2ff9aed5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Graphics;
using Android.OS;
using Android.Runtime;
using Android.Support.V4.App;
using Android.Support.V4.Content;
using Android.Util;
using Android.Views;
using Android.Widget;
using Com.Lechange.Opensdk.Api;
using Com.Lechange.Opensdk.Device;
using Com.Lechange.Opensdk.Listener;
using Com.Lechange.Opensdk.Media;
using HDL_ON.DAL.Server;
using HDL_ON_Android.LeChengView.Form;
using Java.IO;
using Java.Lang;
 
namespace HDL_ON_Android
{
    [Activity(Label = "LeChengVideoActivity", WindowSoftInputMode = SoftInput.AdjustResize, LaunchMode = LaunchMode.SingleInstance, ConfigurationChanges = (ConfigChanges.Keyboard | ConfigChanges.KeyboardHidden | ConfigChanges.LayoutDirection | ConfigChanges.Locale | ConfigChanges.Orientation | ConfigChanges.ScreenSize), Theme = "@style/MyTheme1", ScreenOrientation = ScreenOrientation.Portrait)]
    public class LeChengVideoActivity : Activity, View.IOnClickListener
    {
        private static LeChengVideoActivity CurtActivity;
        //
        private string URL = "openapi.easy4ip.com:443"; //海外(正式)
        private static string URL_LC_CHINA = "openapi.lechange.cn:443"; //中国大陆(正式)
        private static string URL_LC_Overseas = "openapi.easy4ip.com:443"; //中国大陆(正式)
        public string AccessToken = "";
        public string DeviceId = "";
        public string Psk = "";
        public string PlayToken = "";//播放playToken
        public string CallId = "";//呼叫Id
        private int Type = 0;//0监控,1对讲
        private string DeviceName = "";// 备注
 
        /// <summary>
        /// 0:国内,1海外
        /// </summary>
        private int Location = 0;
 
        private FrameLayout flWindow;
        private FrameLayout flWindowContent;
        private ImageView ivBack;
        private TextView tvName;
        private MonitorFragment monitorFragment = null;
        private ReverseCallFragment reverseCallFragment = null;
 
        public LCOpenSDK_PlayWindow mPlayWindow;
 
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
 
            SetContentView(Resource.Layout.activity_video_phone);
 
            CurtActivity = this;
            AccessToken = Intent.GetStringExtra("AccessToken");//"St_0000d307a13c965c4db285469484490e"
            DeviceId = Intent.GetStringExtra("DeviceId");//6K056CFAAJ8FFA1
            Psk = Intent.GetStringExtra("Psk");//6K056CFAAJ8FFA1
            PlayToken = Intent.GetStringExtra("PlayToken"); // 播放playToken//"8a5e041afa674a2ba3e84d90a54ea962"
            Type = Intent.GetIntExtra("Type", 0);//0监控,1对讲//1
            Location = Intent.GetIntExtra("Location", 0);//1
            CallId = Intent.GetStringExtra("callId");//1384766990812426242
            DeviceName = Intent.GetStringExtra("DeviceName");
 
            InitLCSDK();
 
            IniView();
 
            IniData();
 
            Permissions();
 
            if (Type == 0)
            {
                monitorFragment = new MonitorFragment();
                FragmentManager.BeginTransaction().Replace(Resource.Id.content, monitorFragment).Commit();
            }
            else
            {
                reverseCallFragment = new ReverseCallFragment();
                FragmentManager.BeginTransaction().Replace(Resource.Id.content, reverseCallFragment).Commit();
            }
        }
 
        private void IniView()
        {
            //
            flWindowContent = (FrameLayout)FindViewById(Resource.Id.remoteFrame);
            flWindow = (FrameLayout)FindViewById(Resource.Id.live_window);
 
            ivBack = (ImageView)FindViewById(Resource.Id.videoBackImg);
            tvName = (TextView)FindViewById(Resource.Id.nameText);
 
            tvName.SetText(DeviceName, null);
            ivBack.SetOnClickListener(this);
 
        }
 
        private void IniData()
        {
            //
            //SetStatusBarColor();// 这里设计的TopBarColor是白色,与系统导航栏字体颜色一直,暂时屏蔽不用
 
            mPlayWindow = new LCOpenSDK_PlayWindow();
            mPlayWindow.InitPlayWindow(this, flWindowContent, 0, true);
 
           
        }
 
        /// <summary>
        ///  初始化乐橙库
        /// </summary>
        private void InitLCSDK()
        {
 
            if (Location == 1)
            {
                URL = URL_LC_Overseas;
            }else {
                URL = URL_LC_CHINA;
            }
 
            InitParams initParams = new InitParams(this, URL, AccessToken);
            LCOpenSDK_Api.InitOpenApi(initParams);
        }
 
        /// <summary>
        /// 开始播放(预览)
        /// </summary>
        public void Play()
        {
            try
            {
                LCOpenSDK_ParamReal paramReal = new LCOpenSDK_ParamReal(AccessToken, DeviceId, 0, Psk, PlayToken, 1, false, true);
                //PlayEventListener listener = new PlayEventListener();//实现播放事件监听回调 2021-10-15 目前不能监听,监听了会空异常
                //mPlayWindow.WindowListener = listener;//实现播放事件监听回调
                mPlayWindow.PlayRtspReal(paramReal);
            }
            catch (System.Exception e)
            {
                string sss = e.Message;
            }
        }
 
        /// <summary>
        /// 停止预览(停止播放画面)
        /// </summary>
        public void Stop()
        {
            mPlayWindow.StopRtspReal(true);//关闭视频
            //mPlayWindow.WindowListener = null;
        }
 
        /// <summary>
        /// 打开对讲
        /// </summary>
        public void StartTalk()
        {
            CloseAudio();// 关闭Audio
            LCOpenSDK_Talk.Listener = new AudioTalkerListener();//设置对讲监听
            //2021-10-29 对讲请求类型,call呼叫,如果不传,默认为talk
            string talkType = "talk";
            if (Type == 1)//0监控,1对讲
            {
                talkType = "call";
            }
            // 对讲参数
            LCOpenSDK_ParamTalk paramTalk = new LCOpenSDK_ParamTalk(AccessToken, DeviceId, -1, Psk, PlayToken, true, talkType);
            LCOpenSDK_Talk.PlayTalk(paramTalk);
        }
 
        /// <summary>
        /// 关闭对讲
        /// </summary>
        public void StopTalk()
        {
            LCOpenSDK_Talk.StopTalk();
            LCOpenSDK_Talk.Listener = null;
        }
 
        /// <summary>
        /// 打开声音(预览的时候可以听到门口机传来的声音)
        /// </summary>
        /// <returns></returns>
        public bool OpenAudio()
        {
            return mPlayWindow.PlayAudio() == 0;
        }
 
        /// <summary>
        /// 关闭声音(预览的时候关闭门口挤传来的声音,注意:打开对讲时需要先调用此方法关闭声音)
        /// </summary>
        /// <returns></returns>
        public bool CloseAudio()
        {
            return mPlayWindow.StopAudio() == 0;
        }
 
        /// <summary>
        /// 开锁
        /// </summary>
        public void Unlock()
        {
            //
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("deviceId", this.DeviceId);
            dic.Add("callId", this.CallId);
 
            var requestJson = HttpUtil.GetSignRequestJson(dic);
            var revertObj = HttpUtil.RequestHttpsPostFroHome("/home-wisdom/platform/imou/openDoorbell", requestJson);
            if (revertObj.Code == StateCode.SUCCESS)
            {
                Toast.MakeText(this, Resources.GetString(Resource.String.unlock_success), ToastLength.Short).Show();
            }
            else
            {
                Toast.MakeText(this, Resources.GetString(Resource.String.unlock_fail), ToastLength.Short).Show();
            }
        }
 
        ///// <summary>
        ///// 发送广播,通知刷新图库的显示
        ///// </summary>
        ///// <param name="path"></param>
        //void SendBroadcastScanFile(Context context, string path)
        //{
        //    // 发送广播,通知刷新图库的显示
        //    Intent scanIntent = new Intent(Intent.ActionMediaScannerScanFile);
        //    scanIntent.SetData(Android.Net.Uri.FromFile(new File(path)));
        //    context.SendBroadcast(scanIntent);
        //}
 
        /// <summary>
        /// 截图
        /// </summary>
        /// <param name="path"></param>
        public void SnapShot(string path)
        {
            try
            {
                int code = mPlayWindow.SnapShot(path);
                if (code == 0)
                {
                    //2021-09-01 增加发送广播,通知刷新图库的显示
                    HDLONUtils.SendBroadcastScanFile(this, path);
 
                    //Android.Provider.MediaStore.Images.Media.InsertImage(this.getContentResolver(), path, "ss", "ss");
                    Toast.MakeText(this, Resources.GetString(Resource.String.screenshot_success), ToastLength.Short).Show();
                }
                else
                {
                    Toast.MakeText(this, Resources.GetString(Resource.String.screenshot_fail), ToastLength.Short).Show();
                }
            }
            catch (System.Exception e)
            {
                string error = e.Message;
            }
            
            //运行到这,photoFile已经存在,这里需要获得这个文件的uri
            //* 分两种情况,android7.0以上和以下
            // 先存入临时目录,然后再移动到相册目录中
            //File photoFile = new File(path);
            //Android.Net.Uri photoUri;
 
            //if (Build.VERSION.SdkInt >= 24)
            //if (Build.VERSION.PreviewSdkInt >= 24)
            //{
 
            //    /**
            //     * FileProvider.getUriForFile(),这个方法中需要填写三个参数,
            //     * 第一个Context,
            //     * 第二个S
            //     * tring 任意
            //     * 第三个File
            //     */
            //    photoUri = FileProvider.GetUriForFile(this, "HDL Video SnapShot", photoFile);
 
            //}
            //else
            //{
            //    photoUri = Android.Net.Uri.FromFile(photoFile);
            //}
    
        }
 
        private ContentResolver getContentResolver()
        {
            throw new NotImplementedException();
        }
 
        /// <summary>
        /// 设置顶部状态栏背景透明
        /// </summary>
        private void SetStatusBarColor()
        {
            Window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
            Window.SetStatusBarColor(Color.Transparent);
        }
 
        /// <summary>
        /// 必要的一些权限
        /// </summary>
        private void Permissions()
        {
            string[] mPermissionList = new string[]
            {
                Manifest.Permission.WriteExternalStorage,
                Manifest.Permission.ReadExternalStorage,
                Manifest.Permission.Camera,
                Manifest.Permission.RecordAudio
            };
 
            foreach (string permissions in mPermissionList)
            {
                if (ContextCompat.CheckSelfPermission(this, permissions) != 0)
                {
                    ActivityCompat.RequestPermissions(this, new string[] { permissions }, 1);
                }
            }
        }
 
        /// <summary>
        /// 点击事件
        /// </summary>
        /// <param name="v"></param>
        public void OnClick(View v)
        {
            if (v.Equals(ivBack))
            {
                this.Finish();
            }
        }
 
        protected override void OnDestroy()
        {
            base.OnDestroy();
 
            Stop();
            mPlayWindow.UninitPlayWindow();//销毁底层资源
        }
 
        /// <summary>
        /// 对讲监听
        /// </summary>
        class AudioTalkerListener : LCOpenSDK_TalkerListener
        {
            /// <summary>
            /// 对讲结果回调
            /// </summary>
            /// <param name="error"></param>
            /// <param name="type"></param>
            public override void OnTalkResult(string error, int type)
            {
                base.OnTalkResult(error, type);
 
                try
                {
                    System.Console.WriteLine("LC OnTalkResult error:" + error + "  type:" + type);
                    bool talkResult = false;
                    if (type == 99 || error.Equals("-1000") || error.Equals("0") || error.Equals("1") || error.Equals("3"))
                    {
                        talkResult = false;
                    }
                    else if (error.Equals("4"))
                    {
                        talkResult = true;
                    }
 
                    if (!talkResult)
                    {
                        // 提示对讲打开失败
                        CurtActivity.StopTalk();
                        CurtActivity.RunOnUiThread(() =>
                        {
                            Toast.MakeText(CurtActivity, Resource.String.talk_open_failed, ToastLength.Short).Show();
                        });
                    }
                    else
                    {
                        CurtActivity.reverseCallFragment.PostAnswer();// post接听电话,给云端做记录
                        CurtActivity.reverseCallFragment.TimeStarts();//接通成功打开计时器
                    }
                }
                catch { }
            }
 
            public override void OnTalkPlayReady()
            {
                base.OnTalkPlayReady();
            }
 
            public override void OnAudioReceive(byte[] bytes, int p1, int p2, int p3, int p4)
            {
                base.OnAudioReceive(bytes, p1, p2, p3, p4);
            }
 
            public override void OnAudioRecord(byte[] bytes, int p1, int p2, int p3, int p4)
            {
                base.OnAudioRecord(bytes, p1, p2, p3, p4);
            }
 
            public override void OnDataLength(int i)
            {
                base.OnDataLength(i);
            }
        }
 
        /// <summary>
        /// 播放、预览结果回调
        /// resultSource:  0--RTSP  1--HLS  5--DHHTTP  99--OPENAPI
        /// </summary>
        class PlayEventListener : LCOpenSDK_EventListener
        {
            public override void OnPlayerResult(int index, string code, int resultSource)
            {
                base.OnPlayerResult(index, code, resultSource);
                System.Console.WriteLine("LC OnPlayerResult index:" + index + "  code:" + code + "  resultSource:"+ resultSource);
            }
 
           
        }
    }
}