wxr
2020-06-16 f6fd8acd7c53c44187e70b4709443318a628f4b5
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
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using Shared;
namespace HDL_ON.UI.Music
{
    public class A31LoveRadioList : FrameLayout
    {
        public A31LoveRadioList()
        {
            Tag = "Music";
        }
 
        VerticalRefreshLayout middViewLayout;
        /// <summary>
        /// 先加载界面出来
        /// </summary>
        public void Show()
        {
            #region   界面布局------
 
            this.BackgroundColor = Color.ViewColor;
            var topView = new TopView();
            this.AddChidren(topView.TopFLayoutView());
            topView.topNameBtn.TextID = StringId.radioLove;
            topView.clickBackBtn.MouseUpEventHandler += (sender, e) =>
            {
 
                if (updateSelectedMusicThread != null)
                {
                    try
                    {
                        if (updateSelectedMusicThread.IsAlive)
                        {
                            updateSelectedMusicThread.Abort();
                            ///关闭线程
                        }
                    }
                    catch { }
                }
                this.RemoveFromParent();
            };
            middViewLayout = new VerticalRefreshLayout
            {
                BackgroundColor = Color.WhiteColor,
                Y = topView.fLayout.Bottom,
                Height = Application.GetRealHeight(H_W.H - H_W.T_Height),
            };
            this.AddChidren(middViewLayout);
            middViewLayout.BeginHeaderRefreshingAction += () =>
            {
                middViewLayout.EndHeaderRefreshing();
            };
            #endregion
            MusicView(Language.StringByID(StringId.radioLove));
        }
        /// <summary> 
        /// 加载数据的方法
        /// </summary>
        public void MusicView(string listName)
        {
            middViewLayout.RemoveAll();
            for (int i = 0; i < A31MusicModel.Current.LoveRadioInfoList.Count; i++)
            {
                var radioInfo = A31MusicModel.Current.LoveRadioInfoList[i];
                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 = Color.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/loveSelected.png",
                };
                radioViewFl.AddChidren(loveIcon);
                loveIcon.MouseUpEventHandler += (sender, e) =>
                {
                    var radioselected = A31MusicModel.Current.LoveRadioInfoList.Find((m) => m.URL == radioInfo.URL);
                    if (radioselected != null)
                    {
                        ///存在就删除
                        A31MusicModel.Current.LoveRadioInfoList.Remove(radioInfo);
                    }
                    A31MusicModel.Save();
                    radioViewFl.RemoveFromParent();
                };
 
                //点击控件
                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(() =>
                            {
                                CnpushList(clickBtn.Tag as MusicInfo, listName);
                            });
                        });
                    });
 
                };
 
            }
        }
        /// <summary>
        /// 定时更新当前播放音乐
        /// </summary>
        System.Threading.Thread updateSelectedMusicThread;
        public void UpdateSelectedMusic()
        {
            updateSelectedMusicThread = new System.Threading.Thread(() =>
            {
                while (this.Parent != null)
                {
                    System.Threading.Thread.Sleep(1000);
                    Application.RunOnMainThread(() =>
                    {
                        try
                        {
                            for (int i = 0; i < middViewLayout.ChildrenCount; i++)
                            {
                                FrameLayout view = (FrameLayout)middViewLayout.GetChildren(i);
                                var songNameBtn = (Button)view.GetChildren(1);//直接FrameLayout父控件找到该控件Button
                                if (A31MusicModel.Current.A31PlayStatus.Title == songNameBtn.Text.ToString())
                                {
                                    songNameBtn.TextColor = Color.SelectedColor;
                                }
                                else
                                {
                                    songNameBtn.TextColor = Color.MusicTxet14Color;
                                }
 
 
                            }
                        }
                        catch { }
 
                    });
                }
            });
            updateSelectedMusicThread.Start();
        }
 
        /// <summary>
        ///收藏列表电台播放
        /// </summary>
        /// <param name="listName"></param>
        public void CnpushList(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 = 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
            {
 
            }
        }
    }
}