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
using System;
using System.Collections.Generic;
using System.Text;
using Shared;
using Shared.SimpleControl.R;
using Shared.SimpleControl.Phone.Music;
using Shared.SimpleControl.Phone;
using Shared.Net;
using Shared.SimpleControl;
 
namespace SmartHome.UI.SimpleControl.Phone.Music
{
    class A31MusicSoAaAl : FrameLayout
    {
        public void Show(A31MusicModel a31, string Listname, List<MusicInfo> musicList)
        {
            AddChidren(new Button
            {
                Height = Application.GetRealHeight(36),
                BackgroundColor = SkinStyle.Current.MusicTopFrameLayout,
            });
 
            var topFrameLayout = new FrameLayout
            {
                Height = Application.GetRealHeight(100),
                Y = Application.GetRealHeight(36),
                BackgroundColor = SkinStyle.Current.MusicTopFrameLayout,
            };
            AddChidren(topFrameLayout);
 
            var listName = new Button
            {                
                Text= Listname,  
                TextColor=SkinStyle.Current.MusicTextColor,
            };
            topFrameLayout.AddChidren(listName);
 
            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.MouseUpEventHandler+=(sender, e)=>
            {
                this.RemoveFromParent();
            };
 
            var middle = new VerticalScrolViewLayout();
            middle.Y = topFrameLayout.Bottom;
            middle.Height = Application.GetRealHeight (Application.DesignHeight - 136);
            middle.BackgroundColor = SkinStyle.Current.MusicVerticalScrolViewLayout;
            AddChidren(middle);
 
            for (int i = 0; i < musicList.Count; i++) {
                var musicInfo = musicList [i];
 
                var rowmisc = new RowLayout {
                    Height = Application.GetRealHeight (100),
                    LineColor = SkinStyle.Current.MusicRowLayoutLineColor,
                };
                middle.AddChidren (rowmisc);
 
                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",
                };
                rowmisc.AddChidren (title);
 
 
                var addlikelits = new Button {
                    Width = Application.GetRealWidth (72),
                    Height = Application.GetRealHeight (58),
                    X = Application.GetRealWidth (560),
                    UnSelectedImagePath = "MusicIcon/add.png",
                    Gravity = Gravity.CenterVertical,
                };
                rowmisc.AddChidren (addlikelits);
 
                addlikelits.MouseUpEventHandler += (sender1, e1) => {
                    A31MyList A31MyList = new A31MyList ();
                    MainPage.MainFrameLayout.AddChidren (A31MyList);
                    A31MyList.Show (musicInfo);
                };
 
                var btnplay = new Button {
                    Width = Application.GetRealWidth (400),
                    Height = Application.GetRealHeight (50),
                    Text = musicInfo.Title,
                    TextAlignment = TextAlignment.BottomLeft,
                    X = Application.GetRealWidth (100),
                    Tag = musicInfo,
                    TextColor = SkinStyle.Current.MusicTextColor,
                };
                rowmisc.AddChidren (btnplay);
 
 
                var artists = new Button {
                    Width = Application.GetRealWidth (400),
                    Height = Application.GetRealHeight (50),
                    TextAlignment = TextAlignment.TopLeft,
                    X = Application.GetRealWidth (100),
                    Y = btnplay.Bottom,
                    //TextColor = 0x75ffffff,
                    Text = musicInfo.Artist + "-" + musicInfo.Album,
                    Tag = musicInfo,
                    TextColor = SkinStyle.Current.MusicArtistTextColor,
                };
                rowmisc.AddChidren (artists);
 
                EventHandler<MouseEventArgs> musics = (sender, e) => {
                    rowmisc.BackgroundColor = 0xffFE5E00;
                    System.Threading.Tasks.Task.Run (() => {
                        System.Threading.Thread.Sleep (50);
                        Application.RunOnMainThread (() => {
                            this.Parent.RemoveAt (this.Parent.ChildrenCount - 4);
                            this.Parent.RemoveAt (this.Parent.ChildrenCount - 3);
                            this.Parent.RemoveAt (this.Parent.ChildrenCount - 2);
                            RemoveFromParent ();
                            A31PlayMusicPage a31PlayMusicPage = new A31PlayMusicPage ();
                            MainPage.MainFrameLayout.AddChidren (a31PlayMusicPage);
                            a31PlayMusicPage.Show (musicList);
                            //a31PlayMusicPage.Source = "wifi";
                            //a31PlayMusicPage.Source = "SONGLIST-NETWORK";
                            a31.A31PlayStatus.Title = btnplay.Text;
                            System.Threading.Tasks.Task.Run (() => {
 
                                pushList (a31, btnplay.Tag as MusicInfo, Listname);
                            });
 
                        });
                    });
                };
                btnplay.MouseUpEventHandler += musics;
                artists.MouseUpEventHandler += musics;
 
            }   
        }
 
        public void pushList (A31MusicModel a31, MusicInfo musicInfo, string listName)
        {
            //最大发送的条数
            int maxCount =50;
            //找出当前点击音乐的索引
            int statIndex = A31MusicModel.Current.CurrentPlayMusicInfoList.FindIndex ((obj) => { return obj == musicInfo; });
            //点击后面还有多少条音乐
            int count = A31MusicModel.Current.CurrentPlayMusicInfoList.Count - statIndex;
            int endIndex = 0;
            if (maxCount < count) {
                //如果后面的音乐大于最大数,设置后面数据的最大索引 
                endIndex = statIndex + maxCount - 1;
            } else {
                //如果后面数据不多,就用总数减去最大条数,得到开始索引
                statIndex = A31MusicModel.Current.CurrentPlayMusicInfoList.Count - maxCount - 1;
                endIndex = A31MusicModel.Current.CurrentPlayMusicInfoList.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;" + listName + "&lt;/ListName&gt;");
            sb.AppendLine ("&lt;ListInfo&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 playNumber = 1;
            for (int trackNumber = 1, index = statIndex; index <= endIndex; trackNumber++, index++) {
                var tempMusicInfo = a31.CurrentPlayMusicInfoList [index];
                //找出当前音乐播放的序号
                if (musicInfo == tempMusicInfo) {
                    playNumber = trackNumber;
                }
 
                if (tempMusicInfo.SourceType == "Local") {
                    tempMusicInfo.URL = "http://" + new 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;" +index+ "&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;" + tempMusicInfo.Duration + "&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;");
                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;" +index + "&lt;/Id&gt;");
                sb.AppendLine ("&lt;Source&gt;iPhone5s_ttpod_search&lt;/Source&gt;");
                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 (a31.IPAddress, a31.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>" + listName + "</QueueName>");
            playString.AppendLine ("<Index>" + playNumber + "</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 { }
        }
 
    }
}