JLChen
2020-06-04 6d55af8792cf8fbef0055e677b900fc352dba9a2
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
using System;
using System.Collections.Generic;
using System.Text;
using Shared;
using Shared.SimpleControl;
using Shared.SimpleControl.Phone;
using Shared.SimpleControl.Phone.Music;
using Shared.SimpleControl.R;
 
 
namespace SmartHome.UI.SimpleControl.Phone.Music
{
    class A31RadioList : FrameLayout
    {
        /// <summary>
        /// 判断是否可以直接播放
        /// </summary>
        /// <param name="ob"></param>
        /// <returns></returns>
        bool isRadios (object [] ob)
        {
            bool result = false;
            foreach (object o in ob) {
                if (o.ToString () == "radio") {
                    result = true;
                    break;
                } else if (o.ToString () == "feed.episode") {
                    result = true;
                    break;
                } else if (o.ToString () == "track") {
                    result = true;
                    break;
                }
            }
            return result;
        }
 
        public void show (A31MusicModel a31, Airable airable, string currname)
        {
            AddChidren (new Button {
                Height = Application.GetRealHeight (36),
                BackgroundColor = SkinStyle.Current.MainColor,
            });
 
            var topFrameLayout = new FrameLayout {
                Height = Application.GetRealHeight (90),
                Y = Application.GetRealHeight (36),
                BackgroundColor = SkinStyle.Current.MainColor,
            };
            AddChidren (topFrameLayout);
 
            var LocallistName = new Button {
                Text = currname,
            };
            topFrameLayout.AddChidren (LocallistName);
 
            var hdl = new Button {
                Width = Application.GetRealWidth (154),
                Height = Application.GetRealHeight (90),
                X = Application.GetRealWidth (486),
                Gravity = Gravity.CenterVertical,
                UnSelectedImagePath = MainPage.LogoString,
            };
            topFrameLayout.AddChidren (hdl);
 
            var back = new Button {
                Width = Application.GetRealWidth (82),
                Height = Application.GetRealHeight (89),
                X = Application.GetRealWidth (10),
                Gravity = Gravity.CenterVertical,
                UnSelectedImagePath = "MusicIcon/HomepageBack.png",
            };
            topFrameLayout.AddChidren (back);
            back.MouseDownEventHandler += (sender, e) => {
                RemoveFromParent ();
            };
 
            var middle = new VerticalScrolViewLayout ();
            middle.Y = topFrameLayout.Bottom;
            middle.Height = Application.GetRealHeight (Application.DesignHeight - 126);
            middle.BackgroundColor = 0xff2F2F2F;
            AddChidren (middle);
 
            if (airable == null) {
                return;
            }
            if (airable.content == null) {
                return;
            }
            if (airable.content.entries == null) {
                return;
            }
            int munber = 0;
            a31.CnRadioInfoList = new List<MusicInfo> ();
            foreach (var entrie in airable.content.entries) {
                if (isRadios (entrie.id)) {
                    a31.CnRadioInfoList.Add (new MusicInfo { URL = entrie.url, Title = entrie.title });
                    var rowsong = new RowLayout {
                        Height = Application.GetRealHeight (100),
                    };
                    middle.AddChidren (rowsong);
 
                    var title = new Button {
                        Width = Application.GetRealWidth (61),
                        Height = Application.GetRealHeight (61),
                        X = Application.GetRealWidth (20),
                        Gravity = Gravity.CenterVertical,
                        Radius = (uint)Application.GetRealHeight (4),
                        UnSelectedImagePath = "MusicIcon/musicplay1.png",
                    };
                    rowsong.AddChidren (title);
 
                    var radioname = new Button {
                        Width = Application.GetRealWidth (400),
                        Text = entrie.title,
                        TextAlignment = TextAlignment.CenterLeft,
                        X = Application.GetRealWidth (130),
                        Tag = munber++,
                    };
                    rowsong.AddChidren (radioname);
                    radioname.MouseUpEventHandler += (sender, e) => {
                        rowsong.BackgroundColor = 0xffFE5E00;
                        System.Threading.Tasks.Task.Run (() => {
                            System.Threading.Thread.Sleep (10);
                            Application.RunOnMainThread (() => {
                                for (int i = MainPage.MainFrameLayout.ChildrenCount - 1; 0 <= i; i--) {
                                    var view = MainPage.MainFrameLayout.GetChildren (i);
                                    if (view.GetType () == typeof (A31RadioList)) {
                                        view.RemoveFromParent ();
                                    } else {
                                        break;
                                    }
                                }
                                MainPage.MainFrameLayout.RemoveAt (MainPage.MainFrameLayout.ChildrenCount - 1);
                                MainPage.MainFrameLayout.RemoveAt (MainPage.MainFrameLayout.ChildrenCount - 1);
                                MainPage.MainFrameLayout.RemoveAt (MainPage.MainFrameLayout.ChildrenCount - 1);
 
                                A31PlayMusicPage a31playmusic = new A31PlayMusicPage ();
                                MainPage.MainFrameLayout.AddChidren (a31playmusic);
                                a31playmusic.Show (new List<MusicInfo> ());
                                //a31playmusic.Source = "RADIO-NETWORK";
                                a31.A31PlayStatus.Title = radioname.Text;
                                System.Threading.Tasks.Task.Run (() => {
                                    pushList (a31, radioname.Tag, currname);
                                });
                            });
                        });
                    };
                } else {
                    //目录列表
                    if (entrie.title != "搜索" || entrie.title != "Suche") {
                        var rowtrue = new RowLayout {
                            Height = Application.GetRealHeight (100)
                        };
                        middle.AddChidren (rowtrue);
 
                        var radius = new Button {
                            Width = Application.GetRealWidth (36),
                            Height = Application.GetRealHeight (36),
                            TextAlignment = TextAlignment.CenterLeft,
                            X = Application.GetRealWidth (20),
                            UnSelectedImagePath = "MusicIcon/radius.png",
                            Gravity = Gravity.CenterVertical,
                        };
                        rowtrue.AddChidren (radius);
 
                        var radioname = new Button {
                            Width = Application.GetRealWidth (440),
                            Gravity = Gravity.CenterVertical,
                            Text = entrie.title,
                            X = Application.GetRealWidth (80),
                            TextAlignment = TextAlignment.CenterLeft,
                            Tag = entrie.url,
                        };
                        rowtrue.AddChidren (radioname);
 
                        var radionext = new Button {
                            Width = Application.GetRealWidth (87),
                            Height = Application.GetRealHeight (100),
                            UnSelectedImagePath = "MusicIcon/Next.png",
                            SelectedImagePath = "MusicIcon/NextSelecte.png",
                            X = Application.GetRealWidth (550),
                        };
                        rowtrue.AddChidren (radionext);
 
                        EventHandler<MouseEventArgs> useshare = (sender, e) => {
                            var radioConfig = Newtonsoft.Json.JsonConvert.DeserializeObject<RadioConfig> (System.Text.Encoding.UTF8.GetString (Shared.IO.FileUtils.ReadFile (RadioConfig.FileName)));
                            MainPage.Loading.Start (Language.StringByID (MyInternationalizationString.load));
                            System.Threading.Tasks.Task.Run (() => {
                                Airable airablelist = null;
                                try {
                                    airablelist = A31CommonLogin.ReadRadiohome (radioConfig.UserName, radioConfig.Passwrod, radioname.Tag.ToString ());
                                    if (airablelist == null) {
                                        airablelist = A31CommonLogin.ReadRadiohome (radioConfig.UserName, radioConfig.Passwrod, radioname.Tag.ToString ());
                                    }
                                } catch { } finally {
                                    Application.RunOnMainThread (() => {
                                        MainPage.Loading.Hide ();
                                        A31RadioList a31RadioList = new A31RadioList ();
                                        MainPage.MainFrameLayout.AddChidren (a31RadioList);
                                        a31RadioList.show (a31, airablelist, radioname.Text);
                                    });
                                }
                            });
                        };
                        radius.MouseUpEventHandler += useshare;
                        radioname.MouseUpEventHandler += useshare;
                        radionext.MouseUpEventHandler += useshare;
                    }
 
                    #region        .......搜索
                    ////.......搜索
                    //else
                    //{
                    //    var searchrowtrue = new RowLayout
                    //    {
                    //        Height = Application.GetRealHeight(100)
                    //    };
                    //    middle.AddChidren(searchrowtrue);
 
                    //    var radius = new Button
                    //    {
                    //        Width = Application.GetRealWidth(36),
                    //        Height = Application.GetRealHeight(36),
                    //        TextAlignment = TextAlignment.CenterLeft,
                    //        X = Application.GetRealWidth(20),
                    //        UnSelectedImagePath = "MusicIcon/radius.png",
                    //        Gravity = Gravity.CenterVertical,
                    //    };
                    //    searchrowtrue.AddChidren(radius);
 
                    //    var search = new Button
                    //    {
                    //        Gravity = Gravity.CenterVertical,
                    //        Text = entrie.title,
                    //        X = Application.GetRealWidth(80),
                    //        TextAlignment = TextAlignment.CenterLeft,
                    //        Tag = entrie.url,
                    //    };
                    //    searchrowtrue.AddChidren(search);
 
                    //    var searchnext = new Button
                    //    {
                    //        Width = Application.GetRealWidth(87),
                    //        Height = Application.GetRealHeight(100),
                    //        UnSelectedImagePath = "MusicIcon/Next.png",
                    //        SelectedImagePath = "MusicIcon/NextSelecte.png",
                    //        X = Application.GetRealWidth(550),
                    //    };
                    //    searchrowtrue.AddChidren(searchnext);
 
                    //    EventHandler<MouseEventArgs> searchs = (sender, e) =>
                    //      {
                    //          A31Search A31search = new A31Search();
                    //          MainPage.MainFrameLayout.AddChidren(A31search);
                    //          A31search.Show(a31);
                    //      };
 
                    //    radius.MouseUpEventHandler += searchs;
                    //    search.MouseUpEventHandler += searchs;
                    //    searchnext.MouseUpEventHandler += searchs;
                    //}
                    #endregion
                }
            }
        }
 
 
        public static string GetPlayUrl (string url)
        {
            System.DateTime dateTime = DateTime.Now;
 
            var radioConfig = Newtonsoft.Json.JsonConvert.DeserializeObject<RadioConfig> (System.Text.Encoding.UTF8.GetString (Shared.IO.FileUtils.ReadFile (RadioConfig.FileName)));
            var airable = A31CommonLogin.ReadRadiohome (radioConfig.UserName, radioConfig.Passwrod, url);
 
            if (airable != null && airable.streams != null) {
                airable = A31CommonLogin.ReadRadiohome (radioConfig.UserName, radioConfig.Passwrod, airable.streams [0].url);
                if (airable == null) {
                    airable = A31CommonLogin.ReadRadiohome (radioConfig.UserName, radioConfig.Passwrod, airable.streams [0].url);
                }
            }
            if (airable == null) {
                return "";
            } else {
                return airable.url;
            }
        }
 
