using System; using System.Collections.Generic; using System.Security; using System.Text; using Shared; using Shared.SimpleControl; using Shared.SimpleControl.Phone.Music; using Shared.SimpleControl.R; namespace SmartHome { public class A31vTuner : FrameLayout { public void Show (A31MusicModel a31, string listname) { 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 RadiolistName = new Button { Text = listname, }; topFrameLayout.AddChidren (RadiolistName); 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 () { Y = topFrameLayout.Bottom, Height = Application.GetRealHeight (Application.DesignHeight - 130), BackgroundColor = 0xff2F2F2F }; AddChidren (middle); for (int i = 0; i < a31.vTunerList.Count; i++) { #region Station if (a31.vTunerList [i].Cntype == "Station") { var cnrow = new RowLayout { Height = Application.GetRealHeight (100) }; middle.AddChidren (cnrow); 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", }; cnrow.AddChidren (title); var vtuner = new Button { Gravity = Gravity.CenterVertical, Text = a31.vTunerList [i].Title, X = Application.GetRealWidth (130), Tag = a31.vTunerList [i], TextAlignment = TextAlignment.CenterLeft, }; cnrow.AddChidren (vtuner); EventHandler vt = (sender, e) => { cnrow.BackgroundColor = 0xffFE5E00; System.Threading.Tasks.Task.Run (() => { System.Threading.Thread.Sleep (50); Application.RunOnMainThread (() => { for (int a = MainPage.MainFrameLayout.ChildrenCount - 1; 0 <= a; a--) { var view = MainPage.MainFrameLayout.GetChildren (a); if (view.GetType () == typeof (A31vTuner)) { view.RemoveFromParent (); } else { break; } } MainPage.MainFrameLayout.RemoveAt (MainPage.MainFrameLayout.ChildrenCount - 1); MainPage.MainFrameLayout.RemoveAt (MainPage.MainFrameLayout.ChildrenCount - 1); A31PlayMusicPage a31PlayMusicPage = new A31PlayMusicPage (); MainPage.MainFrameLayout.AddChidren (a31PlayMusicPage); a31PlayMusicPage.Show (new List ()); }); a31.A31PlayStatus.Title = vtuner.Text; System.Threading.Tasks.Task.Run (() => { pushList (a31, vtuner.Tag as MusicInfo, vtuner.Text); //MyMusic.dateTime = System.DateTime.Now.AddSeconds (-5); }); }); }; title.MouseUpEventHandler += vt; vtuner.MouseUpEventHandler += vt; } #endregion #region Dir if (a31.vTunerList [i].Cntype == "Dir") { var cnrow = new RowLayout { Height = Application.GetRealHeight (100) }; middle.AddChidren (cnrow); 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, }; cnrow.AddChidren (radius); var cntext = new Button { Gravity = Gravity.CenterVertical, Text = a31.vTunerList [i].Title, X = Application.GetRealWidth (80), Tag = a31.vTunerList [i].URL, TextAlignment = TextAlignment.CenterLeft, }; cnrow.AddChidren (cntext); var next = new Button { Width = Application.GetRealWidth (87), Height = Application.GetRealHeight (100), UnSelectedImagePath = "MusicIcon/Next.png", SelectedImagePath = "MusicIcon/NextSelecte.png", X = Application.GetRealWidth (550), }; cnrow.AddChidren (next); EventHandler tunerresources = (sender, e) => { MainPage.Loading.Start (Language.StringByID (MyInternationalizationString.load)); a31.vTunerList.Clear (); System.Threading.Tasks.Task.Run (() => { try { var vTunerdtat = loginvTunerList (cntext.Tag.ToString()+"&mac=a45f7477eb353cc7108b0b25da30fca3"); var se = SecurityElement.FromString (vTunerdtat); foreach (SecurityElement item in se.Children) { if (item.Tag == "Item") { var itemType = item.SearchForTextOfTag ("ItemType"); if (itemType == "Station") { a31.vTunerList.Add (new MusicInfo { Title = item.SearchForTextOfTag ("StationName"), URL = item.SearchForTextOfTag ("StationUrl"), Cntype = itemType, }); } else if (itemType == "Dir") { a31.vTunerList.Add (new MusicInfo { Title = item.SearchForTextOfTag ("Title") == null ? item.SearchForTextOfTag ("IconTitle") : item.SearchForTextOfTag ("Title"), URL = item.SearchForTextOfTag ("UrlDir"), Cntype = itemType, }); } } } } catch { } finally { Application.RunOnMainThread (() => { MainPage.Loading.Hide (); A31vTuner a31vtuner = new A31vTuner (); MainPage.MainFrameLayout.AddChidren (a31vtuner); a31vtuner.Show (a31, cntext.Text); }); } }); }; radius.MouseUpEventHandler += tunerresources; next.MouseUpEventHandler += tunerresources; cntext.MouseUpEventHandler += tunerresources; } #endregion } } public void pushList (A31MusicModel a31, MusicInfo musicInfo, string listName) { StringBuilder sb = new StringBuilder (); sb.AppendLine (""); sb.AppendLine (""); sb.AppendLine (""); sb.AppendLine (""); sb.AppendLine (""); sb.AppendLine ("<?xml version="1.0" ?>"); sb.AppendLine ("<PlayList>"); sb.AppendLine ("<ListName>" + listName + "</ListName>"); sb.AppendLine ("<ListInfo>"); sb.AppendLine ("<Radio>1</Radio>"); sb.AppendLine ("<SourceName>TuneIn</SourceName>"); sb.AppendLine ("<TrackNumber>1</TrackNumber>"); sb.AppendLine ("<SearchUrl></SearchUrl>"); sb.AppendLine ("<Quality>3</Quality>"); sb.AppendLine ("<RealIndex>0</RealIndex>"); sb.AppendLine ("<SrcParent></SrcParent>"); sb.AppendLine ("<PicUrl></PicUrl>"); sb.AppendLine ("</ListInfo>"); sb.AppendLine ("<Tracks>"); var url = musicInfo.URL; if (url != null) { url = url.TrimEnd ('\n').Replace ("&", "&amp;"); } else { return; } sb.AppendLine ("<Track" + 1 + ">"); sb.AppendLine ("<Source>TuneIn</Source>"); sb.AppendLine ("<URL>" + url + "</URL>"); sb.AppendLine ("<Source>TuneIn</Source>"); sb.AppendLine ("<Id>0</Id>"); sb.AppendLine ("<Key>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;size=50&amp;page=1</Key>"); sb.AppendLine ("<Metadata>"); sb.AppendLine ("&lt;DIDL-Lite xmlns:dc=&quot;http://purl.org/dc/elements/1.1/&quot; xmlns:upnp=&quot;urn:schemas-upnp-org:metadata-1-0/upnp/&quot; xmlns:song=&quot;www.wiimu.com/song/&quot; xmlns=&quot;urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/&quot;&gt;"); sb.AppendLine ("&lt;upnp:class&gt;object.item.audioItem.musicTrack&lt;/upnp:class&gt;"); sb.AppendLine ("&lt;item&gt;"); sb.AppendLine ("&lt;song:bitrate&gt;0&lt;/song:bitrate&gt;"); sb.AppendLine ("&lt;song:id&gt;0&lt;/song:id&gt;"); sb.AppendLine ("&lt;song:singerid&gt;0&lt;/song:singerid&gt;"); sb.AppendLine ("&lt;song:albumid&gt;0&lt;/song:albumid&gt;"); sb.AppendLine ("&lt;res protocolInfo=&quot;http-get:*:audio/mpeg:DLNA.ORG_PN=MP3;DLNA.ORG_OP=01;&quot; duration=&quot;1000&quot;&gt;" + url + "&lt;/res&gt;"); sb.AppendLine ("&lt;dc:title&gt;" + musicInfo.Title + "&lt;/dc:title&gt;"); sb.AppendLine ("&lt;upnp:artist&gt;TuneIn&lt;/upnp:artist&gt;"); sb.AppendLine ("&lt;upnp:album&gt;&lt;/upnp:album&gt;"); sb.AppendLine ("&lt;upnp:albumArtURI&gt;" + musicInfo.Image + "&lt;/upnp:albumArtURI&gt;"); sb.AppendLine ("&lt;/item&gt;"); sb.AppendLine ("&lt;/DIDL-Lite&gt;"); sb.AppendLine ("</Metadata>"); sb.AppendLine ("</Track" + 1 + ">"); sb.AppendLine ("</Tracks>"); sb.AppendLine ("</PlayList>"); sb.AppendLine (""); sb.AppendLine (""); sb.AppendLine (""); sb.AppendLine (""); //sendMusicLists(a31.IPAddress, a31.Port, "CreateQueue", sb.ToString()); sendMusicLists (a31.IPAddress, a31.Port, "CreateQueue", sb.ToString ()); StringBuilder playString = new StringBuilder (); playString.AppendLine (""); playString.AppendLine (""); playString.AppendLine (""); playString.AppendLine (""); playString.AppendLine ("" + listName + ""); playString.AppendLine ("" + 1 + ""); playString.AppendLine (""); playString.AppendLine (""); playString.AppendLine (""); 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 (); 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 (); 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 { } } /// /// 读取电台信息 /// /// 读取到的电台组列表信息,读取不到反馈为null string loginvTunerList (string url) { Shared.Net.MyWebClient webClient = new Shared.Net.MyWebClient (); //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)); //var dd = recevieBytes.Length; return System.Text.Encoding.UTF8.GetString (recevieBytes, 0, recevieBytes.Length); } catch (Exception e) { return null; } } } }