1
wxr
2023-03-31 7e42cc13a14b7de31c9f5d5c61cdf24f3246335d
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
using System;
using System.Collections.Generic;
using System.Text;
using Shared;
using Shared.SimpleControl.Phone;
using Shared.SimpleControl;
using Shared.SimpleControl.R;
using System.Xml;
using SmartHome.UI.SimpleControl.Pad.Music;
using System.Security;
 
 
namespace Shared.SimpleControl.Pad.Music
{
    /// <summary>
    /// 音乐源界面
    /// </summary>
    class A31MusicSourcePage : FrameLayout
    {
        A31MusicModel a31MusicModel;
        public void Show (A31MusicModel a31, FrameLayout MusicSourcePage, FrameLayout PalyMusicPage, FrameLayout bordorView)
        {
            a31MusicModel = a31;
 
            var topFrameLayout = new FrameLayout {
                Height = Application.GetRealHeight (140),
                BackgroundColor = 0xFF222322,
            };
            AddChidren (topFrameLayout);
 
            var btnTitle = new Button {
                TextID = MyInternationalizationString.MusicSource,
                TextSize = 20,
            };
            topFrameLayout.AddChidren (btnTitle);
 
            var tempFrameLayout = new FrameLayout {
                Y = topFrameLayout.Bottom,
                Height = Application.GetRealHeight (1536 - 140 - 150),
                BackgroundColor = 0xff191919,
            };
            AddChidren (tempFrameLayout);
 
            VerticalScrolViewLayout middle = new VerticalScrolViewLayout ();
            tempFrameLayout.AddChidren (middle);
 
            #region 本地音乐
            var LocalMusic = new RowLayout {
                Height = Application.GetRealHeight (100),
            };
            if (Application.DeviceType == Device.Android) {
                middle.AddChidren (LocalMusic);
            }
            var musichoto = new Button {
                Width = Application.GetRealWidth (61),
                Height = Application.GetRealHeight (81),
                UnSelectedImagePath = "MusicIcon/musicMusic.png",
                SelectedImagePath = "MusicIcon/HomepageMusicSelected.png",
                TextAlignment = TextAlignment.CenterLeft,
                X = Application.GetRealWidth (30),
                Gravity = Gravity.CenterVertical,
            };
            LocalMusic.AddChidren (musichoto);
 
            var musicSD = new Button {
                Height = Application.GetRealHeight (100),
                TextID = MyInternationalizationString.Musicmusic,
                TextAlignment = TextAlignment.CenterLeft,
                X = Application.GetRealWidth (130),
            };
            LocalMusic.AddChidren (musicSD);
 
            var nextSD = new Button {
                Width = Application.GetRealWidth (87),
                Height = Application.GetRealHeight (100),
                UnSelectedImagePath = "MusicIcon/Next.png",
                SelectedImagePath = "MusicIcon/NextSelecte.png",
                X = Application.GetRealWidth (480),
            };
            LocalMusic.AddChidren (nextSD);
 
            EventHandler<MouseEventArgs> LocalMusicource = (sender, e) => {
                MainPage.Loading.Start (Language.StringByID (MyInternationalizationString.load) + "...");
                System.Threading.Tasks.Task.Run (() => {
                    Application.RunOnMainThread (() => {
                        MainPage.Loading.Hide ();
                        A31LocalMusic a31Music = new A31LocalMusic ();
                        MusicSourcePage.AddChidren (a31Music);
                        a31Music.Show (a31, MusicSourcePage, PalyMusicPage);
                    });
                });
            };
            musicSD.MouseUpEventHandler += LocalMusicource;
            nextSD.MouseUpEventHandler += LocalMusicource;
            musichoto.MouseUpEventHandler += LocalMusicource;
            #endregion
 
            #region   USB
            var USBrowlayout = new RowLayout {
                Height = Application.GetRealHeight (100),
            };
            middle.AddChidren (USBrowlayout);
 
            var USBhoto = new Button {
                Width = Application.GetRealWidth (61),
                Height = Application.GetRealHeight (81),
                UnSelectedImagePath = "MusicIcon/USB.png",
                SelectedImagePath = "MusicIcon/USBselected.png",
                TextAlignment = TextAlignment.CenterLeft,
                X = Application.GetRealWidth (30),
                Gravity = Gravity.CenterVertical,
            };
            USBrowlayout.AddChidren (USBhoto);
 
            var usb = new Button {
                Height = Application.GetRealHeight (100),
                TextID = MyInternationalizationString.MuiscUSB,
                TextAlignment = TextAlignment.CenterLeft,
                X = Application.GetRealWidth (130),
            };
            USBrowlayout.AddChidren (usb);
 
            var usbSD = new Button {
                Width = Application.GetRealWidth (87),
                Height = Application.GetRealHeight (100),
                UnSelectedImagePath = "MusicIcon/Next.png",
                SelectedImagePath = "MusicIcon/NextSelecte.png",
                X = Application.GetRealWidth (480),
            };
            USBrowlayout.AddChidren (usbSD);
 
            EventHandler<MouseEventArgs> USBsource = (sender, e) => {
                MainPage.Loading.Start (Language.StringByID (MyInternationalizationString.load) + "...");
                System.Threading.Tasks.Task.Run (() => {
                    var musicInfoList = new List<MusicInfo> ();
                    try {
 
                        //var dd = usbserarchSong (a31.IPAddress);
 
                        var usbString = getUSBPlayList ();
                        if (usbString == null) {
                            return;
                        }
                        var se = System.Security.SecurityElement.FromString (usbString);
                        while (se.Children != null) {
                            se = se.Children [0] as System.Security.SecurityElement;
                        }
 
                        foreach (SecurityElement track in SecurityElement.FromString (se.Text).SearchForChildByTag ("Tracks").Children) {
                            MusicInfo musicInfo = new MusicInfo ();
                            musicInfo.URL = track.SearchForTextOfTag ("URL");
                            var metadata = track.SearchForTextOfTag ("Metadata");
 
                            var item = SecurityElement.FromString (metadata).SearchForChildByTag ("item");
                            musicInfo.Title = item.SearchForTextOfTag ("dc:title");
                            musicInfo.Artist = item.SearchForTextOfTag ("upnp:artist");
                            musicInfo.Album = item.SearchForTextOfTag ("upnp:album");
                            musicInfo.Duration = item.SearchForTextOfTag ("res");
                            musicInfo.AlbumId = item.SearchForTextOfTag ("song:albumid");
                            musicInfoList.Add (musicInfo);
                        }
 
                        /*
                        if (dd == "no music file") {
                            return;
                        }
 
                        var songList = Newtonsoft.Json.JsonConvert.DeserializeObject<A31infolist> (dd);
                        foreach (var v in songList.infolist) {
                            byte [] bytes = new byte [v.Title.Length / 2];
                            for (int i = 0, j = 0; i < bytes.Length; j += 2, i++) {
                                //把16进度转换成byte
                                bytes [i] = Convert.ToByte (v.Title.Substring (j, 2), 16);
                            }
                            //把byte数组转换成文字
                            v.Title = System.Text.Encoding.UTF8.GetString (bytes);
 
                            byte [] bytes1 = new byte [v.Album.Length / 2];
                            for (int i = 0, j = 0; i < bytes1.Length; j += 2, i++) {
                                //把16进度转换成byte
                                bytes1 [i] = Convert.ToByte (v.Album.Substring (j, 2), 16);
                            }
                            //把byte数组转换成文字
                            v.Album = System.Text.Encoding.UTF8.GetString (bytes1);
 
                            byte [] bytes2 = new byte [v.Artist.Length / 2];
                            for (int i = 0, j = 0; i < bytes2.Length; j += 2, i++) {
                                //把16进度转换成byte
                                bytes2 [i] = Convert.ToByte (v.Artist.Substring (j, 2), 16);
                            }
                            //把byte数组转换成文字
                            v.Artist = System.Text.Encoding.UTF8.GetString (bytes2);
 
                            byte [] bytes3 = new byte [v.filename.Length / 2];
                            for (int i = 0, j = 0; i < bytes3.Length; j += 2, i++) {
                                //把16进度转换成byte
                                bytes3 [i] = Convert.ToByte (v.filename.Substring (j, 2), 16);
                            }
                            //把byte数组转换成文字
                            v.filename = System.Text.Encoding.UTF8.GetString (bytes3);
                            musicInfoList.Add (new MusicInfo {
                                SourceType = "USB",
                                Title = v.Title,
                                Album = v.Album,
                                Artist = v.Artist,
                                URL = v.filename
                            });
                        }
*/
                    } catch { } finally {
                        Application.RunOnMainThread (() => {
                            MainPage.Loading.Hide ();
                            A31Usbmusic usbmusic = new A31Usbmusic ();
                            MusicSourcePage.AddChidren (usbmusic);
                            usbmusic.show (a31, musicInfoList, MusicSourcePage, PalyMusicPage);
                        });
                    }
                });
            };
 
            usb.MouseUpEventHandler += USBsource;
            usbSD.MouseUpEventHandler += USBsource;
            USBhoto.MouseUpEventHandler += USBsource;
 
            #endregion
 
            #region            我的列表
 
            var rowaddlist = new RowLayout {
                Height = Application.GetRealHeight (100),
            };
            middle.AddChidren (rowaddlist);
 
            var listhoto = new Button {
                Width = Application.GetRealWidth (61),
                Height = Application.GetRealHeight (81),
                UnSelectedImagePath = "MusicIcon/mylist.png",
                TextAlignment = TextAlignment.CenterLeft,
                X = Application.GetRealWidth (30),
                Gravity = Gravity.CenterVertical,
            };
            rowaddlist.AddChidren (listhoto);
 
            var addlistname = new Button {
                Height = Application.GetRealHeight (100),
                TextID = MyInternationalizationString.Musiclist,
                TextAlignment = TextAlignment.CenterLeft,
                X = Application.GetRealWidth (130),
                Gravity = Gravity.CenterVertical,
            };
            rowaddlist.AddChidren (addlistname);
 
            var listback = new Button {
                Width = Application.GetRealWidth (87),
                Height = Application.GetRealHeight (100),
                UnSelectedImagePath = "MusicIcon/Next.png",
                SelectedImagePath = "MusicIcon/NextSelecte.png",
                X = Application.GetRealWidth (480),
                Gravity = Gravity.CenterVertical,
            };
            rowaddlist.AddChidren (listback);
 
            EventHandler<MouseEventArgs> addlist = (sender, e) => {
 
                MainPage.Loading.Start (Language.StringByID (MyInternationalizationString.load) + "...");
                System.Threading.Tasks.Task.Run (() => {
                    Application.RunOnMainThread (() => {
                        MainPage.Loading.Hide ();
                        A31MyList a31MyList = new A31MyList ();
                        MusicSourcePage.AddChidren (a31MyList);
                        a31MyList.Show (a31, MusicSourcePage, PalyMusicPage);
                    });
                });
 
            };
 
            addlistname.MouseUpEventHandler += addlist;
            listback.MouseUpEventHandler += addlist;
            #endregion
 
            #region      我的最爱
 
            var likelayout = new RowLayout {
                Height = Application.GetRealHeight (100),
            };
            middle.AddChidren (likelayout);
 
            var likehoto = new Button {
                Width = Application.GetRealWidth (61),
                Height = Application.GetRealHeight (81),
                UnSelectedImagePath = "MusicIcon/like.png",
                SelectedImagePath = "MusicIcon/likeSelected.png",
                TextAlignment = TextAlignment.CenterLeft,
                X = Application.GetRealWidth (30),
                Gravity = Gravity.CenterVertical,
            };
            likelayout.AddChidren (likehoto);
 
            var loveList = new Button {
                Height = Application.GetRealHeight (100),
                TextID = MyInternationalizationString.Musiclike,
                TextAlignment = TextAlignment.CenterLeft,
                X = Application.GetRealWidth (130),
            };
            likelayout.AddChidren (loveList);
 
            var lovenext = new Button {
                Width = Application.GetRealWidth (87),
                Height = Application.GetRealHeight (100),
                UnSelectedImagePath = "MusicIcon/Next.png",
                SelectedImagePath = "MusicIcon/NextSelecte.png",
                X = Application.GetRealWidth (480),
            };
            likelayout.AddChidren (lovenext);
            EventHandler<MouseEventArgs> lovea31Source = (sender, e) => {
 
                MainPage.Loading.Start (Language.StringByID (MyInternationalizationString.load) + "...");
                System.Threading.Tasks.Task.Run (() => {
                    Application.RunOnMainThread (() => {
                        MainPage.Loading.Hide ();
                        A31LikeList a31likeList = new A31LikeList { };
                        MusicSourcePage.AddChidren (a31likeList);
                        a31likeList.Show ();
                    });
                });
            };
            loveList.MouseUpEventHandler += lovea31Source;
            lovenext.MouseUpEventHandler += lovea31Source;
            likehoto.MouseUpEventHandler += lovea31Source;
 
            #endregion
 
            #region  DLNA服务器音乐
            var rowlayout1 = new RowLayout {
                Height = Application.GetRealHeight (100),
            };
            middle.AddChidren (rowlayout1);
 
            var dlnahoto = new Button {
                Width = Application.GetRealWidth (61),
                Height = Application.GetRealHeight (81),
                UnSelectedImagePath = "MusicIcon/dlna.png",
                SelectedImagePath = "MusicIcon/dlnaSelected.png",
                TextAlignment = TextAlignment.CenterLeft,
                X = Application.GetRealWidth (30),
                Gravity = Gravity.CenterVertical,
            };
            rowlayout1.AddChidren (dlnahoto);
 
            var dlna = new Button {
                Height = Application.GetRealHeight (100),
                TextID = MyInternationalizationString.Musicdlna,
                TextAlignment = TextAlignment.CenterLeft,
                X = Application.GetRealWidth (130),
                Gravity = Gravity.CenterVertical,
            };
            rowlayout1.AddChidren (dlna);
 
            var dlnaback = new Button {
                Width = Application.GetRealWidth (87),
                Height = Application.GetRealHeight (100),
                UnSelectedImagePath = "MusicIcon/Next.png",
                SelectedImagePath = "MusicIcon/NextSelecte.png",
                X = Application.GetRealWidth (480),
                Gravity = Gravity.CenterVertical,
            };
            rowlayout1.AddChidren (dlnaback);
 
            EventHandler<MouseEventArgs> selectA31dlna = (sender, e) => {
                MainPage.Loading.Start (Language.StringByID (MyInternationalizationString.load) + "...");
                System.Threading.Tasks.Task.Run (() => {
                    List<MusicInfo> list = new List<MusicInfo> ();
                    DLNAServer dlnaServer = null;
                    try {
                        //创建Socket
                        System.Net.Sockets.Socket socket = new System.Net.Sockets.Socket (System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp);
                        //绑定本地端口
                        //socket.Bind (new System.Net.IPEndPoint (System.Net.IPAddress.Any, 1900));
 
                        for (int i = 10000; i < 65535; i++) {
                            try {
                                //绑定本地端口
                                socket.Bind (new System.Net.IPEndPoint (System.Net.IPAddress.Any, i));
                                break;
                            } catch { }
                        }
 
 
                        string s = "M-SEARCH * HTTP/1.1\r\nMan:\"ssdp:discover\"\r\nMx:3\r\nHost:239.255.255.250:1900\r\nSt: urn:schemas-upnp-org:device:MediaServer:1\r\n\r\n";
 
                        System.Net.EndPoint remoteIpEndPoint = new System.Net.IPEndPoint (0, 0);
                        byte [] bytes = System.Text.Encoding.ASCII.GetBytes (s);
                        //请求获取DLNA服务器信息
                        // socket.SendTo (bytes, System.Net.Sockets.SocketFlags.None, new System.Net.IPEndPoint (System.Net.IPAddress.Parse ("239.255.255.250"), 1900));
                        // socket.SendTo (bytes, System.Net.Sockets.SocketFlags.None, new System.Net.IPEndPoint (System.Net.IPAddress.Parse ("239.255.255.250"), 1900));
                        //如果500毫秒没有数据回复,就关闭当前Socket,不再等待接收数据
 
                        System.Threading.Tasks.Task.Run (() => {
                            DateTime dateTime = DateTime.Now;
                            while (socket != null) {
                                if (3000 < (DateTime.Now - dateTime).TotalMilliseconds) {
                                    break;
                                }
                                socket.SendTo (bytes, System.Net.Sockets.SocketFlags.None, new System.Net.IPEndPoint (System.Net.IPAddress.Parse ("239.255.255.250"), 1900));
                                System.Threading.Thread.Sleep (500);
                            }
 
                            if (socket != null) {
                                socket.Close ();
                                socket = null;
                            }
                        });
                        //接收回来的数据
                        byte [] receviceBytes = new byte [1024];
 
                        //开始在这里等待接收数据,
                        var len = socket.ReceiveFrom (receviceBytes, ref remoteIpEndPoint);
                        //接收到数据后就关闭Socket
                        socket.Close ();
                        socket = null;
 
                        var dd = System.Text.Encoding.UTF8.GetString (receviceBytes);
                        if (len != 0) {
                            var ms = new System.IO.MemoryStream (receviceBytes, 0, len);
                            var sr = new System.IO.StreamReader (ms);
                            string url = null;
                            string tempLine = null;
                            //一行一行数据判断,找出需要的信息
                            while ((tempLine = sr.ReadLine ()) != null) {
                                //找出Ip地址相关的信息
                                if (tempLine.StartsWith ("LOCATION: http://")) {
                                    dlnaServer = new DLNAServer ();
                                    url = tempLine.Replace ("LOCATION: ", "");
                                    dlnaServer.URL = tempLine.Replace ("LOCATION: http://", "").Split ('/') [0];
                                    break;
                                }
                            }
 
                            //关闭流
                            sr.Close ();
                            ms.Close ();
 
                            if (dlnaServer == null) {
                                return;
                            }
                            dlnaServer.ControlURL = getCommand (url);
 
                            var listResult = readDlnaList (dlnaServer, "0");
 
                            if (listResult != null) {
                                listResult = listResult.Replace ("&amp;", "&").Replace ("&lt;", "<").Replace ("&gt;", ">").Replace ("&quot;", "\"").Replace ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "");
 
                                int startIndex = listResult.IndexOf ("<DIDL-Lite");
                                int endIndex = listResult.IndexOf ("</DIDL-Lite>") + "</DIDL-Lite>".Length;
                                if (startIndex < endIndex) {
                                    var xml = new XmlDocument ();
                                    xml.LoadXml (listResult.Substring (startIndex, endIndex - startIndex));
                                    foreach (XmlNode node in xml.ChildNodes [0].ChildNodes) {
                                        MusicInfo musicInfo = new MusicInfo ();
                                        musicInfo.ID = node.Attributes ["id"].Value;
                                        foreach (XmlNode temNode in node.ChildNodes) {
                                            switch (temNode.Name) {
                                            case "dc:title":
                                                musicInfo.dlnalistName = temNode.InnerText;
                                                break;
                                            }
                                        }
                                        list.Add (musicInfo);
                                    }
                                }
                            }
                        }
                    } catch { } finally {
                        Application.RunOnMainThread (() => {
                            MainPage.Loading.Hide ();
                            if (dlnaServer == null) {
                                new Alert (Language.StringByID (MyInternationalizationString.Musicprompt), Language.StringByID (MyInternationalizationString.Musicdevice), Language.StringByID (MyInternationalizationString.MusicOK)).Show ();
                                return;
                            }
                            A31Dlna a31Dlna = new A31Dlna ();
                            MusicSourcePage.AddChidren (a31Dlna);
                            a31Dlna.show (dlnaServer, list, MusicSourcePage, PalyMusicPage);
                        });
                    }
                });
            };
            dlnahoto.MouseUpEventHandler += selectA31dlna;
            dlna.MouseUpEventHandler += selectA31dlna;
            dlnaback.MouseDownEventHandler += selectA31dlna;
            #endregion
 
