wxr
2022-11-24 2af932533ef851bf983385244e9912976dbd4daa
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
package com.lechange.demo.ui;
 
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.drawable.BitmapDrawable;
import android.hardware.SensorManager;
import android.media.MediaScannerConnection;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.widget.PopupWindowCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.OrientationEventListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
 
import com.common.openapi.ClassInstanceManager;
import com.common.openapi.DeviceLocalCacheService;
import com.common.openapi.DeviceRecordService;
import com.common.openapi.IGetDeviceInfoCallBack;
import com.common.openapi.MethodConst;
import com.common.openapi.entity.CloudRecordsData;
import com.common.openapi.entity.ControlMovePTZData;
import com.common.openapi.entity.DeviceDetailListData;
import com.common.openapi.entity.DeviceLocalCacheData;
import com.common.openapi.entity.LocalRecordsData;
import com.common.openapi.entity.RecordsData;
import com.lechange.common.log.Logger;
import com.lechange.demo.R;
import com.lechange.demo.adapter.MediaPlayRecordAdapter;
import com.lechange.demo.dialog.EncryptKeyInputDialog;
import com.lechange.demo.handler.ActivityHandler;
import com.lechange.demo.tools.DateHelper;
import com.lechange.demo.tools.DeviceAbilityHelper;
import com.lechange.demo.tools.MediaPlayHelper;
import com.lechange.demo.view.Direction;
import com.lechange.demo.view.LcCloudRudderView;
import com.lechange.demo.view.LcPopupWindow;
import com.lechange.opensdk.api.LCOpenSDK_Api;
import com.lechange.opensdk.listener.LCOpenSDK_EventListener;
import com.lechange.opensdk.listener.LCOpenSDK_TalkerListener;
import com.lechange.opensdk.media.LCOpenSDK_ParamReal;
import com.lechange.opensdk.media.LCOpenSDK_ParamTalk;
import com.lechange.opensdk.media.LCOpenSDK_PlayWindow;
import com.lechange.opensdk.media.LCOpenSDK_Talk;
import com.mm.android.deviceaddmodule.LCDeviceEngine;
import com.mm.android.deviceaddmodule.device_wifi.DeviceConstant;
import com.mm.android.deviceaddmodule.mobilecommon.utils.LogUtil;
import com.mm.android.deviceaddmodule.utils.LCUtils;
 
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
public class DeviceOnlineMediaPlayActivity extends AppCompatActivity implements View.OnClickListener, IGetDeviceInfoCallBack.IDeviceCacheCallBack {
    private static final String TAG = DeviceOnlineMediaPlayActivity.class.getSimpleName();
 
    public LCOpenSDK_PlayWindow mPlayWin = new LCOpenSDK_PlayWindow();
    private DeviceDetailListData.ResponseData.DeviceListBean deviceListBean;
    private Bundle bundle;
    private FrameLayout frLiveWindow, frLiveWindowContent;
    private TextView tvNoVideo, tvDeviceName, tvCloudVideo, tvLocalVideo, tvLoadingMsg,tvPixel;
    private RecyclerView rcvVideoList;
    private LinearLayout llVideoContent, llVideo, llSpeak, llScreenShot, llCloudStage, llFullScreen, llSound, llPlayStyle, llPlayPause, llDetail, llBack, llVideo1, llSpeak1, llScreenShot1, llCloudStage1;
    private ImageView ivPalyPause, ivPlayStyle, ivSound, ivCloudStage, ivScreenShot, ivSpeak, ivVideo, ivCloudStage1, ivScreenShot1, ivSpeak1, ivVideo1;
    private ProgressBar pbLoading;
    private RelativeLayout rlLoading;
    private LcCloudRudderView rudderView;
    private boolean cloudvideo = true;
    private boolean isShwoRudder = false;
    private boolean showHD = true;//显示HD和SD的切换
    private AudioTalkerListener audioTalkerListener = new AudioTalkerListener();
 
    private VideoMode videoMode = VideoMode.MODE_HD;
    private SoundStatus soundStatus = SoundStatus.PLAY;
    private SoundStatus soundStatusPre = SoundStatus.PLAY;
    private SpeakStatus speakStatus = SpeakStatus.STOP;
    private RecordStatus recordStatus = RecordStatus.STOP;
    private PlayStatus playStatus = PlayStatus.ERROR;
    private LinearLayoutManager linearLayoutManager;
    private MediaPlayRecordAdapter mediaPlayRecordAdapter;
    private List<RecordsData> recordsDataList = new ArrayList<>();
    private List<RecordsData> cloudRecordsDataList;
    private List<RecordsData> localRecordsDataList;
    private String cloudRecordsDataTip = "";
    private String localRecordsDataTip = "";
    private DeviceRecordService deviceRecordService = ClassInstanceManager.newInstance().getDeviceRecordService();
    private Direction mPTZPreDirection = null;
    private int mCurrentOrientation;
    private LinearLayout llController;
    private FrameLayout frRecord;
    private RelativeLayout rlTitle;
    private ImageView ivChangeScreen;
    private EncryptKeyInputDialog encryptKeyInputDialog;
    private String encryptKey;
    private boolean supportPTZ;
    private String videoPath = null;
    // 屏幕方向改变监听器
    private OrientationEventListener mOrientationEventListener;
 
    private int mVideoViewCurrentOrientationRequest = NO_ORIENTATION_REQUEST;
    private LcPopupWindow lcPopupWindow;
    private int imageSize = -1;//视频播放分辨率
    private int bateMode;
    private TextView tvCoverStream; //debug模式显示拉流方式
    private ImageView ivLimitLeft;
    private ImageView ivLimitRight;
    private ImageView ivLimitUp;
    private ImageView ivLimitDown;
 
 
    public enum PlayStatus {
        PLAY, PAUSE, ERROR
    }
 
    public enum LoadStatus {
        LOADING, LOAD_SUCCESS, LOAD_ERROR
    }
 
    public enum SoundStatus {
        PLAY, STOP, NO_SUPPORT
    }
 
    public enum SpeakStatus {
        PLAY, STOP, NO_SUPPORT,OPENING
    }
 
    public enum VideoMode {
        MODE_HD, MODE_SD
    }
 
    public enum RecordStatus {
        START, STOP
    }
 
