wxr
2020-09-29 51cb254cbd616e84b7a466b195eb43ab097c3897
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
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Security;
using System.Text;
using Shared;
 
namespace HDL_ON.UI.Music
{
    public class SendMethod
    {
        /// <summary>
        ///搜索A31音乐播放器
        /// </summary>
        /// <param name="action"></param>
        /// <param name="time"></param>
        /// <param name="uid"></param>
        public static void Seach(Action<A31MusicModel> action, int time = 5 * 1000, string uid = "AllUniqueDeviceName")
        {
            System.Threading.Tasks.Task.Run(() =>
            {
                System.Net.Sockets.UdpClient udpClient = null;
                int localPort = 65535;
                for (; 1024 < localPort; localPort--)
                {
                    try
                    {
                        udpClient = new System.Net.Sockets.UdpClient(localPort);
                        break;
                    }
                    catch (Exception e) { System.Console.WriteLine(e.Message); }
                }
 
                System.Threading.Tasks.Task.Run(() =>
                {
                    var tempDateTime = DateTime.Now;
                    while (udpClient != null)
                    {
                        try
                        {
                            if (time < (DateTime.Now - tempDateTime).TotalMilliseconds)
                            {
                                var tempBytes = System.Text.Encoding.UTF8.GetBytes("完成");
                                udpClient.Send(tempBytes, tempBytes.Length, new System.Net.IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"), localPort));
                            }
                            else
                            {
                                var stringBuilder = new StringBuilder();
                                stringBuilder.AppendLine("M-SEARCH * HTTP/1.1");
                                stringBuilder.AppendLine("St: ssdp:wiimudevice");
                                stringBuilder.AppendLine("Mx: 3");
                                stringBuilder.AppendLine("Host: 239.255.255.250:1900");
                                stringBuilder.AppendLine("Man: \"ssdp:discover\"");
                                stringBuilder.AppendLine();
                                var tempBytes = System.Text.Encoding.ASCII.GetBytes(stringBuilder.ToString());
                                //请求获取A31服务器信息
                                udpClient.Send(tempBytes, tempBytes.Length, new System.Net.IPEndPoint(System.Net.IPAddress.Parse("239.255.255.250"), 1900));
                                udpClient.Send(tempBytes, tempBytes.Length, new System.Net.IPEndPoint(System.Net.IPAddress.Parse("239.255.255.250"), 1900));
                                //如果1000毫秒没有数据回复,就关闭当前Socket,不再等待接收数据
                            }
                            System.Threading.Thread.Sleep(500);
                        }
                        catch (Exception e) { System.Console.WriteLine(e.Message); }
                    }
                });
                while (true)
                {
                    try
                    {
                        //接收回来的数据
                        var remoteIpEndPoint = new System.Net.IPEndPoint(0, 0);
                        //开始在这里等待接收数据,
                        var receviceBytes = udpClient.Receive(ref remoteIpEndPoint);
                        if (receviceBytes == null)
                        {
                            break;
                        }
                        if ("完成" == System.Text.Encoding.UTF8.GetString(receviceBytes))
                        {
                            if (action != null)
                            {
                                //表示完成了
                                action(null);
                            }
                            try
                            {
                                udpClient.Close();
                                udpClient = null;
                            }
                            catch (Exception e) { System.Console.WriteLine(e.Message); }
                            break;
                        }
 
                        var sr = new System.IO.StreamReader(new System.IO.MemoryStream(receviceBytes, 0, receviceBytes.Length));
                        string tempLine = null;
                        string ipAddress = null;
                        int port = 0;
                        string uniqueDeviceName = null;
                        //一行一行数据判断,找出需要的信息
                        while ((tempLine = sr.ReadLine()) != null)
                        {
                            //找出Ip地址相关的信息
                            //System.Console.WriteLine (tempLine);
                            if (tempLine.StartsWith("LOCATION: http://"))
                            {
                                tempLine = tempLine.Replace("LOCATION: http://", "").Split('/')[0];
                                string[] ipAndPort = tempLine.Split(':');
                                ipAddress = ipAndPort[0];
                                port = int.Parse(ipAndPort[1]);
                            }
                            else if (tempLine.StartsWith("USN: uuid:"))
                            {
                                uniqueDeviceName = tempLine.Replace("USN: uuid:", "").Split(':')[0];
                            }
                        }
                        //关闭流
                        sr.Close();
 
                        if (action != null)
                        {
                            if ("AllUniqueDeviceName" == uid)
                            {
                                action(new A31MusicModel { IPAddress = ipAddress, Port = port, Name = GetDeviceName(ipAddress, port), UniqueDeviceName = uniqueDeviceName });
                            }
                            else if (uid == uniqueDeviceName)
                            {
                                if (action != null)
                                {
                                    //表示完成了
                                    action(null);
                                }
                                try
                                {
                                    udpClient.Close();
                                    udpClient = null;
                                }
                                catch (Exception e) { System.Console.WriteLine(e.Message); }
                                break;
                            }
                        }
                    }
                    catch (Exception e) { System.Console.WriteLine(e.Message); }
                }
            });
        }
        /// <summary>
        /// 更新A31播放器的状态
        /// </summary>
        /// <param name="a31MusicModel"></param>
        public static void ReadStatus(A31MusicModel a31MusicModel)
        {
            try
            {
                if (a31MusicModel.ServerClientType == 1)
                {
                    var result = OpenWeb("http://" + a31MusicModel.IPAddress + "/httpapi.asp?command=multiroom:getSlaveList");
                    if (result != null && result != "Failed")
                    {
                        var slaves = Newtonsoft.Json.JsonConvert.DeserializeObject<Slaves>(result);
                        if (slaves != null && slaves.slave_list != null && slaves.slave_list.Count != 0)
                        {
                            a31MusicModel.Slave = slaves;
                        }
                    }
                }
 
                WebClient webClient = new WebClient();
                webClient.Headers.Add("Soapaction", "\"urn:schemas-upnp-org:service:AVTransport:1#GetInfoEx\"");
                webClient.Headers.Add("CONTENT-TYPE", "text/xml; charset=\"utf-8\"");
 
                var recevieBytes = webClient.UploadData(new Uri("http://" + a31MusicModel.IPAddress + ":" + a31MusicModel.Port + "/upnp/control/rendertransport1"), "POST", System.Text.Encoding.UTF8.GetBytes("<?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:GetInfoEx xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID></u:GetInfoEx></s:Body></s:Envelope>"));
                a31MusicModel.LastDateTime = DateTime.Now;//记录数据反馈的时间(因为有时间网络差数据不回复,导致当前播放音乐时间显示不正确)
 
                var se = System.Security.SecurityElement.FromString(System.Text.Encoding.UTF8.GetString(recevieBytes)).SearchForChildByTag("s:Body").SearchForChildByTag("u:GetInfoExResponse");
 
                if ("PLAYING" == se.SearchForTextOfTag("CurrentTransportState"))
                {
                    a31MusicModel.A31PlayStatus.status = "play";
                }
                else
                {
                    a31MusicModel.A31PlayStatus.status = "stop";
                }
                a31MusicModel.A31PlayStatus.totlen = (DateTime.Parse(se.SearchForTextOfTag("TrackDuration")) - DateTime.Parse("00:00:00")).TotalMilliseconds.ToString();
 
                var trackMetaData = se.SearchForTextOfTag("TrackMetaData");
                if (string.IsNullOrEmpty(trackMetaData))
                {
                    return;
                }
                if (A31MusicModel.IsJson(trackMetaData))
                {
                    var a31QQSong = Newtonsoft.Json.JsonConvert.DeserializeObject<A31QQSong>(trackMetaData);
                    a31MusicModel.A31PlayStatus.Title = a31QQSong.title;
                    a31MusicModel.A31PlayStatus.Album = a31QQSong.album;
                    a31MusicModel.A31PlayStatus.Artist = a31QQSong.creator;
                }
                else
                {
                    var metadata = trackMetaData.Replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "").Replace("&", "&amp;amp;");
                    var item = SecurityElement.FromString(metadata).SearchForChildByTag("item");
                    a31MusicModel.A31PlayStatus.Title = item.SearchForTextOfTag("dc:title");
                    a31MusicModel.A31PlayStatus.Artist = item.SearchForTextOfTag("upnp:artist");
                    a31MusicModel.A31PlayStatus.Album = item.SearchForTextOfTag("upnp:album");
                }
                a31MusicModel.A31PlayStatus.curpos = (DateTime.Parse(se.SearchForTextOfTag("RelTime")) - DateTime.Parse("00:00:00")).TotalMilliseconds.ToString();
                a31MusicModel.A31PlayStatus.vol = se.SearchForTextOfTag("CurrentVolume");
                a31MusicModel.A31PlayStatus.loop = se.SearchForTextOfTag("LoopMode");
                a31MusicModel.A31PlayStatus.Source = se.SearchForTextOfTag("PlayMedium");
                a31MusicModel.A31PlayStatus.playSource = se.SearchForTextOfTag("TrackSource");
                a31MusicModel.A31PlayStatus.TrackURL = se.SearchForTextOfTag("TrackURI").Replace("&", "&amp;amp;"); 
 
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                #region 更新其他界面音乐状态
                Application.RunOnMainThread(() =>
                {
                    a31MusicModel.trait_on_off.value = a31MusicModel.A31PlayStatus.status == "play" ? "on" : "off";
                    //Console.WriteLine($"music : {a31MusicModel.sid} : {a31MusicModel.on_off}");
                    HomePage.UpdataFunctionStates(a31MusicModel);
                    foreach (var e in A31MusicModel.A31MusicModelList)
                    {
                        RoomPage.UpdataStates(a31MusicModel);
                    }
                });
                #endregion
            }
        }
 