            #region 在线电台
            var RowCn = new RowLayout {
                Height = Application.GetRealHeight (100),
            };
            middle.AddChidren (RowCn);
 
            var cn = new Button {
                Width = Application.GetRealWidth (61),
                Height = Application.GetRealHeight (81),
                UnSelectedImagePath = "MusicIcon/local.png",
                SelectedImagePath = "MusicIcon/localselected.png",
                TextAlignment = TextAlignment.CenterLeft,
                X = Application.GetRealWidth (30),
                Gravity = Gravity.CenterVertical,
            };
            RowCn.AddChidren (cn);
 
            var cnR = new Button {
                Height = Application.GetRealHeight (100),
                TextID = MyInternationalizationString.cnRadio,
                TextAlignment = TextAlignment.CenterLeft,
                X = Application.GetRealWidth (130),
                Gravity = Gravity.CenterVertical,
            };
            RowCn.AddChidren (cnR);
 
            var cnback = new Button {
                Width = Application.GetRealWidth (87),
                Height = Application.GetRealHeight (100),
                UnSelectedImagePath = "MusicIcon/Next.png",
                SelectedImagePath = "MusicIcon/NextSelecte.png",
                X = Application.GetRealWidth (480),
                Gravity = Gravity.CenterVertical,
            };
            RowCn.AddChidren (cnback);
 