    private Handler mHandler;
 
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        mHandler = new ActivityHandler(this) {
            @Override
            public void handleMsg(Message msg) {
                switch (msg.what) {
                    case MSG_REQUEST_ORIENTATION:
                        requestedOrientation(msg.arg1, false);
                        break;
                    default:
                        break;
                }
               // handlePlayMessage(msg);
            }
 
        };
        super.onCreate(savedInstanceState);
        mCurrentOrientation = Configuration.ORIENTATION_PORTRAIT;
        initOrientationEventListener();
        setContentView(R.layout.activity_device_online_media_play);
        initView();
        initData();
        operatePTZ();
    }
 
    protected void requestedOrientation(int requestedOrientation, boolean isForce) {
 
        if (!isForce) {
 
            if (mVideoViewCurrentOrientationRequest == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
 
                /*
                 * // 如果是开启全屏开关,禁止竖屏 if (requestedOrientation ==
                 * ActivityInfo.SCREEN_ORIENTATION_PORTRAIT || requestedOrientation ==
                 * ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) { return; }
                 */
                mVideoViewCurrentOrientationRequest = NO_ORIENTATION_REQUEST;
 
            } else {
 
                if (mVideoViewCurrentOrientationRequest == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
 
                    if (requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
 
                        // 如果监听器发出的请求是横屏,表示屏幕处于横屏,直接返回;
                        mVideoViewCurrentOrientationRequest = NO_ORIENTATION_REQUEST;
                        return;
                    }
 
                    if (requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
 
                        // 如果监听器发出的请求和当前开关方向保持一致时,表示屏幕已经恢复到制定的竖屏,将开关请求状态重置;
                        mVideoViewCurrentOrientationRequest = NO_ORIENTATION_REQUEST;
                        return;
                    }
 
                    // 如果监听器发出的请求与当前开关方向不一致时,表示屏幕已经转到其他方向,将开关请求状态重置,并发出相应的请求;
                    mVideoViewCurrentOrientationRequest = NO_ORIENTATION_REQUEST;
                }
 
                // 如果开关没有发出请求,或者开关请求状态已经重置,则直接发出相应请求;
 
            }
 
        } else {
            mVideoViewCurrentOrientationRequest = requestedOrientation;
        }
 
        try {
           setRequestedOrientation(requestedOrientation);
        } catch (Exception e) {
        }
    }
 
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        mCurrentOrientation = newConfig.orientation;
        super.onConfigurationChanged(newConfig);
        switchScreenDirection();
    }
 
    private void operatePTZ() {
        rudderView.setRudderListener(new LcCloudRudderView.RudderListener() {
            @Override
            public void onSteeringWheelChangedBegin() {
 
            }
 
            @Override
            public void onSteeringWheelChangedContinue(Direction direction) {
                if (direction == null && mPTZPreDirection != null) {
                    controlPTZ(mPTZPreDirection, 200, true);
                    mPTZPreDirection = null;
                } else if (direction != mPTZPreDirection) {
                    // 方向不同才更新云台指示图标
                    mPTZPreDirection = direction;
                    controlPTZ(mPTZPreDirection, 30000, false);
                }
            }
 
            @Override
            public void onSteeringWheelChangedSingle(Direction direction) {
                controlPTZ(direction, 200, false);
            }
 
            @Override
            public void onSteeringWheelChangedEnd() {
            }
        });
    }
 
    private void controlPTZ(Direction em, long time, boolean stop) {
        String operation = "";
        if (em == Direction.Left) {
            operation = "2";
        } else if (em == Direction.Right) {
            operation = "3";
        } else if (em == Direction.Up) {
            operation = "0";
        } else if (em == Direction.Down) {
            operation = "1";
        }
        if (stop) {
            operation = "10";
        }
        ControlMovePTZData controlMovePTZData = new ControlMovePTZData();
        controlMovePTZData.data.deviceId = deviceListBean.deviceId;
        controlMovePTZData.data.channelId = deviceListBean.channels.get(deviceListBean.checkedChannel).channelId;
        controlMovePTZData.data.operation = operation;
        controlMovePTZData.data.duration = time;
        deviceRecordService.controlMovePTZ(controlMovePTZData);
    }
 
    private void initCloudRecord() {
        if ((cloudRecordsDataTip != null && !cloudRecordsDataTip.isEmpty()) || cloudRecordsDataList != null) {
            if (cloudRecordsDataList != null) {
                showRecordList(cloudRecordsDataList);
            } else {
                showRecordListTip(cloudRecordsDataTip);
            }
        } else {
            getCloudRecord();
           /* deviceRecordService.queryCloudUse(deviceListBean.deviceId, deviceListBean.channels.get(deviceListBean.checkedChannel).channelId, new IGetDeviceInfoCallBack.ICommon<Integer>() {
 
                @Override
                public void onCommonBack(Integer response) {
                    if (response==-1||response==0){
                        cloudRecordsDataTip = getResources().getString(R.string.lc_demo_device_cloud_not_open);
                        showRecordListTip(cloudRecordsDataTip);
                    }else{
                        getCloudRecord();
                    }
 
                }
 
                @Override
                public void onError(Throwable throwable) {
                    Toast.makeText(DeviceOnlineMediaPlayActivity.this,throwable.getMessage(),Toast.LENGTH_SHORT).show();
                }
            });*/
        }
    }
 
 
    private void getCloudRecord(){
        CloudRecordsData cloudRecordsData = new CloudRecordsData();
        cloudRecordsData.data.deviceId = deviceListBean.deviceId;
        cloudRecordsData.data.channelId = deviceListBean.channels.get(deviceListBean.checkedChannel).channelId;
        cloudRecordsData.data.beginTime = DateHelper.dateFormat(new Date(System.currentTimeMillis())) + " 00:00:00";
        cloudRecordsData.data.endTime = DateHelper.dateFormat(new Date(System.currentTimeMillis())) + " 23:59:59";
        cloudRecordsData.data.count = 6;
        deviceRecordService.getCloudRecords(cloudRecordsData, new IGetDeviceInfoCallBack.IDeviceCloudRecordCallBack() {
            @Override
            public void deviceCloudRecord(CloudRecordsData.Response result) {
                List<CloudRecordsData.ResponseData.RecordsBean> cloudRecords = result.data.records;
                if (cloudRecords != null && cloudRecords.size() > 0) {
                    cloudRecordsDataList = new ArrayList<>();
                    for (CloudRecordsData.ResponseData.RecordsBean recordsBean : cloudRecords) {
                        RecordsData recordsData = RecordsData.parseCloudData(recordsBean);
                        cloudRecordsDataList.add(recordsData);
                    }
                    showRecordList(cloudRecordsDataList);
                } else {
                    queryCloudUseState(deviceListBean.deviceId, deviceListBean.channels.get(deviceListBean.checkedChannel).channelId);
                }
            }
 
            @Override
            public void onError(Throwable throwable) {
                cloudRecordsDataTip = throwable .getMessage();
                showRecordListTip(cloudRecordsDataTip);
            }
        });
    }
 
    /**
     * 查询套餐使用情况
     * @param deviceId
     * @param channelId
     */
    private void queryCloudUseState(String deviceId,String channelId){
        deviceRecordService.queryCloudUse(deviceId,channelId, new IGetDeviceInfoCallBack.ICommon<Integer>() {
            @Override
            public void onCommonBack(Integer response) {
                switch (response){
                    case -1://未开通
                        cloudRecordsDataTip = getResources().getString(R.string.lc_demo_device_cloud_state_not_opened);
                        break;
                    case 0://过期
                        cloudRecordsDataTip = getResources().getString(R.string.lc_demo_device_cloud_state_expired);
                        break;
                    case 1://使用中
                        cloudRecordsDataTip = getResources().getString(R.string.lc_demo_device_more_video);
                        break;
                    case 2://暂停
                        cloudRecordsDataTip = getResources().getString(R.string.lc_demo_device_cloud_state_suspended);
                        break;
                }
                showRecordListTip(cloudRecordsDataTip);
            }
            @Override
            public void onError(Throwable throwable) {
                cloudRecordsDataTip = throwable .getMessage();
                showRecordListTip(cloudRecordsDataTip);
            }
        });
    }
 
    private void showRecordList(List<RecordsData> list) {
        rcvVideoList.setVisibility(View.VISIBLE);
        tvNoVideo.setVisibility(View.GONE);
        recordsDataList.clear();
        for (RecordsData a : list) {
            recordsDataList.add(a);
        }
        if (mediaPlayRecordAdapter == null) {
            mediaPlayRecordAdapter = new MediaPlayRecordAdapter(recordsDataList, DeviceOnlineMediaPlayActivity.this);
            rcvVideoList.setAdapter(mediaPlayRecordAdapter);
        } else {
            mediaPlayRecordAdapter.notifyDataSetChanged();
        }
        //加载更多
        mediaPlayRecordAdapter.setLoadMoreClickListener(new MediaPlayRecordAdapter.LoadMoreClickListener() {
            @Override
            public void loadMore() {
                gotoRecordList();
            }
        });
        //进入视频片段播放页
        mediaPlayRecordAdapter.setOnItemClickListener(new MediaPlayRecordAdapter.OnItemClickListener() {
            @Override
            public void click(int recordType, int position) {
                Bundle bundle = new Bundle();
                bundle.putSerializable(MethodConst.ParamConst.deviceDetail, deviceListBean);
                bundle.putSerializable(MethodConst.ParamConst.recordData, recordsDataList.get(position));
                bundle.putInt(MethodConst.ParamConst.recordType, recordsDataList.get(position).recordType == 0 ? MethodConst.ParamConst.recordTypeCloud : MethodConst.ParamConst.recordTypeLocal);
                Intent intent = new Intent(DeviceOnlineMediaPlayActivity.this, DeviceRecordPlayActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }
 
    private void gotoRecordList() {
        if (cloudvideo) {
            Bundle bundle = new Bundle();
            bundle.putSerializable(MethodConst.ParamConst.deviceDetail, deviceListBean);
            bundle.putInt(MethodConst.ParamConst.recordType, MethodConst.ParamConst.recordTypeCloud);
            Intent intent = new Intent(DeviceOnlineMediaPlayActivity.this, DeviceRecordListActivity.class);
            intent.putExtras(bundle);
            startActivity(intent);
        } else {
            Bundle bundle = new Bundle();
            bundle.putSerializable(MethodConst.ParamConst.deviceDetail, deviceListBean);
            bundle.putInt(MethodConst.ParamConst.recordType, MethodConst.ParamConst.recordTypeLocal);
            Intent intent = new Intent(DeviceOnlineMediaPlayActivity.this, DeviceRecordListActivity.class);
            intent.putExtras(bundle);
            startActivity(intent);
        }
    }
 
    private void showRecordListTip(String txt) {
        rcvVideoList.setVisibility(View.GONE);
        tvNoVideo.setVisibility(View.VISIBLE);
        tvNoVideo.setText(txt);
    }
 
    private void initLocalRecord() {
        if ((localRecordsDataTip != null && !localRecordsDataTip.isEmpty()) || localRecordsDataList != null) {
            if (localRecordsDataList != null) {
                showRecordList(localRecordsDataList);
            } else {
                showRecordListTip(localRecordsDataTip);
            }
        } else {
            getLocalRecord();
          /*  deviceRecordService.querySDUse(deviceListBean.deviceId, new IGetDeviceInfoCallBack.ICommon<String>() {
                @Override
                public void onCommonBack(String response) {
                    if (!"empty".equals(response)){
                        getLocalRecord();
                    }else{
                        localRecordsDataTip = getResources().getString(R.string.lc_demo_device_local_sd);
                        showRecordListTip(localRecordsDataTip);
                    }
                }
                @Override
                public void onError(Throwable throwable) {
                    Toast.makeText(DeviceOnlineMediaPlayActivity.this,throwable.getMessage(),Toast.LENGTH_SHORT).show();
                }
            });*/
 
        }
    }
 
    private void getLocalRecord(){
        LocalRecordsData localRecordsData = new LocalRecordsData();
        localRecordsData.data.deviceId = deviceListBean.deviceId;
        localRecordsData.data.channelId = deviceListBean.channels.get(deviceListBean.checkedChannel).channelId;
        localRecordsData.data.beginTime = DateHelper.dateFormat(new Date(System.currentTimeMillis())) + " 00:00:00";
        localRecordsData.data.endTime = DateHelper.dateFormat(new Date(System.currentTimeMillis())) + " 23:59:59";
        localRecordsData.data.type = "All";
        localRecordsData.data.queryRange = "1-6";
        deviceRecordService.queryLocalRecords(localRecordsData, new IGetDeviceInfoCallBack.IDeviceLocalRecordCallBack() {
            @Override
            public void deviceLocalRecord(LocalRecordsData.Response result) {
                List<LocalRecordsData.ResponseData.RecordsBean> localRecords = result.data.records;
                if (localRecords != null && localRecords.size() > 0) {
                    localRecordsDataList = new ArrayList<>();
                    for (LocalRecordsData.ResponseData.RecordsBean recordsBean : localRecords) {
                        RecordsData recordsData = RecordsData.parseLocalData(recordsBean);
                        localRecordsDataList.add(recordsData);
                    }
                    showRecordList(localRecordsDataList);
                } else {
                    localRecordsDataTip = getResources().getString(R.string.lc_demo_device_more_video);
                    showRecordListTip(localRecordsDataTip);
                }
            }
 
            @Override
            public void onError(Throwable throwable) {
                localRecordsDataTip = throwable.getMessage();
                showRecordListTip(localRecordsDataTip);
            }
        });
    }
 
    private void initView() {
        frLiveWindow = findViewById(R.id.fr_live_window);
        frLiveWindowContent = findViewById(R.id.fr_live_window_content);
        tvCoverStream = findViewById(R.id.tv_cover_stream);
        tvCloudVideo = findViewById(R.id.tv_cloud_video);
        tvLocalVideo = findViewById(R.id.tv_local_video);
        llBack = findViewById(R.id.ll_back);
        tvDeviceName = findViewById(R.id.tv_device_name);
        llDetail = findViewById(R.id.ll_detail);
        llPlayPause = findViewById(R.id.ll_paly_pause);
        llPlayStyle = findViewById(R.id.ll_play_style);
        llSound = findViewById(R.id.ll_sound);
        llFullScreen = findViewById(R.id.ll_fullscreen);
        llCloudStage = findViewById(R.id.ll_cloudstage);
        llScreenShot = findViewById(R.id.ll_screenshot);
        llSpeak = findViewById(R.id.ll_speak);
        llVideo = findViewById(R.id.ll_video);
        llVideoContent = findViewById(R.id.ll_video_content);
        rcvVideoList = findViewById(R.id.rcv_video_list);
        tvNoVideo = findViewById(R.id.tv_no_video);
        rudderView = findViewById(R.id.rudder);
        ivPalyPause = findViewById(R.id.iv_paly_pause);
        ivPlayStyle = findViewById(R.id.iv_play_style);
        tvPixel = findViewById(R.id.tv_play_pixel);
        ivSound = findViewById(R.id.iv_sound);
        ivCloudStage = findViewById(R.id.iv_cloudStage);
        ivScreenShot = findViewById(R.id.iv_screen_shot);
        ivSpeak = findViewById(R.id.iv_speak);
        ivVideo = findViewById(R.id.iv_video);
 
        llCloudStage1 = findViewById(R.id.ll_cloudstage1);
        llScreenShot1 = findViewById(R.id.ll_screenshot1);
        llSpeak1 = findViewById(R.id.ll_speak1);
        llVideo1 = findViewById(R.id.ll_video1);
        ivCloudStage1 = findViewById(R.id.iv_cloudStage1);
        ivScreenShot1 = findViewById(R.id.iv_screen_shot1);
        ivSpeak1 = findViewById(R.id.iv_speak1);
        ivVideo1 = findViewById(R.id.iv_video1);
 
        rlLoading = findViewById(R.id.rl_loading);
        pbLoading = findViewById(R.id.pb_loading);
        tvLoadingMsg = findViewById(R.id.tv_loading_msg);
        llController = findViewById(R.id.ll_controller);
        frRecord = findViewById(R.id.fr_record);
        rlTitle = findViewById(R.id.rl_title);
        ivChangeScreen = findViewById(R.id.iv_change_screen);
        ivLimitLeft = findViewById(R.id.iv_direction_limit_left);
        ivLimitRight = findViewById(R.id.iv_direction_limit_right);
        ivLimitUp = findViewById(R.id.iv_direction_limit_up);
        ivLimitDown = findViewById(R.id.iv_direction_limit_down);
        linearLayoutManager = new LinearLayoutManager(DeviceOnlineMediaPlayActivity.this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rcvVideoList.setLayoutManager(linearLayoutManager);
        initCommonClickListener();
        // 初始化播放窗口
        switchScreenDirection();
        mPlayWin.initPlayWindow(this, frLiveWindowContent, 0, false);
        setWindowListener(mPlayWin);
        mPlayWin.openTouchListener();//开启收拾监听
 
 
    }
 
    private void switchScreenDirection() {
        if (mCurrentOrientation == Configuration.ORIENTATION_PORTRAIT) {
            RelativeLayout.LayoutParams mLayoutParams = new RelativeLayout.LayoutParams(frLiveWindow.getLayoutParams());
            DisplayMetrics metric = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metric);
            mLayoutParams.width = metric.widthPixels; // 屏幕宽度(像素)
            mLayoutParams.height = metric.widthPixels * 9 / 16;
            mLayoutParams.setMargins(0, 0, 0, 0);
            mLayoutParams.addRule(RelativeLayout.BELOW, R.id.rl_title);
            frLiveWindow.setLayoutParams(mLayoutParams);
            MediaPlayHelper.quitFullScreen(DeviceOnlineMediaPlayActivity.this);
            llController.setVisibility(View.GONE);
            rlTitle.setVisibility(View.VISIBLE);
            llSpeak1.setVisibility(View.GONE);
            llCloudStage1.setVisibility(View.GONE);
            llVideo1.setVisibility(View.GONE);
            llScreenShot1.setVisibility(View.GONE);
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(frRecord
                    .getLayoutParams());
            layoutParams.addRule(RelativeLayout.BELOW, R.id.ll_controller);
            frRecord.setLayoutParams(layoutParams);
            FrameLayout.LayoutParams layoutParams3 = new FrameLayout.LayoutParams(rudderView.getLayoutParams());
            layoutParams3.gravity = Gravity.CENTER;
            rudderView.setLayoutParams(layoutParams3);
            switchCloudRudder(false);
        } else if (mCurrentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
            DisplayMetrics metric = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metric);
            RelativeLayout.LayoutParams mLayoutParams = new RelativeLayout.LayoutParams(metric.widthPixels, metric.heightPixels);
            mLayoutParams.setMargins(0, 0, 0, 0);
            mLayoutParams.removeRule(RelativeLayout.BELOW);
            frLiveWindow.setLayoutParams(mLayoutParams);
            MediaPlayHelper.setFullScreen(DeviceOnlineMediaPlayActivity.this);
            llController.setVisibility(View.GONE);
            rlTitle.setVisibility(View.GONE);
            llSpeak1.setVisibility(View.GONE);
            llCloudStage1.setVisibility(View.VISIBLE);
            llVideo1.setVisibility(View.GONE);
            llScreenShot1.setVisibility(View.GONE);
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(frRecord
                    .getLayoutParams());
            layoutParams.removeRule(RelativeLayout.BELOW);
            frRecord.setLayoutParams(layoutParams);
            FrameLayout.LayoutParams layoutParams3 = new FrameLayout.LayoutParams(rudderView.getLayoutParams());
            layoutParams3.gravity = Gravity.CENTER_VERTICAL;
            rudderView.setLayoutParams(layoutParams3);
            switchCloudRudder(false);
        }
    }
 
    private void initData() {
        bundle = getIntent().getExtras();
        if (bundle == null) {
            return;
        }
        deviceListBean = (DeviceDetailListData.ResponseData.DeviceListBean) bundle.getSerializable(MethodConst.ParamConst.deviceDetail);
        switchVideoList(true);
        getDeviceLocalCache();
        tvDeviceName.setText(deviceListBean.channels.get(deviceListBean.checkedChannel).channelName);
 
        if(deviceListBean.channels.get(deviceListBean.checkedChannel).resolutions!=null&&deviceListBean.channels.get(deviceListBean.checkedChannel).resolutions.size()>0){
            showHD = false;
            bateMode = deviceListBean.channels.get(deviceListBean.checkedChannel).resolutions.get(0).streamType;
            imageSize = deviceListBean.channels.get(deviceListBean.checkedChannel).resolutions.get(0).imageSize;
            tvPixel.setText(deviceListBean.channels.get(deviceListBean.checkedChannel).resolutions.get(0).name);
        }else{
            showHD = true;
        }
        lcPopupWindow = new LcPopupWindow(DeviceOnlineMediaPlayActivity.this,deviceListBean.channels.get(deviceListBean.checkedChannel).resolutions);
        lcPopupWindow.getContentView().measure(lcPopupWindow.makeDropDownMeasureSpec(lcPopupWindow.getWidth()),
                lcPopupWindow.makeDropDownMeasureSpec(lcPopupWindow.getHeight()
        ));
    }
 
    /**
     * 获取设备缓存信息
     */
    private void getDeviceLocalCache() {
        DeviceLocalCacheData deviceLocalCacheData = new DeviceLocalCacheData();
        deviceLocalCacheData.setDeviceId(deviceListBean.deviceId);
        if (deviceListBean.channels != null && deviceListBean.channels.size() > 0) {
            deviceLocalCacheData.setChannelId(deviceListBean.channels.get(deviceListBean.checkedChannel).channelId);
        }
        DeviceLocalCacheService deviceLocalCacheService = ClassInstanceManager.newInstance().getDeviceLocalCacheService();
        deviceLocalCacheService.findLocalCache(deviceLocalCacheData, this);
    }
 
 
    @Override
    public void deviceCache(DeviceLocalCacheData deviceLocalCacheData) {
        BitmapDrawable bitmapDrawable = MediaPlayHelper.picDrawable(deviceLocalCacheData.getPicPath());
        if (bitmapDrawable != null) {
            rlLoading.setBackground(bitmapDrawable);
        }
    }
 
    @Override
    public void onError(Throwable throwable) {
 
    }
 
    @Override
    protected void onResume() {
        super.onResume();
        loadingStatus(LoadStatus.LOADING, getResources().getString(R.string.lc_demo_device_video_play_loading), deviceListBean.deviceId);
    }
 
    @Override
    protected void onPause() {
        super.onPause();
        stop();
        recordStatus = RecordStatus.STOP;
    }
 
    private void initAbility(boolean loadSuccess) {
        String deviceAbility = deviceListBean.ability;
        String channelAbility = deviceListBean.channels.get(deviceListBean.checkedChannel).ability;
        //云台
        supportPTZ = DeviceAbilityHelper.isHasAbility(deviceAbility,channelAbility,"PT","PTZ") && loadSuccess;
        cloudStageClickListener(supportPTZ);
        //对讲
        speakClickListener(DeviceAbilityHelper.isHasAbility(deviceAbility,channelAbility,"AudioTalkV1","AudioTalk") && loadSuccess);
    }
 
    private void switchVideoList(boolean b) {
        this.cloudvideo = b;
        tvCloudVideo.setSelected(cloudvideo);
        tvLocalVideo.setSelected(!cloudvideo);
        if (cloudvideo) {
            initCloudRecord();
        } else {
            initLocalRecord();
        }
    }
 
    private void initCommonClickListener() {
        llBack.setOnClickListener(this);
        llDetail.setOnClickListener(this);
        tvCloudVideo.setOnClickListener(this);
        tvLocalVideo.setOnClickListener(this);
        tvNoVideo.setOnClickListener(this);
        llFullScreen.setOnClickListener(this);
    }
 
    private void featuresClickListener(boolean loadSuccess) {
        llPlayPause.setOnClickListener(loadSuccess ? this : null);
        llPlayStyle.setOnClickListener(loadSuccess ? this : null);
 
        llScreenShot.setOnClickListener(loadSuccess ? this : null);
        llScreenShot1.setOnClickListener(loadSuccess ? this : null);
        llVideo.setOnClickListener(loadSuccess ? this : null);
        llVideo1.setOnClickListener(loadSuccess ? this : null);
        llSound.setOnClickListener(loadSuccess ? this : null);
        ivPalyPause.setImageDrawable(loadSuccess ? getDrawable(R.mipmap.lc_demo_live_video_icon_h_pause) : getDrawable(R.mipmap.lc_demo_live_video_icon_h_pause_disable));
        if(showHD){
            ivPlayStyle.setVisibility(View.VISIBLE);
            tvPixel.setVisibility(View.GONE);
            ivPlayStyle.setImageDrawable(videoMode == VideoMode.MODE_HD ?
                    (loadSuccess ? getDrawable(R.mipmap.lc_demo_live_video_icon_h_hd) : getDrawable(R.mipmap.lc_demo_live_video_icon_h_hd_disable)) :
                    (loadSuccess ? getDrawable(R.mipmap.lc_demo_live_video_icon_h_sd) : getDrawable(R.mipmap.lc_demo_live_video_icon_h_hd_disable)));
        }else{
            ivPlayStyle.setVisibility(View.GONE);
            tvPixel.setVisibility(View.VISIBLE);
        }
 
        ivScreenShot.setImageDrawable(loadSuccess
                ? getDrawable(R.drawable.lc_demo_photo_capture_selector)
                : getDrawable(R.mipmap.lc_demo_livepreview_icon_screenshot_disable));
        ivVideo.setImageDrawable(loadSuccess
                ? getDrawable(R.mipmap.lc_demo_livepreview_icon_video)
                : getDrawable(R.mipmap.lc_demo_livepreview_icon_video_disable));
 
        ivScreenShot1.setImageDrawable(loadSuccess
                ? getDrawable(R.mipmap.live_video_icon_h_screenshot)
                : getDrawable(R.mipmap.live_video_icon_h_screenshot_disable));
        ivVideo1.setImageDrawable(loadSuccess
                ? getDrawable(R.mipmap.live_video_icon_h_video_off)
                : getDrawable(R.mipmap.live_video_icon_h_video_off_disable));
        ivSound.setImageDrawable(loadSuccess ? getDrawable(R.mipmap.lc_demo_live_video_icon_h_sound_off) : getDrawable(R.mipmap.lc_demo_live_video_icon_h_sound_off_disable));
//        //媒体声
//        if (soundStatus != SoundStatus.PLAY) {
//            return;
//        }
        if (loadSuccess&&openAudio()) {
            soundStatus = SoundStatus.PLAY;
            ivSound.setImageDrawable(getDrawable(R.mipmap.lc_demo_live_video_icon_h_sound_on));
        }
    }
 
    private void cloudStageClickListener(boolean isSupport) {
        llCloudStage.setOnClickListener(isSupport ? this : null);
        ivCloudStage.setImageDrawable(isSupport
                ? getDrawable(R.mipmap.lc_demo_livepreview_icon_cloudstage)
                : getDrawable(R.mipmap.lc_demo_livepreview_icon_cloudstage_disable));
        llCloudStage1.setOnClickListener(isSupport ? this : null);
        ivCloudStage1.setImageDrawable(isSupport
                ? getDrawable(R.mipmap.live_video_icon_h_cloudterrace_off)
                : getDrawable(R.mipmap.live_video_icon_h_cloudterrace_off_disable));
    }
 
    private void speakClickListener(boolean isSupport) {
        ivSpeak.setOnClickListener(isSupport ? this : null);
        ivSpeak.setImageDrawable(isSupport
                ? getDrawable(R.mipmap.lc_demo_livepreview_icon_speak)
                : getDrawable(R.mipmap.lc_demo_livepreview_icon_speak_disable));
        ivSpeak1.setOnClickListener(isSupport ? this : null);
        ivSpeak1.setImageDrawable(isSupport
                ? getDrawable(R.mipmap.live_video_icon_h_talk_off)
                : getDrawable(R.mipmap.live_video_icon_h_talk_off_disable));
    }
 
    private void setWindowListener(LCOpenSDK_PlayWindow playWin) {
        playWin.setWindowListener(new LCOpenSDK_EventListener() {
            //手势缩放开始事件
            @Override
            public void onZoomBegin(int index) {
                super.onZoomBegin(index);
                LogUtil.debugLog(TAG, "onZoomBegin: index= " + index);
            }
 
            //手势缩放中事件
            @Override
            public void onZooming(int index, float dScale) {
                super.onZooming(index, dScale);
                LogUtil.debugLog(TAG, "onZooming: index= " + index + " , dScale= " + dScale);
                mPlayWin.doScale(dScale);
            }
 
            //缩放结束事件
            @Override
            public void onZoomEnd(int index, ZoomType zoomType) {
                super.onZoomEnd(index, zoomType);
                LogUtil.debugLog(TAG, "onZoomEnd: index= " + index + " , zoomType= " + zoomType);
            }
 
            //窗口单击事件
            @Override
            public void onControlClick(int index, float dx, float dy) {
                super.onControlClick(index, dx, dy);
                LogUtil.debugLog(TAG, "onControlClick: index= " + index + " , dx= " + dx + " , dy= " + dy);
            }
 
            //窗口双击事件
            @Override
            public void onWindowDBClick(int index, float dx, float dy) {
                super.onWindowDBClick(index, dx, dy);
                LogUtil.debugLog(TAG, "onWindowDBClick: index= " + index + " , dx= " + dx + " , dy= " + dy);
            }
 
            //滑动开始事件
            @Override
            public boolean onSlipBegin(int index, Direction direction, float dx, float dy) {
                LogUtil.debugLog(TAG, "onSlipBegin: index= " + index + " , direction= " + direction + " , dx= " + dx + " , dy= " + dy);
                return super.onSlipBegin(index, direction, dx, dy);
            }
 
            //滑动中事件
            @Override
            public void onSlipping(int index, Direction direction, float prex, float prey, float dx, float dy) {
                super.onSlipping(index, direction, prex, prey, dx, dy);
                mPlayWin.doTranslate(dx,dy);
                LogUtil.debugLog(TAG, "onSlipping: index= " + index + " , direction= " + direction + " , prex= " + prex + " , prey= " + prey + " , dx= " + dx + " , dy= " + dy);
            }
 
            //滑动结束事件
            @Override
            public void onSlipEnd(int index, Direction direction, float dx, float dy) {
                super.onSlipEnd(index, direction, dx, dy);
                mPlayWin.doTranslateEnd();
                LogUtil.debugLog(TAG, "onSlipEnd: index= " + index + " , direction= " + direction + " , dx= " + dx + " , dy= " + dy);
            }
 
            //长按开始回调
            @Override
            public void onWindowLongPressBegin(int index, Direction direction, float dx, float dy) {
                super.onWindowLongPressBegin(index, direction, dx, dy);
                LogUtil.debugLog(TAG, "onWindowLongPressBegin: index= " + index + " , direction= " + direction + " , dx= " + dx + " , dy= " + dy);
            }
 
            //长按事件结束
            @Override
            public void onWindowLongPressEnd(int index) {
                super.onWindowLongPressEnd(index);
                LogUtil.debugLog(TAG, "onWindowLongPressEnd: index= " + index);
            }
 
            /**
             * 播放事件回调
             * resultSource:  0--RTSP  1--HLS  5--DHHTTP  99--OPENAPI
             */
            @Override
            public void onPlayerResult(int index, String code, int resultSource) {
                //mPlayWin.setSEnhanceMode(4);//设置降噪等级最大
                super.onPlayerResult(index, code, resultSource);
                LogUtil.debugLog(TAG, "onPlayerResult: index= " + index + " , code= " + code + " , resultSource= " + resultSource);
                boolean failed = false;
                if (resultSource == 99) {
                    //code  -1000 HTTP交互出错或超时
                    failed = true;
                } else {
                    if (resultSource == 5 && (!(code.equals("1000") || code.equals("0") || code.equals("4000")))) {
                        // code 1000-开启播放成功  0-开始拉流
                        failed = true;
                        if (code.equals("1000005")) {
                            inputEncryptKey();
                        }
                    }
 
                    else if (resultSource == 0 && (code.equals("0") || code.equals("1") || code.equals("3") || code.equals("7"))) {
                        // code
                        // 0-组帧失败,错误状态
                        // 1-内部要求关闭,如连接断开等,错误状态
                        // 3-RTSP鉴权失败,错误状态
                        // 7-秘钥错误
                        failed = true;
                        if (code.equals("7")) {
                            inputEncryptKey();
                        }
                    }
                }
                if (failed) {
                    loadingStatus(LoadStatus.LOAD_ERROR, getResources().getString(R.string.lc_demo_device_video_play_error) + ":" + code + "." + resultSource, "");
                    playStatus = PlayStatus.ERROR;
                }
            }
 
            //分辨率改变事件
            @Override
            public void onResolutionChanged(int index, int width, int height) {
                super.onResolutionChanged(index, width, height);
                LogUtil.debugLog(TAG, "onResolutionChanged: index= " + index + " , width= " + width + " , height= " + height);
            }
 
            //播放开始回调
            @Override
            public void onPlayBegan(int index) {
                super.onPlayBegan(index);
                LogUtil.debugLog(TAG, "onPlayBegan: index= " + index);
                loadingStatus(LoadStatus.LOAD_SUCCESS, "", "");
                playStatus = PlayStatus.PLAY;
            }
 
            //接收数据回调
            @Override
            public void onReceiveData(int index, int len) {
                super.onReceiveData(index, len);
                LogUtil.debugLog(TAG, "onReceiveData: index= " + index + " , len= " + len);
            }
 
            //接收帧流回调
            @Override
            public void onStreamCallback(int index, byte[] bytes, int len) {
                super.onStreamCallback(index, bytes, len);
                LogUtil.debugLog(TAG, "onStreamCallback: index= " + index + " , len= " + len);
            }
 
            //播放结束事件
            @Override
            public void onPlayFinished(int index) {
                super.onPlayFinished(index);
                LogUtil.debugLog(TAG, "onPlayFinished: index= " + index);
            }
 
            //播放时间信息回调
            @Override
            public void onPlayerTime(int index, long time) {
                super.onPlayerTime(index, time);
                LogUtil.debugLog(TAG, "onPlayerTime: index= " + index + " , time= " + time);
            }
 
 
            @Override
            public void onIVSInfo(int index, final String ivsInfo, long type, long len, long realLen) {
                super.onIVSInfo(index, ivsInfo, type, len, realLen);
                LogUtil.debugLog(TAG, "onIVSInfo: index= " + index + " , ivsInfo= " + ivsInfo);
 
                if (playStatus !=PlayStatus.PLAY) {
                    return;
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            if (ivsInfo != null && ivsInfo.contains("PtzLimitStatus")) {
                                final String source = ivsInfo.substring(ivsInfo.lastIndexOf("[") + 1, ivsInfo.lastIndexOf("]")).replace(" ", "");
                                String[] target = source.split(",");
                                if (target != null && target.length == 2) {
                                    final String hor = target[0];
                                    final String ver = target[1];
                                    if (hor.equals("1") || hor.equals("-1") || ver.equals("1") || ver.equals("-1")) {
                                        if(hor.equals("1")){
                                            ivLimitLeft.setVisibility(View.VISIBLE);
                                            ivLimitRight.setVisibility(View.GONE);
                                            ivLimitDown.setVisibility(View.GONE);
                                            ivLimitUp.setVisibility(View.GONE);
                                        }else if(hor.equals("-1")){
                                            ivLimitLeft.setVisibility(View.GONE);
                                            ivLimitRight.setVisibility(View.VISIBLE);
                                            ivLimitDown.setVisibility(View.GONE);
                                            ivLimitUp.setVisibility(View.GONE);
                                        }else if(ver.equals("1")){
                                            ivLimitLeft.setVisibility(View.GONE);
                                            ivLimitRight.setVisibility(View.GONE);
                                            ivLimitDown.setVisibility(View.VISIBLE);
                                            ivLimitUp.setVisibility(View.GONE);
                                        }else if(ver.equals("-1")){
                                            ivLimitLeft.setVisibility(View.GONE);
                                            ivLimitRight.setVisibility(View.GONE);
                                            ivLimitDown.setVisibility(View.GONE);
                                            ivLimitUp.setVisibility(View.VISIBLE);
                                        }
                                    }
                                }
                            }else{
                                ivLimitLeft.setVisibility(View.GONE);
                                ivLimitRight.setVisibility(View.GONE);
                                ivLimitDown.setVisibility(View.GONE);
                                ivLimitUp.setVisibility(View.GONE);
                            }
                        } catch (NullPointerException e) {
                            e.printStackTrace();
                        } catch (ArrayIndexOutOfBoundsException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });
    }
 
    /**
     * 输入秘钥
     */
    private void inputEncryptKey() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (encryptKeyInputDialog == null) {
                    encryptKeyInputDialog = new EncryptKeyInputDialog(DeviceOnlineMediaPlayActivity.this);
                }
                encryptKeyInputDialog.show();
                encryptKeyInputDialog.setOnClick(new EncryptKeyInputDialog.OnClick() {
                    @Override
                    public void onSure(String txt) {
                        encryptKey = txt;
                        loadingStatus(LoadStatus.LOADING, getResources().getString(R.string.lc_demo_device_video_play_change), txt);
                    }
                });
            }
        });
    }
 
    /**
     * 播放状态
     *
     * @param loadStatus 播放状态
     * @param msg
     */
    private void loadingStatus(final LoadStatus loadStatus, final String msg, final String psk) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (loadStatus == LoadStatus.LOADING) {
                    //先关闭
                    stop();
                    //开始播放
                    play(psk);
                    rlLoading.setVisibility(View.VISIBLE);
                    pbLoading.setVisibility(View.VISIBLE);
                    tvLoadingMsg.setText(msg);
                } else if (loadStatus == LoadStatus.LOAD_SUCCESS) {
                    //播放成功
                    rlLoading.setVisibility(View.GONE);
                    rudderView.enable(true);
                    if(LCUtils.isDebug(DeviceOnlineMediaPlayActivity.this)){
                        showStreamTypeCover();
                    }
                    initAbility(true);
                    featuresClickListener(true);
 
 
                } else {
                    //播放失败
                    stop();
                    rlLoading.setVisibility(View.VISIBLE);
                    pbLoading.setVisibility(View.GONE);
                    tvLoadingMsg.setText(msg);
                    initAbility(false);
                    featuresClickListener(false);
                }
            }
        });
    }
 
 
    public void showStreamTypeCover(){
        tvCoverStream.setVisibility(View.VISIBLE);
        if(mPlayWin.isP2pTag()){
            tvCoverStream.setText("P2P");
        }else{
            tvCoverStream.setText("MTS");
        }
    }
 
    /**
     * 开始播放
     */
    public void play(String psk) {
 
        if(showHD){
            bateMode = videoMode == VideoMode.MODE_HD ? 0 : 1;
        }
        LCOpenSDK_ParamReal paramReal = new LCOpenSDK_ParamReal(
                LCDeviceEngine.newInstance().subAccessToken,
                deviceListBean.deviceId,
                Integer.parseInt(deviceListBean.channels.get(deviceListBean.checkedChannel).channelId),
                psk,
                deviceListBean.playToken,
                bateMode,
                true,true,imageSize
        );
        mPlayWin.playRtspReal(paramReal);
 
    }
 
    /**
     * 停止播放
     */
    public void stop() {
        captureLastPic();
        stopRecord();// 关闭录像
        closeAudio();// 关闭音频
        stopTalk1();//关闭对讲
        rudderView.enable(false);
        mPlayWin.stopRtspReal(true);// 关闭视频
    }
 
    /**
     * 保存最后一帧做封面
     */
    private void captureLastPic() {
        if (playStatus == PlayStatus.ERROR) {
            return;
        }
        String capturePath;
        try {
            capturePath = capture(false);
        } catch (Throwable e) {
            capturePath = null;
        }
        if (capturePath == null) {
            return;
        }
        DeviceLocalCacheService deviceLocalCacheService = ClassInstanceManager.newInstance().getDeviceLocalCacheService();
        DeviceLocalCacheData deviceLocalCacheData = new DeviceLocalCacheData();
        deviceLocalCacheData.setPicPath(capturePath);
        deviceLocalCacheData.setDeviceName(deviceListBean.name);
        deviceLocalCacheData.setDeviceId(deviceListBean.deviceId);
        if (deviceListBean.channels != null && deviceListBean.channels.size() > 0) {
            deviceLocalCacheData.setChannelId(deviceListBean.channels.get(deviceListBean.checkedChannel).channelId);
            deviceLocalCacheData.setChannelName(deviceListBean.channels.get(deviceListBean.checkedChannel).channelName);
        }
        deviceLocalCacheService.addLocalCache(deviceLocalCacheData);
    }
 
    /**
     * 开始录像
     */
    public boolean startRecord() {
        // 录像的路径
 
        String channelName = null;
        if (deviceListBean.channels != null && deviceListBean.channels.size() > 0) {
            channelName = deviceListBean.channels.get(deviceListBean.checkedChannel).channelName;
        } else {
            channelName = deviceListBean.name;
        }
        // 去除通道中在目录中的非法字符
        channelName = channelName.replace("-", "");
        videoPath = MediaPlayHelper.getCaptureAndVideoPath(MediaPlayHelper.DHFilesType.DHVideo, channelName);
        LogUtil.debugLog(TAG,"videopath:::----start"+videoPath);
        //MediaScannerConnection.scanFile(this, new String[]{videoPath}, null, null);
        // 开始录制 1
        int ret = mPlayWin.startRecord(videoPath, 1, 0x7FFFFFFF);
        return ret == 0;
    }
 
    /**
     * 关闭录像
     */
    public boolean stopRecord() {
        return mPlayWin.stopRecord() == 0;
    }
 
    /**
     * 截图
     */
    public String capture(boolean notify) {
        String captureFilePath = null;
        String channelName = null;
        if (deviceListBean.channels != null && deviceListBean.channels.size() > 0) {
            channelName = deviceListBean.channels.get(deviceListBean.checkedChannel).channelName;
        } else {
            channelName = deviceListBean.name;
        }
        // 去除通道中在目录中的非法字符
        channelName = channelName.replace("-", "");
        captureFilePath = MediaPlayHelper.getCaptureAndVideoPath(notify ? MediaPlayHelper.DHFilesType.DHImage : MediaPlayHelper.DHFilesType.DHImageCache, channelName);
        int ret = mPlayWin.snapShot(captureFilePath);
        if (ret == 0) {
            if (notify) {
                // 扫描到相册中
                MediaPlayHelper.updatePhotoAlbum(captureFilePath);
               // MediaScannerConnection.scanFile(this, new String[]{captureFilePath}, null, null);
            }
        } else {
            captureFilePath = null;
        }
        return captureFilePath;
    }
 
    /**
     * 打开声音
     */
    public boolean openAudio() {
        return mPlayWin.playAudio() == 0;
    }
 
    /**
     * 关闭声音
     */
    public boolean closeAudio() {
        return mPlayWin.stopAudio() == 0;
    }
 
    /**
     * 开始对讲 多通道通道号参数传入对应的通道号,单通道传-1
     */
    public void startTalk() {
        closeAudio();
        soundStatus = SoundStatus.STOP;
        speakStatus = SpeakStatus.OPENING;
        ivSound.setImageDrawable(getDrawable(R.mipmap.lc_demo_live_video_icon_h_sound_off_disable));
        llSound.setClickable(false);//声音按钮不允许点击
        LCOpenSDK_Talk.setListener(audioTalkerListener);//对讲前先设备监听
        int channelId = -1;
 
        if(  Integer.parseInt(deviceListBean.channelNum)> 1) {
           /* if(null != deviceListBean.channels.get(deviceListBean.checkedChannel).channelId && !"".equals(deviceListBean.channels.get(deviceListBean.checkedChannel).channelId)) {
                channelId = Integer.parseInt(deviceListBean.channels.get(deviceListBean.checkedChannel).channelId);
            } else {
                stopTalk();
                Logger.e(TAG, "server returned NVR device channelId is null or empty charators");
                return;
            }*/
           channelId = Integer.parseInt(deviceListBean.channels.get(deviceListBean.checkedChannel).channelId);
           Logger.d(TAG, "deviceCatalog = " + deviceListBean.catalog + ", routing device talk...");
       }
        Logger.d(TAG, "playTalk, channelId = " + channelId);
        LCOpenSDK_ParamTalk paramTalk = new LCOpenSDK_ParamTalk(
                LCDeviceEngine.newInstance().subAccessToken,
                deviceListBean.deviceId,
                channelId,
                TextUtils.isEmpty(encryptKey) ? deviceListBean.deviceId : encryptKey,
                deviceListBean.playToken,
                true,"talk"
        );
        LCOpenSDK_Talk.playTalk(paramTalk);
    }
 
    /**
     * 停止对讲
     */
    public void stopTalk1() {
        ivSound.setImageDrawable(getDrawable(R.mipmap.lc_demo_live_video_icon_h_sound_off_disable));
        speakStatus = SpeakStatus.STOP;
        ivSpeak.setImageDrawable(getDrawable(R.mipmap.lc_demo_livepreview_icon_speak));
        ivSpeak1.setImageDrawable(getDrawable(R.mipmap.live_video_icon_h_talk_off));
 
        LCOpenSDK_Talk.stopTalk();
        LCOpenSDK_Talk.setListener(null);//停止对讲后对讲监听置为空
        llSound.setClickable(true);
    }
 
    /**
     * 停止对讲
     */
    public void stopTalk() {
        soundStatus = soundStatusPre;
        if (SoundStatus.PLAY == soundStatus) {
            ivSound.setImageDrawable(getDrawable(R.mipmap.lc_demo_live_video_icon_h_sound_on));
            openAudio();
        }else{
            ivSound.setImageDrawable(getDrawable(R.mipmap.lc_demo_live_video_icon_h_sound_off));
        }
        speakStatus = SpeakStatus.STOP;
        ivSpeak.setImageDrawable(getDrawable(R.mipmap.lc_demo_livepreview_icon_speak));
        ivSpeak1.setImageDrawable(getDrawable(R.mipmap.live_video_icon_h_talk_off));
 
        LCOpenSDK_Talk.stopTalk();
        LCOpenSDK_Talk.setListener(null);//停止对讲后对讲监听置为空
        llSound.setClickable(true);
    }
 
    class AudioTalkerListener extends LCOpenSDK_TalkerListener {
        public AudioTalkerListener() {
            super();
        }
 
        @Override
        public void onTalkResult(String error, int type) {
            super.onTalkResult(error, type);
            boolean 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;
            }
            final boolean finalTalkResult = talkResult;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (!finalTalkResult) {
                        stopTalk();
                        // 提示对讲打开失败
                        Toast.makeText(DeviceOnlineMediaPlayActivity.this, R.string.lc_demo_device_talk_open_failed, Toast.LENGTH_SHORT).show();
                        speakStatus = SpeakStatus.STOP;
                        ivSpeak.setImageDrawable(getDrawable(R.mipmap.lc_demo_livepreview_icon_speak));
                        ivSpeak1.setImageDrawable(getDrawable(R.mipmap.live_video_icon_h_talk_off));
                    } else {
                        // 提示对讲打开成功
                        Toast.makeText(DeviceOnlineMediaPlayActivity.this, R.string.lc_demo_device_talk_open_success, Toast.LENGTH_SHORT).show();
                        speakStatus = SpeakStatus.PLAY;
                        ivSpeak.setImageDrawable(getDrawable(R.mipmap.lc_demo_livepreview_icon_speak_ing));
                        ivSpeak1.setImageDrawable(getDrawable(R.mipmap.live_video_icon_h_talk_on));
 
                    }
                }
            });
        }
 
        @Override
        public void onTalkPlayReady() {
            super.onTalkPlayReady();
        }
 
        @Override
        public void onAudioRecord(byte[] bytes, int i, int i1, int i2, int i3) {
            super.onAudioRecord(bytes, i, i1, i2, i3);
        }
 
        @Override
        public void onAudioReceive(byte[] bytes, int i, int i1, int i2, int i3) {
            super.onAudioReceive(bytes, i, i1, i2, i3);
        }
 
        @Override
        public void onDataLength(int i) {
            super.onDataLength(i);
        }
    }
 
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ll_back) {
            //返回
            finish();
        } else if (id == R.id.ll_fullscreen) {
            //横竖屏切换
            if (mCurrentOrientation == Configuration.ORIENTATION_PORTRAIT) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                mCurrentOrientation = Configuration.ORIENTATION_LANDSCAPE;
            } else {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                mCurrentOrientation = Configuration.ORIENTATION_PORTRAIT;
            }
            ivChangeScreen.setImageDrawable(mCurrentOrientation == Configuration.ORIENTATION_LANDSCAPE ? getResources().getDrawable(R.mipmap.live_btn_smallscreen) : getResources().getDrawable(R.mipmap.video_fullscreen));
        } else if (id == R.id.ll_detail) {
            Bundle bundle = new Bundle();
            bundle.putSerializable(MethodConst.ParamConst.deviceDetail, deviceListBean);
            Intent intent = new Intent(DeviceOnlineMediaPlayActivity.this, DeviceDetailActivity.class);
            intent.putExtras(bundle);
            startActivityForResult(intent, 0);
        } else if (id == R.id.ll_paly_pause) {
            //播放暂停
            if (playStatus == PlayStatus.PLAY) {
                stop();
                initAbility(false);
                featuresClickListener(false);
                llPlayPause.setOnClickListener(this);
            } else {
                getDeviceLocalCache();
                loadingStatus(LoadStatus.LOADING, getResources().getString(R.string.lc_demo_device_video_play_loading), TextUtils.isEmpty(encryptKey) ? deviceListBean.deviceId : encryptKey);
            }
            playStatus = (playStatus == PlayStatus.PLAY) ? PlayStatus.PAUSE : PlayStatus.PLAY;
            ivPalyPause.setImageDrawable(playStatus == PlayStatus.PLAY ? getDrawable(R.mipmap.lc_demo_live_video_icon_h_pause) : getDrawable(R.mipmap.lc_demo_live_video_icon_h_play));
        } else if (id == R.id.ll_play_style) {
            if(showHD){
                //视频清晰度切换
                videoMode = (videoMode == VideoMode.MODE_HD) ? VideoMode.MODE_SD : VideoMode.MODE_HD;
                ivPlayStyle.setImageDrawable(videoMode == VideoMode.MODE_HD ? getDrawable(R.mipmap.lc_demo_live_video_icon_h_hd) : getDrawable(R.mipmap.lc_demo_live_video_icon_h_sd));
                loadingStatus(LoadStatus.LOADING, getResources().getString(R.string.lc_demo_device_video_play_change), TextUtils.isEmpty(encryptKey) ? deviceListBean.deviceId : encryptKey);
            }else {
                int offsetX = llPlayStyle.getWidth()/3*2;
                int offsetY = -(lcPopupWindow.getContentView().getMeasuredHeight()+10);
                PopupWindowCompat.showAsDropDown(lcPopupWindow,llPlayStyle,offsetX,offsetY,Gravity.START);
                lcPopupWindow.setPixelRecycleListener(new LcPopupWindow.onRecyclerViewItemClickListener() {
                    @Override
                    public void onItemClick(RecyclerView parent, View view, int position, String name, int image_size,int streamType) {
                        tvPixel.setText(name);
                        lcPopupWindow.dismiss();
                        imageSize = image_size;
                        bateMode = streamType;
                        loadingStatus(LoadStatus.LOADING, getResources().getString(R.string.lc_demo_device_video_play_change), TextUtils.isEmpty(encryptKey) ? deviceListBean.deviceId : encryptKey);
                    }
                });
            }
 
 
 
        } else if (id == R.id.ll_sound) {
            //媒体声 如果是开启去关闭,反之
            if (soundStatus == SoundStatus.NO_SUPPORT) {
                return;
            }
            boolean result = false;
            if (soundStatus == SoundStatus.PLAY) {
                result = closeAudio();
            } else {
                result = openAudio();
            }
            if (!result) {
                return;
            }
            soundStatus = (soundStatus == SoundStatus.PLAY) ? SoundStatus.STOP : SoundStatus.PLAY;
            ivSound.setImageDrawable(soundStatus == SoundStatus.PLAY ? getDrawable(R.mipmap.lc_demo_live_video_icon_h_sound_on) : getDrawable(R.mipmap.lc_demo_live_video_icon_h_sound_off));
        } else if (id == R.id.ll_cloudstage || id == R.id.ll_cloudstage1) {
            //云台和录像列表切换
            switchCloudRudder(!isShwoRudder);
        } else if (id == R.id.ll_screenshot || id == R.id.ll_screenshot1) {
            //截图
            if (capture(true) != null) {
                Toast.makeText(this, getResources().getString(R.string.lc_demo_device_capture_success), Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, getResources().getString(R.string.lc_demo_device_capture_failed), Toast.LENGTH_SHORT).show();
            }
        } else if (id == R.id.iv_speak || id == R.id.iv_speak1) {
            //对讲 如果是打开状态去关闭,反之
            if (speakStatus == SpeakStatus.NO_SUPPORT || speakStatus==SpeakStatus.OPENING) {
                return;
            }
            if (speakStatus == SpeakStatus.STOP) {
                soundStatusPre = soundStatus;
                startTalk();
            } else {
                Toast.makeText(DeviceOnlineMediaPlayActivity.this,R.string.lc_demo_device_talk_close_success,Toast.LENGTH_SHORT).show();
                stopTalk();
 
            }
        } else if (id == R.id.ll_video || id == R.id.ll_video1) {
            //录像 如果是关闭状态去打开,反之
            if (recordStatus == RecordStatus.STOP) {
                if (startRecord()) {
                    Toast.makeText(this, getResources().getString(R.string.lc_demo_device_record_begin), Toast.LENGTH_SHORT).show();
                } else {
                    return;
                }
            } else {
                if (stopRecord()) {
                    Toast.makeText(this, getResources().getString(R.string.lc_demo_device_record_stop), Toast.LENGTH_SHORT).show();
                    LogUtil.debugLog(TAG,"videopath:::"+videoPath);
                    MediaPlayHelper.updatePhotoVideo(videoPath);
                } else {
                    return;
                }
            }
            recordStatus = (recordStatus == RecordStatus.START) ? RecordStatus.STOP : RecordStatus.START;
            ivVideo.setImageDrawable(recordStatus == RecordStatus.START
                    ? getDrawable(R.mipmap.lc_demo_livepreview_icon_video_ing)
                    : getDrawable(R.mipmap.lc_demo_livepreview_icon_video));
            ivVideo1.setImageDrawable(recordStatus == RecordStatus.START
                    ? getDrawable(R.mipmap.live_video_icon_h_video_on)
                    : getDrawable(R.mipmap.live_video_icon_h_video_off));
        } else if (id == R.id.tv_cloud_video) {
            //切换至云录像
            switchVideoList(true);
        } else if (id == R.id.tv_local_video) {
            //切换至设备录像
            switchVideoList(false);
        } else if (id == R.id.tv_no_video) {
            //暂无录像切换
            gotoRecordList();
        }
    }
 
    private void switchCloudRudder(boolean isShow) {
        this.isShwoRudder = isShow;
        if (isShow) {
            ivCloudStage.setImageDrawable(getDrawable(R.mipmap.lc_demo_livepreview_icon_cloudstage_click));
            ivCloudStage1.setImageDrawable(getDrawable(R.mipmap.live_video_icon_h_cloudterrace_on));
            llVideoContent.setVisibility(View.GONE);
            rudderView.setVisibility(View.VISIBLE);
        } else {
            ivCloudStage.setImageDrawable(supportPTZ
                    ? getDrawable(R.mipmap.lc_demo_livepreview_icon_cloudstage)
                    : getDrawable(R.mipmap.lc_demo_livepreview_icon_cloudstage_disable));
            ivCloudStage1.setImageDrawable(supportPTZ
                    ? getDrawable(R.mipmap.live_video_icon_h_cloudterrace_off)
                    : getDrawable(R.mipmap.live_video_icon_h_cloudterrace_off_disable));
            llVideoContent.setVisibility(mCurrentOrientation == Configuration.ORIENTATION_PORTRAIT ? View.VISIBLE : View.GONE);
            rudderView.setVisibility(View.GONE);
        }
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPlayWin.uninitPlayWindow();// 销毁底层资源
        uninitOrientationEventListener();
    }
 
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && data != null) {
            boolean unBind = data.getBooleanExtra(DeviceConstant.IntentKey.DHDEVICE_UNBIND, false);
            if (unBind) {
                finish();
            }
        }
        if (resultCode == 100 && data != null) {
            String name = data.getStringExtra(DeviceConstant.IntentKey.DHDEVICE_NEW_NAME);
            tvDeviceName.setText(name);
            deviceListBean.channels.get(deviceListBean.checkedChannel).channelName = name;
        }
    }
 
 
    public static final int NO_ORIENTATION_REQUEST = -1;
 
    public static final int MSG_REQUEST_ORIENTATION = 1;
 
 
    private void initOrientationEventListener() {
 
        mOrientationEventListener = new MediaPlayOrientationEventListener(this,
                SensorManager.SENSOR_DELAY_NORMAL, mHandler);
 
        if (mOrientationEventListener.canDetectOrientation()) {
            mOrientationEventListener.enable();
        } else {
            mOrientationEventListener.disable();
        }
    }
 
 
    private void uninitOrientationEventListener() {
        if (mOrientationEventListener != null) {
            mOrientationEventListener.disable();
            mOrientationEventListener = null;
        }
    }
 
 
 
    static class MediaPlayOrientationEventListener extends OrientationEventListener {
 
        private WeakReference<Handler> mWRHandler;
 
        private int mOrientationEventListenerLastOrientationRequest = NO_ORIENTATION_REQUEST;
 
        public MediaPlayOrientationEventListener(Context context, int rate, Handler handler) {
            super(context, rate);
            mWRHandler = new WeakReference<Handler>(handler);
        }
 
        @Override
        public void onOrientationChanged(int orientation) {
 
            int requestedOrientation = createOrientationRequest(orientation);
            if (requestedOrientation != NO_ORIENTATION_REQUEST
                    && mOrientationEventListenerLastOrientationRequest != requestedOrientation) {
 
                Handler handler = mWRHandler.get();
 
                if (handler != null) {
                    handler.removeMessages(MSG_REQUEST_ORIENTATION);
                    Message msg = handler.obtainMessage(MSG_REQUEST_ORIENTATION);
                    msg.arg1 = requestedOrientation;
                    handler.sendMessageDelayed(msg, 200);
                    mOrientationEventListenerLastOrientationRequest = requestedOrientation;
                }
            }
        }
 
        private int createOrientationRequest(int rotation) {
            int requestedOrientation = NO_ORIENTATION_REQUEST;
 
            if (rotation == -1) {
 
            } else if (rotation < 10 || rotation > 350) {// 手机顶部向上
 
                requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
 
            } else if (rotation < 100 && rotation > 80) {// 手机左边向上
 
                requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
 
            } else if (rotation < 190 && rotation > 170) {// 手机底边向上
 
                requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
 
            } else if (rotation < 280 && rotation > 260) {// 手机右边向上
 
                requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
            }
 
            return requestedOrientation;
        }
 
    }
}