        public static string OpenWeb(string url)
        {
            try
            {
                var webClient = new WebClient();
                return webClient.DownloadString(url);
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
                return null;
            }
        }
        /// <summary>
        /// 获取A31的名称
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        static string GetDeviceName(string ip, int port)
        {
            string deviceName = null;
            System.IO.StreamReader sr = null;
            WebClient webClient = new WebClient();
            try
            {
                var receviceBytes = webClient.DownloadData(new Uri("http://" + ip + ":" + port + "/description.xml"));
                sr = new System.IO.StreamReader(new System.IO.MemoryStream(receviceBytes), Encoding.UTF8);
                string line = null;
                string deviceType = null;
 
                while ((line = sr.ReadLine()) != null)
                {
                    //System.Console.WriteLine (line);
                    if (line.StartsWith("<friendlyName>"))
                    {
                        deviceName = line.Replace("<friendlyName>", "").Replace("</friendlyName>", "");
                    }
                    else if (line.StartsWith("<manufacturer>"))
                    {
                        deviceType = line.Replace("<manufacturer>", "").Replace("</manufacturer>", "");
                    }
                }
                switch (deviceType)
                {
                    case "iEAST":
                    case "Linkplay Technology Inc.":
                        break;
                    //不是A31的音乐数据
                    default:
                        deviceName = null;
                        break;
                }
 
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
            }
            finally
            {
                if (sr != null)
                {
                    sr.Close();
                }
            }
            return deviceName;
        }
 