            EventHandler<MouseEventArgs> CNresources = (sender, e) => {
                MainPage.Loading.Start (Language.StringByID (MyInternationalizationString.load) + "...");
                System.Threading.Tasks.Task.Run (() => {
                    var radioList = readRadioList ("http://opml.radiotime.com/Browse.ashx?partnerId=yvcOjvJP");
                    Application.RunOnMainThread (() => {
                        MainPage.Loading.Hide ();
                        A31CNRadio cnRadio = new A31CNRadio ();
                        MusicSourcePage.AddChidren (cnRadio);
                        cnRadio.show (a31, radioList, MusicSourcePage, PalyMusicPage);
                    });
                });
            };
            cn.MouseUpEventHandler += CNresources;
            cnR.MouseUpEventHandler += CNresources;
            cnback.MouseUpEventHandler += CNresources;
 
            #endregion
 
            #region Pandora潘多拉
 
            var pandoraRow = new RowLayout {
                Height = Application.GetRealHeight (100),
            };
            middle.AddChidren (pandoraRow);
 
            var p = new Button {
                Width = Application.GetRealWidth (61),
                Height = Application.GetRealHeight (81),
                UnSelectedImagePath = "MusicIcon/pandora.png",
                TextAlignment = TextAlignment.CenterLeft,
                X = Application.GetRealWidth (30),
                Gravity = Gravity.CenterVertical,
            };
            pandoraRow.AddChidren (p);
 