        public void pushList (A31MusicModel a31, object tag, 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;SourceName&gt;&lt;/SourceName&gt;");
            sb.AppendLine ("&lt;TrackNumber&gt;1&lt;/TrackNumber&gt;");
            sb.AppendLine ("&lt;SearchUrl&gt;searchurl&lt;/SearchUrl&gt;");
            sb.AppendLine ("&lt;/ListInfo&gt;");
            sb.AppendLine ("&lt;Tracks&gt;");
 
            var tempMusicInfo = a31.CnRadioInfoList [int.Parse (tag.ToString ())];
            string url = GetPlayUrl (tempMusicInfo.URL);
 
            sb.AppendLine ("&lt;Track" + 1 + "&gt;");
            sb.AppendLine ("&lt;URL&gt;" + url + "&lt;/URL&gt;");
            sb.AppendLine ("&lt;Source&gt;&lt;/Source&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;" + tempMusicInfo.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;" + tempMusicInfo.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 (a31.IPAddress, a31.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 (a31.IPAddress, a31.Port, playString.ToString ());
        }
 
        void sendMusicLists (string ip, int port, string soapAction, string listInfo)
        {
            Shared.Net.MyWebClient webClient = new Shared.Net.MyWebClient (5000);
            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)
        {
            Shared.Net.MyWebClient webClient = new Shared.Net.MyWebClient (5000);
            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 { }
        }
    }
}