        /// <summary>
        /// 切换播放器模式,修改音乐名称,切换蓝牙,线路输入
        /// </summary>
        /// <param name="coutn"></param>
        public static void SendCommand(string url)
        {
            System.Threading.Tasks.Task.Run(() =>
            {
                WebClient webClient = new WebClient();
                try
                {
                    byte[] recevieBytes1 = webClient.DownloadData(new Uri(url));
                }
                catch (Exception ex)
                {
                    //this.IPAddress = ex.Message;
                }
            });
        }
        /// <summary>
        /// 播放
        /// </summary>
        public static void Play(A31MusicModel a31player)
        {
            System.Threading.Tasks.Task.Run(() =>
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">");
                sb.AppendLine("<s:Body>");
                sb.AppendLine("<u:Play xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\">");
                sb.AppendLine("<InstanceID>0</InstanceID>");
                sb.AppendLine("<Speed>1</Speed>");
                sb.AppendLine("</u:Play>");
                sb.AppendLine("</s:Body>");
                sb.AppendLine("</s:Envelope>");
 
                System.Net.WebClient webClient = new System.Net.WebClient();
                webClient.Headers.Add("SOAPACTION", "\"urn:schemas-upnp-org:service:AVTransport:1#Play\"");
                webClient.Headers.Add("CONTENT-TYPE", "text/xml; charset=\"utf-8\"");
                try
                {
                    webClient.UploadData(new Uri("http://" + a31player.IPAddress + ":" + a31player.Port + "/upnp/control/rendertransport1"), "POST", Encoding.UTF8.GetBytes(sb.ToString()));
                }
                catch { }
            });
        }
        /// <summary>
        /// 暂停
        /// </summary>
        public static void Pause(A31MusicModel a31player)
        {
            System.Threading.Tasks.Task.Run(() =>
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">");
                sb.AppendLine("<s:Body>");
                sb.AppendLine("<u:Pause xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\">");
                sb.AppendLine("<InstanceID>0</InstanceID>");
                sb.AppendLine("</u:Pause>");
                sb.AppendLine("</s:Body>");
                sb.AppendLine("</s:Envelope>");
 
                System.Net.WebClient webClient = new System.Net.WebClient();
                webClient.Headers.Add("SOAPACTION", "\"urn:schemas-upnp-org:service:AVTransport:1#Pause\"");
                webClient.Headers.Add("CONTENT-TYPE", "text/xml; charset=\"utf-8\"");
                try
                {
                    webClient.UploadData(new Uri("http://" + a31player.IPAddress + ":" + a31player.Port + "/upnp/control/rendertransport1"), "POST", Encoding.UTF8.GetBytes(sb.ToString()));
                }
                catch { }
            });
 
        }
        /// <summary>
        /// 下一曲
        /// </summary>
        public static void Next(A31MusicModel a31player)
        {
            System.Threading.Tasks.Task.Run(() =>
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">");
                sb.AppendLine("<s:Body>");
                sb.AppendLine("<u:Next xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\">");
                sb.AppendLine("<InstanceID>0</InstanceID>");
                sb.AppendLine("</u:Next>");
                sb.AppendLine("</s:Body>");
                sb.AppendLine("</s:Envelope>");
 
                System.Net.WebClient webClient = new System.Net.WebClient();
                webClient.Headers.Add("SOAPACTION", "\"urn:schemas-upnp-org:service:AVTransport:1#Next\"");
                webClient.Headers.Add("CONTENT-TYPE", "text/xml; charset=\"utf-8\"");
                try
                {
                    webClient.UploadData(new Uri("http://" + a31player.IPAddress + ":" + a31player.Port + "/upnp/control/rendertransport1"), "POST", Encoding.UTF8.GetBytes(sb.ToString()));
                }
                catch { }
            });
 
        }
        /// <summary>
        /// 上一曲
        /// </summary>
        public static void Previous(A31MusicModel a31player)
        {
            System.Threading.Tasks.Task.Run(() =>
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">");
                sb.AppendLine("<s:Body>");
                sb.AppendLine(" <u:Previous xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\">");
                sb.AppendLine("<InstanceID>0</InstanceID>");
                sb.AppendLine("</u:Previous>");
                sb.AppendLine("</s:Body>");
                sb.AppendLine("</s:Envelope>");
 
                System.Net.WebClient webClient = new System.Net.WebClient();
                webClient.Headers.Add("SOAPACTION", "\"urn:schemas-upnp-org:service:AVTransport:1#Previous\"");
                webClient.Headers.Add("CONTENT-TYPE", "text/xml; charset=\"utf-8\"");
                try
                {
                    webClient.UploadData(new Uri("http://" + a31player.IPAddress + ":" + a31player.Port + "/upnp/control/rendertransport1"), "POST", Encoding.UTF8.GetBytes(sb.ToString()));
                }
                catch { }
            });
 
        }
        /// <summary>
        /// 快进
        /// </summary>
        public static void Seek(string seekvolume, A31MusicModel a31player)
        {
            System.Threading.Tasks.Task.Run(() =>
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">");
                sb.AppendLine("<s:Body>");
                sb.AppendLine(" <u:Seek xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\">");
                sb.AppendLine("<InstanceID>0</InstanceID>");
                sb.AppendLine("<Unit>REL_TIME</Unit>");
                sb.AppendLine("<Target>" + seekvolume + "</Target>");
                sb.AppendLine("</u:Seek>");
                sb.AppendLine("</s:Body>");
                sb.AppendLine("</s:Envelope>");
 
                System.Net.WebClient webClient = new System.Net.WebClient();
                webClient.Headers.Add("SOAPACTION", "\"urn:schemas-upnp-org:service:AVTransport:1#Seek\"");
                webClient.Headers.Add("CONTENT-TYPE", "text/xml; charset=\"utf-8\"");
                try
                {
                    webClient.UploadData(new Uri("http://" + a31player.IPAddress + ":" + a31player.Port + "/upnp/control/rendertransport1"), "POST", Encoding.UTF8.GetBytes(sb.ToString()));
                }
                catch { }
            });
        }
        /// <summary>
        /// 调节音量
        /// </summary>
        /// <param name="volume">Volume.</param>
        public static void ControlVolume(int volume, A31MusicModel a31player)
        {
            System.Threading.Tasks.Task.Run(() => {
                A31MusicModel.ProgressDateTime = DateTime.Now;
                try
                {
                    if (a31player.ServerClientType == -1)
                    {
                        new System.Net.WebClient().DownloadData(new Uri("http://" + a31player.MainPlayIP + "/httpapi.asp?command=multiroom:SlaveVolume:" + a31player.IPAddress + ":" + volume));
                    }
                    else if (a31player.ServerClientType == 1)
                    {
                        new System.Net.WebClient().DownloadData(new Uri("http://" + a31player.IPAddress + "/httpapi.asp?command=setPlayerCmd:slave_vol:" + volume));
                    }
                    else
                    {
                        var sb = new StringBuilder();
                        sb.AppendLine("<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">");
                        sb.AppendLine("<s:Body>");
                        sb.AppendLine("<u:SetVolume xmlns:u=\"urn:schemas-upnp-org:service:RenderingControl:1\">");
                        sb.AppendLine("<InstanceID>0</InstanceID>");
                        sb.AppendLine("<Channel>Master</Channel>");
                        sb.AppendLine("<DesiredVolume>" + volume + "</DesiredVolume>");
                        sb.AppendLine("</u:SetVolume>");
                        sb.AppendLine("</s:Body>");
                        sb.AppendLine("</s:Envelope>");
 
                        var webClient = new System.Net.WebClient();
                        webClient.Headers.Add("SOAPACTION", "\"urn:schemas-upnp-org:service:RenderingControl:1#SetVolume\"");
                        webClient.Headers.Add("CONTENT-TYPE", "text/xml; charset=\"utf-8\"");
                        webClient.UploadData(new Uri("http://" + a31player.IPAddress + ":" + a31player.Port + "/upnp/control/rendercontrol1"), "POST", Encoding.UTF8.GetBytes(sb.ToString()));
                    }
                }
                catch { }
            });
 
        }
        /// <summary>
        /// 获取USb的列表
        /// </summary>
        public static List<MusicInfo> GetUsbList(A31MusicModel a31player)
        {
            var musicInfoList = new List<MusicInfo>();
            musicInfoList.Clear();
            var usbString = GetUSBPlayList(a31player);
            if (usbString == null)
            {
                return musicInfoList;
            }
            var se = System.Security.SecurityElement.FromString(usbString);
            if (se == null)
            {
                return musicInfoList;
            }
            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").Replace("&", "&amp;amp;");
                var metadata = track.SearchForTextOfTag("Metadata").Replace("&", "&amp;");
                var item = SecurityElement.FromString(metadata).SearchForChildByTag("item");
                musicInfo.Title = item.SearchForTextOfTag("dc:title").Replace("&", "&amp;amp;"); 
                musicInfo.Artist = item.SearchForTextOfTag("upnp:artist").Replace("&", "&amp;amp;");
                musicInfo.Album = item.SearchForTextOfTag("upnp:album").Replace("&", "&amp;amp;");
                musicInfo.Duration = item.SearchForTextOfTag("res");
                musicInfo.AlbumId = item.SearchForTextOfTag("song:albumid").Replace("&", "&amp;amp;");
                musicInfoList.Add(musicInfo);
            }
            return musicInfoList;
        }
        /// <summary>
        ///请求USB列表命令
        /// </summary>
        public static string GetUSBPlayList(A31MusicModel a31player)
        {
 
            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>");
 
            WebClient webClient = new WebClient();
            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://" + a31player.IPAddress + ":" + a31player.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;
        }
 
        /// <summary>
        /// 读取电台组列表
        /// </summary>
        /// <returns>读取到的电台组列表信息,读取不到反馈为null</returns>
        public static string ReadRadioList(string url)
        {
            WebClient webClient = new WebClient();
            webClient.Proxy = null;
            webClient.Headers.Add("Content-type", "plain/text; charset=UTF-8");
            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;
            }
 
        }
 
        /// <summary>
        /// 获取当前播放的列表
        /// </summary>
        public static  string GetCurrentPlayList(A31MusicModel a31player)
        {
            System.Text.StringBuilder getPlayList = new System.Text.StringBuilder();
            getPlayList.AppendLine("<?xml version=\"1.0\"encoding=\"utf-8\"?>");
            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>CurrentQueue</QueueName>");
            getPlayList.AppendLine("</u:BrowseQueue>");
            getPlayList.AppendLine("</s:Body>");
            getPlayList.AppendLine("</s:Envelope>");
 
            System.Net.WebClient webClient = new System.Net.WebClient();
            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://" + a31player.IPAddress + ":" + a31player.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;
        }
        /// <summary>
        /// 播放USB音乐命令
        /// </summary>
        /// <param name="listName">列表名称</param>
        /// <param name="number">播放索引</param>
        public static void ListMusicPlay(string listName, object number, A31MusicModel a31player)
        {
            System.Text.StringBuilder playstrings = new System.Text.StringBuilder();
            playstrings.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>");
            playstrings.AppendLine("<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">");
            playstrings.AppendLine("<s:Body>");
            playstrings.AppendLine("<u:PlayQueueWithIndex xmlns:u=\"urn:schemas-wiimu-com:service:PlayQueue:1\">");
            playstrings.AppendLine("<QueueName>" + listName + "</QueueName>");
            playstrings.AppendLine("<Index>" + number + "</Index>");
            playstrings.AppendLine("</u:PlayQueueWithIndex>");
            playstrings.AppendLine("</s:Body>");
            playstrings.AppendLine("</s:Envelope>");
            System.Net.WebClient webClient = new System.Net.WebClient();
            webClient.Headers.Add("SOAPACTION", "\"urn:schemas-wiimu-com:service:PlayQueue:1#PlayQueueWithIndex\"");
            webClient.Headers.Add("CONTENT-TYPE", "text/xml; charset=\"utf-8\"");
            try
            {
                byte[] recevieBytes = webClient.UploadData(new Uri("http://" + a31player.IPAddress + ":" + a31player.Port + "/upnp/control/PlayQueue1"), "POST", Encoding.UTF8.GetBytes(playstrings.ToString()));
                var s = System.Text.Encoding.UTF8.GetString(recevieBytes, 0, recevieBytes.Length);
            }
            catch { }
        }
 
 
 
        /// <summary>
        /// 推送播放命令
        /// </summary>
        /// <param name="musicInfo">播放音乐</param>
        /// <param name="listName">列表名称</param>
        /// <param name="musicList">推送列表(默认推50首)</param>
        public  static void PushList(MusicInfo musicInfo, string listName, List<MusicInfo> musicList, A31MusicModel a31player,string musicSource)
        {
 
            //最大发送的条数
            int maxCount = 50;
            //找出当前点击音乐的索引
            int statIndex = musicList.FindIndex((obj) => { return obj == musicInfo; });
            //点击后面还有多少条音乐
            int count = musicList.Count - statIndex;
            int endIndex = 0;
            if (maxCount < count)
            {
                //如果后面的音乐大于最大数,设置后面数据的最大索引 
                endIndex = statIndex + maxCount - 1;
            }
            else
            {
                //如果后面数据不多,就用总数减去最大条数,得到开始索引
                statIndex = musicList.Count - maxCount - 1;
                endIndex = musicList.Count - 1;
            }
            //如果开始索引小于0,那就设置为第一条音乐
            if (statIndex < 0)
            {
                statIndex = 0;
            }
 
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            sb.AppendLine("<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">");
            sb.AppendLine("<s:Body>");
            sb.AppendLine("<u:CreateQueue xmlns:u=\"urn:schemas-wiimu-com:service:PlayQueue:1\">");
            sb.AppendLine("<QueueContext>&lt;?xml version=\"1.0\"?&gt;");
            sb.AppendLine("&lt;PlayList&gt;");
            sb.AppendLine("&lt;ListName&gt;DLNA" + listName + "&lt;/ListName&gt;");
            sb.AppendLine("&lt;ListInfo&gt;");
            sb.AppendLine("&lt;Radio&gt;0&lt;/Radio&gt;");//
            sb.AppendLine("&lt;SourceName&gt;UPnPServer&lt;/SourceName&gt;");//          
            sb.AppendLine("&lt;MarkSearch&gt;0&lt;/MarkSearch&gt;");
            sb.AppendLine("&lt;TrackNumber&gt;" + (endIndex - statIndex + 1) + "&lt;/TrackNumber&gt;");
            sb.AppendLine("&lt;Quality&gt;0&lt;/Quality&gt;");
            sb.AppendLine("&lt;UpdateTime&gt;5367&lt;/UpdateTime&gt;");
            sb.AppendLine("&lt;LastPlayIndex&gt;2&lt;/LastPlayIndex&gt;");
            sb.AppendLine("&lt;SwitchPageMode&gt;0&lt;/SwitchPageMode&gt;");
            sb.AppendLine("&lt;CurrentPage&gt;0&lt;/CurrentPage&gt;");
            sb.AppendLine("&lt;TotalPages&gt;0&lt;/TotalPages&gt;");
            sb.AppendLine("&lt;/ListInfo&gt;");
            sb.AppendLine("&lt;Tracks&gt;");
 
            int number = 1;
 
            for (int i = statIndex, Tracknumber = 1; i <= endIndex; i++, Tracknumber++)
            {
                var tempMusicInfo = musicList[i];
 
                if (musicInfo == tempMusicInfo)
                {
                    number = Tracknumber;
                }
 
                if (tempMusicInfo.SourceType == "Local")
                {
                    tempMusicInfo.URL = "http://" + new Shared.Net.NetWiFi().IpAddress + ":" + com.hdl.on.Server.Port + "/" + tempMusicInfo.ID;
                }
 
                sb.AppendLine("&lt;Track" + Tracknumber + "&gt;");
                sb.AppendLine("&lt;URL&gt;" + tempMusicInfo.URL + "&lt;/URL&gt;");
                sb.AppendLine("&lt;Metadata&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;");
                sb.AppendLine("&amp;lt;DIDL-Lite xmlns:dc=&amp;quot;http://purl.org/dc/elements/1.1/&amp;quot; xmlns:upnp=&amp;quot;urn:schemas-upnp-org:metadata-1-0/upnp/&amp;quot; xmlns:song=&amp;quot;www.wiimu.com/song/&amp;quot; xmlns=&amp;quot;urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/&amp;quot;&amp;gt;");
                sb.AppendLine("&amp;lt;upnp:class&amp;gt;object.item.audioItem.musicTrack&amp;lt;/upnp:class&amp;gt;");
                sb.AppendLine("&amp;lt;item id=&amp;quot;0&amp;quot;&amp;gt;");
                sb.AppendLine("&amp;lt;song:subid&amp;gt;&amp;lt;/song:subid&amp;gt;");
                sb.AppendLine("&amp;lt;song:description&amp;gt;unknown&amp;lt;/song:description&amp;gt;");
                sb.AppendLine("&amp;lt;song:skiplimit&amp;gt;6&amp;lt;/song:skiplimit&amp;gt;");
                sb.AppendLine("&amp;lt;song:id&amp;gt;" + tempMusicInfo.ID + "&amp;lt;/song:id&amp;gt;");
                sb.AppendLine("&amp;lt;song:like&amp;gt;0&amp;lt;/song:like&amp;gt;");
                sb.AppendLine("&amp;lt;song:singerid&amp;gt;0&amp;lt;/song:singerid&amp;gt;");
                sb.AppendLine("&amp;lt;song:albumid&amp;gt;" + tempMusicInfo.AlbumId + "&amp;lt;/song:albumid&amp;gt;");
 
                sb.AppendLine("&amp;lt;res protocolInfo=&amp;quot;http-get:*:audio/mpeg:DLNA.ORG_PN=MP3;DLNA.ORG_OP=01;&amp;quot; duration=&amp;quot;" + "0:0:0.0" + "&amp;quot;&amp;gt;" + tempMusicInfo.URL + "&amp;lt;/res&amp;gt;");
 
                sb.AppendLine("&amp;lt;dc:title&amp;gt;" + tempMusicInfo.Title + "&amp;lt;/dc:title&amp;gt;");
                sb.AppendLine("&amp;lt;dc:creator&amp;gt;DJ Sanny J&amp;lt;/dc:creator&amp;gt;");
                if (musicSource== "我的列表") {
                    sb.AppendLine("&amp;lt;upnp:artist&amp;gt;" + listName + "&amp;lt;/upnp:artist&amp;gt;");
                }
                else {
                    sb.AppendLine("&amp;lt;upnp:artist&amp;gt;" + tempMusicInfo.Artist + "&amp;lt;/upnp:artist&amp;gt;");
                }
                sb.AppendLine("&amp;lt;upnp:album&amp;gt;" + tempMusicInfo.Album + "&amp;lt;/upnp:album&amp;gt;");
                sb.AppendLine("&amp;lt;upnp:albumArtURI&amp;gt;unknown&amp;lt;/upnp:albumArtURI&amp;gt;");
                sb.AppendLine("&amp;lt;/item&amp;gt;");
                sb.AppendLine("&amp;lt;/DIDL-Lite&amp;gt;");
                sb.AppendLine("&lt;/Metadata&gt;");
                sb.AppendLine("&lt;Id&gt;" + tempMusicInfo.ID + "&lt;/Id&gt;");
                sb.AppendLine("&lt;Source&gt;STATION-NETWORK&lt;/Source&gt;");//UPnPServer
                sb.AppendLine("&lt;Key&gt;" + tempMusicInfo.URL + "&lt;/Key&gt;");
                sb.AppendLine("&lt;/Track" + Tracknumber + "&gt;");
            }
            sb.AppendLine("&lt;/Tracks&gt;");
            sb.AppendLine("&lt;/PlayList&gt;");
            sb.AppendLine("</QueueContext>");
            sb.AppendLine("</u:CreateQueue>");
            sb.AppendLine("</s:Body>");
            sb.AppendLine("</s:Envelope>");
 
            SendMusicLists(a31player.IPAddress, a31player.Port, "CreateQueue", sb.ToString());
 
            StringBuilder playString = new StringBuilder();
            playString.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            playString.AppendLine("<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">");
            playString.AppendLine("<s:Body>");
            playString.AppendLine("<u:PlayQueueWithIndex xmlns:u=\"urn:schemas-wiimu-com:service:PlayQueue:1\">");
            playString.AppendLine("<QueueName>DLNA" + listName + "</QueueName>");
            playString.AppendLine("<Index>" + number + "</Index>");
            playString.AppendLine("</u:PlayQueueWithIndex>");
            playString.AppendLine("</s:Body>");
            playString.AppendLine("</s:Envelope>");
 
            Play(a31player.IPAddress, a31player.Port, playString.ToString());
        }
 
       static void SendMusicLists(string ip, int port, string soapAction, string listInfo)
        {
            System.Net.WebClient webClient = new System.Net.WebClient();
            webClient.Headers.Add("SOAPACTION", "\"urn:schemas-wiimu-com:service:PlayQueue:1#" + soapAction + "\"");
            webClient.Headers.Add("CONTENT-TYPE", "text/xml; charset=\"utf-8\"");
            try
            {
                byte[] recevieBytes = webClient.UploadData(new Uri("http://" + ip + ":" + port + "/upnp/control/PlayQueue1"), "POST", System.Text.Encoding.UTF8.GetBytes(listInfo));
                var s = System.Text.Encoding.UTF8.GetString(recevieBytes, 0, recevieBytes.Length);
            }
            catch(Exception e)
            {
                var d = e.Message;
            }
        }
 
       static void Play(string ip, int port, string info)
        {
            System.Net.WebClient webClient = new System.Net.WebClient();
            webClient.Headers.Add("SOAPACTION", "\"urn:schemas-wiimu-com:service:PlayQueue:1#PlayQueueWithIndex\"");
            webClient.Headers.Add("CONTENT-TYPE", "text/xml; charset=\"utf-8\"");
            try
            {
                byte[] recevieBytes = webClient.UploadData(new Uri("http://" + ip + ":" + port + "/upnp/control/PlayQueue1"), "POST", System.Text.Encoding.UTF8.GetBytes(info));
                var s = System.Text.Encoding.UTF8.GetString(recevieBytes, 0, recevieBytes.Length);
            }
            catch
            {
 
            }
        }
 
 
 
        static System.Threading.Thread thread;
        /// <summary>
        /// 修改蓝牙名称
        /// </summary>
        /// <param name="message"></param>
        public static void ModifyBluetoothName(string message)
        {
            if (thread != null)
            {
                thread.Abort();
            }
            thread = new System.Threading.Thread(() =>
            {
                var tcpClient = new TcpClient() { ReceiveTimeout = 1000 };
                try
                {
                    //进行连接
                    tcpClient.Connect(new IPEndPoint(IPAddress.Parse(A31MusicModel.Current.IPAddress), 8899));
                    var buffter = Encoding.UTF8.GetBytes(message);
                    var sendByes = new byte[20 + buffter.Length];
                    sendByes[0] = 0x18;
                    sendByes[1] = 0x96;
                    sendByes[2] = 0x18;
                    sendByes[3] = 0x20;
 
                    sendByes[4] = (byte)((buffter.Length & 0x000000FF) >> 0);
                    sendByes[5] = (byte)((buffter.Length & 0x0000FF00) >> 8);
                    sendByes[6] = (byte)((buffter.Length & 0x00FF0000) >> 16);
                    sendByes[7] = (byte)((buffter.Length & 0xFF000000) >> 24);
 
                    var checkSum = getCheckSum(buffter);
                    sendByes[8] = (byte)((checkSum & 0x000000FF) >> 0);
                    sendByes[9] = (byte)((checkSum & 0x0000FF00) >> 8);
                    sendByes[10] = (byte)((checkSum & 0x00FF0000) >> 16);
                    sendByes[11] = (byte)((checkSum & 0xFF000000) >> 24);
 
                    sendByes[16] = 0x08;
 
                    Array.Copy(buffter, 0, sendByes, 20, buffter.Length);
                    tcpClient.GetStream().Write(sendByes, 0, sendByes.Length);
                    tcpClient.GetStream().Flush();
 
                    thread = null;
                }
                catch { }
                finally
                {
                    tcpClient.Close();
                }
            });
            thread.Start();
        }
        static uint getCheckSum(byte[] buffer)
        {
            uint check_sum = 0;
 
            for (int i = 0; i < buffer.Length; i++)
            {
                check_sum += buffer[i];
            }
 
            return check_sum;
        }
        public class A31QQSong
        {
            public string title = string.Empty;
            public string album = string.Empty;
            public string creator = string.Empty;
        }
 
    }
}