            var pandora = new Button {
                Height = Application.GetRealHeight (100),
                TextID = MyInternationalizationString.Pandora,
                TextAlignment = TextAlignment.CenterLeft,
                X = Application.GetRealWidth (130),
                Gravity = Gravity.CenterVertical,
            };
            pandoraRow.AddChidren (pandora);
 
            var pandoraback = new Button {
                Width = Application.GetRealWidth (87),
                Height = Application.GetRealHeight (100),
                UnSelectedImagePath = "MusicIcon/Next.png",
                SelectedImagePath = "MusicIcon/NextSelecte.png",
                X = Application.GetRealWidth (480),
                Gravity = Gravity.CenterVertical,
            };
            pandoraRow.AddChidren (pandoraback);
 
            EventHandler<MouseEventArgs> pandorasources = (sender, e) => {
                MainPage.Loading.Start (Language.StringByID (MyInternationalizationString.load) + "...");
                System.Threading.Tasks.Task.Run (() => {
                    Application.RunOnMainThread (() => {
                        MainPage.Loading.Hide ();
                        A31Pandora a31pandora = new A31Pandora ();
                        MusicSourcePage.AddChidren (a31pandora);
                        a31pandora.show (a31, MusicSourcePage, PalyMusicPage);
                    });
                });
 
            };
            pandora.MouseUpEventHandler += pandorasources;
            pandoraback.MouseUpEventHandler += pandorasources;
            p.MouseUpEventHandler += pandorasources;
 
