wxr
2020-08-11 2bec9c838d2d688025698de8ec1de401ffd7dd1f
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
using System;
using System.Net;
using System.Text;
using System.Xml;
using Shared;
namespace HDL_ON.UI.Music
{
    public class A31RadioList : FrameLayout
    {
        public A31RadioList()
        {
            Tag = "Music";
        }
 
        VerticalRefreshLayout middViewLayout;
        /// <summary>
        /// 先加载界面出来
        /// </summary>
        public void Show(string listName)
        {
           
            #region   界面布局------
            this.BackgroundColor = MusicColor.ViewColor;
            var topView = new TopView();
            this.AddChidren(topView.TopFLayoutView());
            topView.topNameBtn.Text= listName;
            topView.clickBackBtn.MouseUpEventHandler += (sender, e) =>
            {
                this.RemoveFromParent();
            };
            middViewLayout = new VerticalRefreshLayout
            {
                BackgroundColor = MusicColor.WhiteColor,
                Y = topView.fLayout.Bottom,
                Height = Application.GetRealHeight(H_W.H - H_W.T_Height),
            };
            this.AddChidren(middViewLayout);
            middViewLayout.BeginHeaderRefreshingAction += () =>
            {
                middViewLayout.EndHeaderRefreshing();
            };
            #endregion
            MusicView(listName);
        }
        /// <summary> 
        /// 加载数据的方法
        /// </summary>
        public void MusicView(string listName)
        {
            middViewLayout.RemoveAll();
            for (int i = 0; i < A31MusicModel.Current.CnRadioInfoList.Count; i++)
            {
                var radioInfo = A31MusicModel.Current.CnRadioInfoList[i];
                #region   Audio  (播放列表) 
                if (radioInfo.Cntype == "audio")
                {
 
                    if (string.IsNullOrEmpty(radioInfo.Title))
                    {
                        //防止歌曲名字为空抛异常
                        radioInfo.Title = " ";
                    }
                    FrameLayout radioViewFl = new FrameLayout
                    {
                        Width = Application.GetRealWidth(375),
                        Height = Application.GetRealHeight(67),
                    };
                    middViewLayout.AddChidren(radioViewFl);
                    //默认电台图标
                    Button radioIconBtn = new Button
                    {
                        X = Application.GetRealWidth(16),
                        Y = Application.GetRealHeight(18),
                        Width = Application.GetMinRealAverage(32),
                        Height = Application.GetMinRealAverage(32),
                        UnSelectedImagePath = "MusicIcon/radioIcon.png",
                    };
                    radioViewFl.AddChidren(radioIconBtn);
                    //电台名称
                    Button radioNameBtn = new Button
                    {
                        X = Application.GetRealWidth(60),
                        Y = Application.GetRealHeight(23),
                        Width = Application.GetRealWidth(250),
                        Height = Application.GetRealHeight(22),
                        TextColor = MusicColor.MusicTxet14Color,
                        TextSize = TextSize.Text16,
                        TextAlignment = TextAlignment.CenterLeft,
                        Tag = radioInfo.URL,
                        Text = radioInfo.Title.Trim()
                    };
                    radioViewFl.AddChidren(radioNameBtn);
 
                    //添加喜爱电台
                    Button loveIcon = new Button
                    {
                        X = Application.GetRealWidth(335),
                        Y = Application.GetRealHeight(20),
                        Width = Application.GetMinRealAverage(28),
                        Height = Application.GetMinRealAverage(28),
                        UnSelectedImagePath = "MusicIcon/love.png",
                        SelectedImagePath = "MusicIcon/loveSelected.png",
                    };
                    radioViewFl.AddChidren(loveIcon);
                    loveIcon.MouseUpEventHandler += (sender, e) =>
                    {
                        loveIcon.IsSelected = !loveIcon.IsSelected;
                        if (loveIcon.IsSelected)
                        {
                            ///查找电台是否存在在列表
                            var radioselected= A31MusicModel.Current.LoveRadioInfoList.Find((m) => m.URL == radioInfo.URL);
                            if (radioselected == null)
                            {
                                ///没有存在就添加
                                A31MusicModel.Current.LoveRadioInfoList.Add(radioInfo);
                            }
                        }
                        else
                        {
                            ///查找音电台是否存在在列表
                            var radioselected = A31MusicModel.Current.LoveRadioInfoList.Find((m) => m.URL == radioInfo.URL);
                            if (radioselected != null)
                            {
                                ///存在就删除
                                A31MusicModel.Current.LoveRadioInfoList.Remove(radioInfo);
                            }
 
                        }
                        A31MusicModel.Save();
                    };
                    var v = A31MusicModel.Current.LoveRadioInfoList.Find((m) => m.URL == radioInfo.URL);
                    if (v != null)
                    {
                        loveIcon.IsSelected = true;
                    }
                    else
                    {
                        loveIcon.IsSelected = false;
                    }
 
                    //点击控件
                    Button clickBtn = new Button
                    {
                        Width = Application.GetRealWidth(280),
                        Height = Application.GetRealHeight(44),
                        Tag = radioInfo,//标记播放哪一首歌曲
                    };
                    radioViewFl.AddChidren(clickBtn);
                    ///点击播放事件
                    clickBtn.MouseUpEventHandler += (sender, e) =>
                    {
 
                        System.Threading.Tasks.Task.Run(() =>
                        {
                            System.Threading.Thread.Sleep(50);
                            Application.RunOnMainThread(() =>
                            {
                                //移除界面
                                A31MusicModel.Current.A31PlayStatus.Title = radioNameBtn.Text;
                                MainPage.BasePageView.RemoveViewByTag("Music");
                                System.Threading.Tasks.Task.Run(() =>
                                {
                                    PushList(clickBtn.Tag as MusicInfo, listName);
                                });
                            });
                        });
 
                    };
 
                }
                #endregion
 
                #region  Link(列表)
                if (radioInfo.Cntype == "link")
                {
                    RowLayout addFlieRow = new RowLayout
                    {
                        Height = Application.GetRealHeight(78),
                        LineColor = MusicColor.WhiteColor,
                        SubViewWidth = Application.GetRealWidth(90),//改变编辑控件宽度多少;
                    };
                    middViewLayout.AddChidren(addFlieRow);
                    //文件图标
                    Button fileIconBtn = new Button
                    {
                        X = Application.GetRealWidth(16),
                        Y = Application.GetRealHeight(8),
                        Width = Application.GetMinRealAverage(62),
                        Height = Application.GetMinRealAverage(62),
                        UnSelectedImagePath = "MusicIcon/file.png",
                    };
                    addFlieRow.AddChidren(fileIconBtn);
                    //文件名控件
                    Button fileNameBtn = new Button
                    {
                        X = fileIconBtn.Right + Application.GetRealWidth(16),
                        Y = Application.GetRealHeight(29),
                        Width = Application.GetRealWidth(220),
                        Height = Application.GetRealHeight(20),
                        TextColor = MusicColor.TextColor,
                        TextSize = TextSize.Text14,
                        TextAlignment = TextAlignment.CenterLeft,
                        Text = radioInfo.Title,
                    };
                    addFlieRow.AddChidren(fileNameBtn);
 
                    Button clickBtn = new Button
                    {
                        X = fileIconBtn.Right + Application.GetRealWidth(16),
                        Width = Application.GetRealWidth(375 - 94),
                        Height = Application.GetRealHeight(78),
                        Tag = radioInfo.URL,
                    };
                    addFlieRow.AddChidren(clickBtn);
                    clickBtn.MouseUpEventHandler += (sender, e) =>
                    {
                        Loading loading = new Loading();
                        this.AddChidren(loading);
                        loading.Start();
                        System.Threading.Tasks.Task.Run(() =>
                        {
                            try
                            {
                                var redString = SendMethod.ReadRadioList(clickBtn.Tag.ToString());
                                if (redString == null)
                                {
                                    return;
                                }
                                int startIndex2 = redString.IndexOf("<body>");
                                int endIndex = redString.IndexOf("</body>") + "</body>".Length;
                                if (endIndex <= startIndex2)
                                {
                                    return;
                                }
                                var ss = redString.Substring(startIndex2, endIndex - startIndex2);
                                XmlDocument xml = new XmlDocument();
                                xml.LoadXml(ss);
                                var listString = xml.SelectSingleNode("body");
                                if (listString == null)
                                {
                                    return;
                                }
                                A31MusicModel.Current.CnRadioInfoList.Clear();
                                foreach (XmlNode childNodes in listString.ChildNodes)
                                {
                                    if (childNodes.Name == "outline" && childNodes.Attributes["URL"] != null)
                                    {
                                        MusicInfo musicInfoCN = new MusicInfo();
                                        musicInfoCN.URL = childNodes.Attributes["URL"] == null ? "" : childNodes.Attributes["URL"].Value;
                                        musicInfoCN.Title = childNodes.Attributes["text"] == null ? "" : childNodes.Attributes["text"].Value;
                                        //musicInfoCN.Image = childNodes.Attributes["image"] == null ? "" : childNodes.Attributes["image"].Value;
 
                                        if (childNodes.Attributes["image"] != null)
                                        {
                                            #region 初始化网络图片路径
                                            string path = "";
                                            if (Shared.Application.IsPad)
                                            {
                                                path = "Pad/" + "MusicImage/";
                                            }
                                            else
                                            {
                                                path = "Phone/" + "MusicImage/";
                                            }
                                            path = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, path + childNodes.Attributes["image"].Value.Replace('/', '_').Replace('\\', '_').Replace(':', '_').Replace('*', '_').Replace('?', '_').Replace('"', '_').Replace('<', '_').Replace('>', '_').Replace('|', '_'));
                                            musicInfoCN.Image = path;
                                            #endregion
                                            //Shared.IO.FileUtils.DownLoadImage(musicInfoCN.Image, childNodes.Attributes["image"].Value);
                                        }
 
                                        musicInfoCN.Cntype = childNodes.Attributes["type"] == null ? "" : childNodes.Attributes["type"].Value;
                                        A31MusicModel.Current.CnRadioInfoList.Add(musicInfoCN);
                                    }
                                    foreach (XmlNode a in childNodes.ChildNodes)
                                    {
                                        if (a.Name == "outline")
                                        {
                                            MusicInfo musicInfoCN = new MusicInfo();
                                            musicInfoCN.URL = a.Attributes["URL"] == null ? "" : a.Attributes["URL"].Value;
                                            musicInfoCN.Title = a.Attributes["text"] == null ? "" : a.Attributes["text"].Value;
                                            if (a.Attributes["image"] != null)
                                            {
                                                #region 初始化网络图片路径
                                                string path = "";
                                                if (Shared.Application.IsPad)
                                                {
                                                    path = "Pad/" + "MusicImage/";
                                                }
                                                else
                                                {
                                                    path = "Phone/" + "MusicImage/";
                                                }
                                                path = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, path + a.Attributes["image"].Value.Replace('/', '_').Replace('\\', '_').Replace(':', '_').Replace('*', '_').Replace('?', '_').Replace('"', '_').Replace('<', '_').Replace('>', '_').Replace('|', '_'));
                                                musicInfoCN.Image = path;
                                                #endregion
                                                // Shared.IO.FileUtils.DownLoadImage(musicInfoCN.Image, a.Attributes["image"].Value);
                                            }
                                            musicInfoCN.Cntype = a.Attributes["type"] == null ? "" : a.Attributes["type"].Value;
                                            A31MusicModel.Current.CnRadioInfoList.Add(musicInfoCN);
                                        }
                                    }
                                }
                            }
                            catch { }
                            finally
                            {
                                Application.RunOnMainThread(() =>
                                {
                                    loading.Hide();
                                    A31RadioList a31RadioList = new A31RadioList();
                                    MainPage.BasePageView.AddChidren(a31RadioList);
                                    MainPage.BasePageView.PageIndex = MainPage.BasePageView.ChildrenCount - 1;
                                    a31RadioList.Show(fileNameBtn.Text);
                                });
                            }
                        });
                    };
                }
                #endregion
 
            }
        }
 
        public void PushList(MusicInfo musicInfo, string listName)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>");
            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>");
            sb.AppendLine("&lt;?xml version=&quot;1.0&quot; ?&gt;");
            sb.AppendLine("&lt;PlayList&gt;");
            sb.AppendLine("&lt;ListName&gt;" + listName + "&lt;/ListName&gt;");
            sb.AppendLine("&lt;ListInfo&gt;");
            sb.AppendLine("&lt;Radio&gt;1&lt;/Radio&gt;");
            sb.AppendLine("&lt;SourceName&gt;TuneIn&lt;/SourceName&gt;");
            sb.AppendLine("&lt;TrackNumber&gt;1&lt;/TrackNumber&gt;");
            sb.AppendLine("&lt;SearchUrl&gt;http://opml.radiotime.com/Tune.ashx?id=s112201&amp;amp;partnerId=yvcOjvJP&lt;/SearchUrl&gt;");
            sb.AppendLine("&lt;Quality&gt;3&lt;/Quality&gt;");
            sb.AppendLine("&lt;RealIndex&gt;0&lt;/RealIndex&gt;");
            sb.AppendLine("&lt;SrcParent&gt;&lt;/SrcParent&gt;");
            sb.AppendLine("&lt;PicUrl&gt;&lt;/PicUrl&gt;");
            sb.AppendLine("&lt;/ListInfo&gt;");
            sb.AppendLine("&lt;Tracks&gt;");
 
 
            var url =SendMethod.ReadRadioList(musicInfo.URL);
 
            if (url != null)
            {
                url = url.TrimEnd('\n').Replace("&", "&amp;amp;");
            }
            else
            {
                return;
            }
 
            sb.AppendLine("&lt;Track" + 1 + "&gt;");
            sb.AppendLine("&lt;Source&gt;TuneIn&lt;/Source&gt;");
            sb.AppendLine("&lt;URL&gt;" + url + "&lt;/URL&gt;");
            sb.AppendLine("&lt;Source&gt;TuneIn&lt;/Source&gt;");
            sb.AppendLine("&lt;Id&gt;0&lt;/Id&gt;");
            sb.AppendLine("&lt;Key&gt;http://so.ard.iyyin.com/s/song_with_out?q=%E8%8A%B1%E9%83%BD%E7%94%B5%E5%8F%B0+100.5+%28%E6%B5%81%E8%A1%8C%E9%9F%B3%E4%B9%90%E5%89%8D40%29%20TuneIn&amp;amp;size=50&amp;amp;page=1&lt;/Key&gt;");
            sb.AppendLine("&lt;Metadata&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&amp;gt;");
            sb.AppendLine("&amp;lt;song:bitrate&amp;gt;0&amp;lt;/song:bitrate&amp;gt;");
            sb.AppendLine("&amp;lt;song:id&amp;gt;0&amp;lt;/song:id&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;0&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;1000&amp;quot;&amp;gt;" + url + "&amp;lt;/res&amp;gt;");
            sb.AppendLine("&amp;lt;dc:title&amp;gt;" + musicInfo.Title + "&amp;lt;/dc:title&amp;gt;");
            sb.AppendLine("&amp;lt;upnp:artist&amp;gt;TuneIn&amp;lt;/upnp:artist&amp;gt;");
            sb.AppendLine("&amp;lt;upnp:album&amp;gt;&amp;lt;/upnp:album&amp;gt;");
            sb.AppendLine("&amp;lt;upnp:albumArtURI&amp;gt;" + musicInfo.Image + "&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;/Track" + 1 + "&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(a31.IPAddress, a31.Port, "CreateQueue", sb.ToString());
            SendMusicLists(A31MusicModel.Current.IPAddress, A31MusicModel.Current.Port, "CreateQueue", sb.ToString());
 
            StringBuilder playString = new StringBuilder();
            playString.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>");
            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>" + listName + "</QueueName>");
            playString.AppendLine("<Index>" + 1 + "</Index>");
            playString.AppendLine("</u:PlayQueueWithIndex>");
            playString.AppendLine("</s:Body>");
            playString.AppendLine("</s:Envelope>");
            Play(A31MusicModel.Current.IPAddress, A31MusicModel.Current.Port, playString.ToString());
 
        }
 
        void SendMusicLists(string ip, int port, string soapAction, string listInfo)
        {
            WebClient webClient = new 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 { }
        }
 
        void Play(string ip, int port, string info)
        {
            WebClient webClient = new 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 { }
        }
    }
}