            #endregion
 
            #region   Spotify
            {
                EventHandler<MouseEventArgs> myEvent = (sender, e) => {
                    string str = "com.spotify.music";
                    if (Shared.Application.DeviceType == Device.Ios) {
                        str = "spotify:";
                    }
                    CommonClass.OpenApp (str);
                };
                var Spotifylayout = new RowLayout {
                    Height = Application.GetRealHeight (100),
                };
                middle.AddChidren (Spotifylayout);
 
                var Spotifyhoto = new Button {
                    Width = Application.GetMinRealAverage (61),
                    Height = Application.GetMinRealAverage (81),
                    UnSelectedImagePath = "MusicIcon/Spotify.png",
                    SelectedImagePath = "MusicIcon/Spotifyselected.png",
                    TextAlignment = TextAlignment.CenterLeft,
                    X = Application.GetRealWidth (30),
                    Gravity = Gravity.CenterVertical,
                };
                Spotifylayout.AddChidren (Spotifyhoto);
 
 
                var Spotify = new Button {
                    Height = Application.GetRealHeight (100),
                    TextID = MyInternationalizationString.Spotify,
                    TextAlignment = TextAlignment.CenterLeft,
                    X = Application.GetRealWidth (130),
                    Gravity = Gravity.CenterVertical,
                };
                Spotifylayout.AddChidren (Spotify);
 
 
                var Spotifyback = new Button {
                    Width = Application.GetRealWidth (87),
                    Height = Application.GetRealHeight (100),
                    UnSelectedImagePath = "MusicIcon/Next.png",
                    SelectedImagePath = "MusicIcon/NextSelecte.png",
                    X = Application.GetRealWidth (480),
                    Gravity = Gravity.CenterVertical,
                };
                Spotifylayout.AddChidren (Spotifyback);
 
                Spotifyhoto.MouseUpEventHandler += myEvent;
                Spotify.MouseUpEventHandler += myEvent;
                Spotifyback.MouseUpEventHandler += myEvent;
 
            }
            #endregion
 
            #region      QQ音乐
            {
                EventHandler<MouseEventArgs> myEvent = (sender, e) => {
                    // if (Application.DeviceType == Device.Android)
                    //{
                    //    com.hdl.on.CommonClass.OpenApp("com.tencent.qqmusic");
                    //}
                    //else if (Application.DeviceType == Device.Ios)
                    //{
                    //    ON.Ios.CommonClass.OpenApp ("qqmusic://");
                    //}
 
                    string str = "com.tencent.qqmusic";
                    if (Shared.Application.DeviceType == Device.Ios) {
                        str = "qqmusic:";
                    }
                    CommonClass.OpenApp (str);
                };
                var qqmusiclayout = new RowLayout {
                    Height = Application.GetRealHeight (100),
                };
                middle.AddChidren (qqmusiclayout);
 
                var qqmusichoto = new Button {
                    Width = Application.GetMinRealAverage (61),
                    Height = Application.GetMinRealAverage (81),
                    UnSelectedImagePath = "MusicIcon/qqmusic.png",
                    SelectedImagePath = "MusicIcon/qqmusicselected.png",
                    TextAlignment = TextAlignment.CenterLeft,
                    X = Application.GetRealWidth (30),
                    Gravity = Gravity.CenterVertical,
                };
                qqmusiclayout.AddChidren (qqmusichoto);
 
                var qqmusic = new Button {
                    Height = Application.GetRealHeight (100),
                    TextID = MyInternationalizationString.qqmusic,
                    TextAlignment = TextAlignment.CenterLeft,
                    X = Application.GetRealWidth (130),
                    Gravity = Gravity.CenterVertical,
                };
                qqmusiclayout.AddChidren (qqmusic);
 
                var qqmusicback = new Button {
                    Width = Application.GetRealWidth (87),
                    Height = Application.GetRealHeight (100),
                    UnSelectedImagePath = "MusicIcon/Next.png",
                    SelectedImagePath = "MusicIcon/NextSelecte.png",
                    X = Application.GetRealWidth (480),
                    Gravity = Gravity.CenterVertical,
                };
                qqmusiclayout.AddChidren (qqmusicback);
 
                qqmusichoto.MouseUpEventHandler += myEvent;
                qqmusic.MouseUpEventHandler += myEvent;
                qqmusicback.MouseUpEventHandler += myEvent;
 
            }
            #endregion
 
            #region       蓝牙
            var bluetoothrowlayout = new RowLayout {
                Height = Application.GetRealHeight (100),
            };
            //支持蓝牙功能
            if (a31.A31DeviceType == 918) {
                middle.AddChidren (bluetoothrowlayout);
            }
            var bluetoothhoto = new Button {
                Width = Application.GetRealWidth (61),
                Height = Application.GetRealHeight (81),
                UnSelectedImagePath = "MusicIcon/bluetooth.png",
                SelectedImagePath = "MusicIcon/bluetoothselected.png",
                TextAlignment = TextAlignment.CenterLeft,
                X = Application.GetRealWidth (30),
                Gravity = Gravity.CenterVertical,
            };
            bluetoothrowlayout.AddChidren (bluetoothhoto);
 
            var bluetooth = new Button {
                Height = Application.GetRealHeight (100),
                TextID = MyInternationalizationString.Musicbluetooth,
                TextAlignment = TextAlignment.CenterLeft,
                X = Application.GetRealWidth (130),
            };
            bluetoothrowlayout.AddChidren (bluetooth);
 
            var nextbluetooth = new Button {
                Width = Application.GetRealWidth (87),
                Height = Application.GetRealHeight (100),
                UnSelectedImagePath = "MusicIcon/Next.png",
                SelectedImagePath = "MusicIcon/NextSelecte.png",
                TextAlignment = TextAlignment.CenterLeft,
                X = Application.GetRealWidth (480),
                Gravity = Gravity.CenterVertical,
            };
            bluetoothrowlayout.AddChidren (nextbluetooth);
 
            EventHandler<MouseEventArgs> bluetoothsources = (sender, e) => {
 
                bluetoothrowlayout.BackgroundColor = 0x00000000;
                //MainPage.Loading.Start (Language.StringByID (MyInternationalizationString.load) + "...");
                System.Threading.Tasks.Task.Run (() => {
                    sendCommand ("bluetooth");
                    //Application.RunOnMainThread (() => {
                    //    MainPage.Loading.Hide ();
 
                    //});
                });
            };
 
            bluetoothhoto.MouseUpEventHandler += bluetoothsources;
            bluetooth.MouseUpEventHandler += bluetoothsources;
            nextbluetooth.MouseUpEventHandler += bluetoothsources;
 
            EventHandler<MouseEventArgs> bluetootmou = (sender, e) => {
                bluetoothrowlayout.BackgroundColor = 0xffFE5E00;
            };
            bluetoothhoto.MouseDownEventHandler += bluetootmou;
            bluetooth.MouseDownEventHandler += bluetootmou;
            nextbluetooth.MouseDownEventHandler += bluetootmou;
 
 
            #endregion
 
            #region   我的设置
 
            var mystelayout = new RowLayout {
                Height = Application.GetRealHeight (100),
            };
            middle.AddChidren (mystelayout);
 
            var sethoto = new Button {
                Width = Application.GetRealWidth (61),
                Height = Application.GetRealHeight (81),
                UnSelectedImagePath = "MusicIcon/set.png",
                SelectedImagePath = "MusicIcon/setSelected.png",
                TextAlignment = TextAlignment.CenterLeft,
                X = Application.GetRealWidth (30),
                Gravity = Gravity.CenterVertical
            };
            mystelayout.AddChidren (sethoto);
 
            var set = new Button {
                Height = Application.GetRealHeight (100),
                TextID = MyInternationalizationString.Musicset,
                TextAlignment = TextAlignment.CenterLeft,
                X = Application.GetRealWidth (130),
                Gravity = Gravity.CenterVertical,
            };
            mystelayout.AddChidren (set);
 
            var setback = new Button {
                Width = Application.GetRealWidth (87),
                Height = Application.GetRealHeight (100),
                UnSelectedImagePath = "MusicIcon/Next.png",
                SelectedImagePath = "MusicIcon/NextSelecte.png",
                X = Application.GetRealWidth (480),
                Gravity = Gravity.CenterVertical,
            };
            mystelayout.AddChidren (setback);
 
            EventHandler<MouseEventArgs> setsources = (sender, e) => {
 
                MainPage.Loading.Start (Language.StringByID (MyInternationalizationString.load) + "...");
                System.Threading.Tasks.Task.Run (() => {
                    Application.RunOnMainThread (() => {
                        MainPage.Loading.Hide ();
                        A31Rename rename = new A31Rename ();
                        MusicSourcePage.AddChidren (rename);
                        rename.show (a31);
                    });
                });
            };
            sethoto.MouseUpEventHandler += setsources;
            set.MouseUpEventHandler += setsources;
            setback.MouseUpEventHandler += setsources;
 
            #endregion
 
            #region 删除当前音乐播放器
            var delrow = new RowLayout {
                Height = Application.GetRealHeight (100),
            };
            //middle.AddChidren (delrow);
 
            var delhoto = new Button {
                Width = Application.GetRealWidth (61),
                Height = Application.GetRealHeight (81),
                UnSelectedImagePath = "MusicIcon/delplayer.png",
                TextAlignment = TextAlignment.CenterLeft,
                X = Application.GetRealWidth (30),
                Gravity = Gravity.CenterVertical,
            };
            delrow.AddChidren (delhoto);
 
            var delplayer = new Button {
                Height = Application.GetRealHeight (100),
                TextID = MyInternationalizationString.Deletemusicplayer,
                TextAlignment = TextAlignment.CenterLeft,
                X = Application.GetRealWidth (130),
                Gravity = Gravity.CenterVertical,
            };
            delrow.AddChidren (delplayer);
 
            EventHandler<MouseEventArgs> delmusicplayer = (sender, e) => {
                var alert = new Alert (Language.StringByID (MyInternationalizationString.prompt), Language.StringByID (MyInternationalizationString.Deletemusicplayer),
                                                          Language.StringByID (MyInternationalizationString.cancel), Language.StringByID (MyInternationalizationString.confirm));
                alert.ResultEventHandler += (sender1, e1) => {
                    if (e1) {
 
                        A31MusicModel.A31MusicModelList.Remove (a31);
                        A31MusicModel.Save ();
 
                        PalyMusicPage.RemoveFromParent ();
                        MusicSourcePage.RemoveFromParent ();
                        //bordorView.RemoveFromParent ();
                        //MyMusic mymusic = new MyMusic ();
                        //bordorView.AddChidren (mymusic);
                        //mymusic.Show (MusicSourcePage, PalyMusicPage);
                    } else { }
                };
                alert.Show ();
 
            };
 
            delhoto.MouseUpEventHandler += delmusicplayer;
            delplayer.MouseUpEventHandler += delmusicplayer;
 
            #endregion
 
        }
 
        /// <summary>
        /// 读取歌曲信息
        /// </summary>
        /// <param name="songName"></param>
        /// <returns></returns>       
        string usbserarchSong (string id)
        {
            Shared.Net.MyWebClient webClient = new Shared.Net.MyWebClient ();
            try {
                byte [] recevieBytes = webClient.DownloadData (new Uri ("http://" + id + "/httpapi.asp?command=getFileInfo" + ":" + 0 + ":" + 500));
                return System.Text.Encoding.UTF8.GetString (recevieBytes, 0, recevieBytes.Length);
            } catch {
                return "";
            }
        }
 
        /// <summary>
        /// 读取DLNA的音乐列表
        /// </summary>
        /// <param name="id">列表ID</param>
        /// <returns>读取到的音乐列表信息,读取不到反馈为null</returns>
        string readDlnaList (DLNAServer dlnaServer, string id)
        {
            string tempString = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?><s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><u:Browse xmlns:u=\"urn:schemas-upnp-org:service:ContentDirectory:1\"><ObjectID>" + id + "</ObjectID><BrowseFlag>BrowseDirectChildren</BrowseFlag><Filter>*</Filter><StartingIndex>0</StartingIndex><RequestedCount>999</RequestedCount><SortCriteria>+dc:title</SortCriteria></u:Browse></s:Body></s:Envelope>";
 
            Shared.Net.MyWebClient webClient = new Shared.Net.MyWebClient ();
            webClient.Proxy = null;
            webClient.Headers.Add ("Content-type", "text/xml;charset=\"utf-8\"");
            webClient.Headers.Add ("Soapaction", "\"urn:schemas-upnp-org:service:ContentDirectory:1#Browse\"");
            try {
                byte [] recevieBytes = webClient.UploadData (new Uri ("http://" + dlnaServer.URL + dlnaServer.ControlURL), "POST", System.Text.Encoding.UTF8.GetBytes (tempString));
                return System.Text.Encoding.UTF8.GetString (recevieBytes, 0, recevieBytes.Length);
            } catch (Exception e) {
                return null;
            }
        }
 
        string getCommand (string url)
        {
            Shared.Net.MyWebClient webClient = new Shared.Net.MyWebClient ();
            try {
                var ddd = SecurityElement.FromString (webClient.DownloadString (url)).SearchForChildByTag ("device").SearchForChildByTag ("serviceList");
                foreach (SecurityElement se in ddd.Children) {
                    if (se.SearchForTextOfTag ("serviceType").Contains ("urn:schemas-upnp-org:service:ContentDirectory")) {
                        return se.SearchForTextOfTag ("controlURL");
                    }
                }
            } catch (Exception e) {
            }
            return "/ContentDirectory/Control";
        }
 
        /// <summary>
        /// 读取电台组列表
        /// </summary>
        /// <returns>读取到的电台组列表信息,读取不到反馈为null</returns>
        string readRadioList (string url)
        {
            Shared.Net.MyWebClient webClient = new Shared.Net.MyWebClient ();
            webClient.Proxy = null;
            webClient.Headers.Add ("Content-type", "plain/text; charset=UTF-8");
            //System.Console.WriteLine("===========" + Language.CurrentLanguage);
            if (Language.CurrentLanguage == "Chinese") {
                webClient.Headers.Add ("Accept-Language", "zh-cn");
            } else {
                webClient.Headers.Add ("Accept-Language", "en-us");
            }
            try {
                byte [] recevieBytes = webClient.DownloadData (new Uri (url));
                return System.Text.Encoding.UTF8.GetString (recevieBytes, 0, recevieBytes.Length);
            } catch (Exception e) {
                return null;
            }
        }
 
        void sendCommand (string coutn)
        {
            System.Threading.Tasks.Task.Run (() => {
                Shared.Net.MyWebClient webClient = new Shared.Net.MyWebClient ();
                try {
                    byte [] recevieBytes1 = webClient.DownloadData (new Uri ("http://" + a31MusicModel.IPAddress + "/httpapi.asp?command=setPlayerCmd:switchmode:" + coutn));
                } catch (Exception ex) {
                    //string ee = ex.Message;
                }
            });
        }
 
 
        static string userName = "ieast";
        static string password = "60b25158ee94";
 
        public static Airable ReadRadiohome (string url)
        {
            System.IO.StringReader sr = null;
            Airable airable = null;
            try {
                Shared.Net.MyWebClient webClient = new Shared.Net.MyWebClient ();
                webClient.Headers.Add ("CONTENT-TYPE", "text/xml;charset=\"utf-8\"");
 
                webClient.Headers.Add ("Authorization", "Basic " + Convert.ToBase64String (Encoding.ASCII.GetBytes (userName + ":" + password)));
                byte [] recevieBytes = webClient.DownloadData (new Uri (url));
                var ddd = Encoding.UTF8.GetString (recevieBytes, 0, recevieBytes.Length);
                sr = new System.IO.StringReader (Encoding.UTF8.GetString (recevieBytes, 0, recevieBytes.Length));
                string line = null;
                StringBuilder stringBuilder = null;
                while ((line = sr.ReadLine ()) != null) {
                    line = line.Trim ();
                    if (stringBuilder == null) {
                        if (line.EndsWith ("<div class=\"col-50\">") && sr.ReadLine ().Trim () == "<code>{") {
                            stringBuilder = new StringBuilder ();
                            stringBuilder.AppendLine ("{");
                        }
                    } else {
                        if (line.EndsWith ("</code>")) {
                            stringBuilder.AppendLine ("}");
                            string s = @stringBuilder.ToString ().Replace (",}", "}").Replace ("\"content\": []", "\"content\": \"\"");
                            airable = Newtonsoft.Json.JsonConvert.DeserializeObject<Airable> (@s);
                            break;
                        } else {
                            if (line.Contains ("</a>")) {
                                line = line.Remove (line.IndexOf ("<a"), line.IndexOf ("\">") - line.IndexOf ("<a") + 2).Replace ("</a>", "");
                            }
                            stringBuilder.AppendLine (@line);
                        }
                    }
                }
            } catch { } finally {
                if (sr != null)
                    sr.Close ();
            }
            return airable;
        }
 
        /// <summary>
        /// 获取当前播放的列表
        /// </summary>
        string getUSBPlayList ()
        {
            StringBuilder getPlayList = new StringBuilder ();
 
            getPlayList.AppendLine ("<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">");
            getPlayList.AppendLine ("<s:Body>");
            getPlayList.AppendLine ("<u:BrowseQueue xmlns:u=\"urn:schemas-wiimu-com:service:PlayQueue:1\">");
            getPlayList.AppendLine ("<QueueName>USBDiskQueue</QueueName>");
            getPlayList.AppendLine ("</u:BrowseQueue>");
            getPlayList.AppendLine ("</s:Body>");
            getPlayList.AppendLine ("</s:Envelope>");
 
            Shared.Net.MyWebClient webClient = new Shared.Net.MyWebClient ();
            webClient.Headers.Add ("SOAPACTION", "\"urn:schemas-wiimu-com:service:PlayQueue:1#BrowseQueue\"");
            webClient.Headers.Add ("CONTENT-TYPE", "text/xml; charset=\"utf-8\"");
            try {
                byte [] recevieBytes = webClient.UploadData (new Uri ("http://" + A31MusicModel.Current.IPAddress + ":" + A31MusicModel.Current.Port + "/upnp/control/PlayQueue1"), "POST", System.Text.Encoding.UTF8.GetBytes (getPlayList.ToString ()));
                return System.Text.Encoding.UTF8.GetString (recevieBytes, 0, recevieBytes.Length);
            } catch { }
            return null;
        }
 
    }
}