using System; using System.Collections.Generic; using System.Text; using Shared; using Shared.SimpleControl.R; using System.Xml; using Shared.SimpleControl.Phone; using Shared.SimpleControl.Phone.Music; using Shared.SimpleControl; namespace SmartHome.UI.SimpleControl.Phone.Music { /// /// 电台播放列表 /// class A31CnList : FrameLayout { public void Show (A31MusicModel a31, string Name) { 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 RadiolistName = new Button { Text = Name, TextColor = SkinStyle.Current.MusicTextColor, }; 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 (); middle.Y = topFrameLayout.Bottom; middle.Height = Application.GetRealHeight (Application.DesignHeight - 136); //middle.BackgroundColor = 0xff2F2F2F; middle.BackgroundColor = SkinStyle.Current.MusicVerticalScrolViewLayout; AddChidren (middle); var cnlistMusicInfo = new List (); foreach (var v in a31.CnRadioInfoList) { #region Audio if (v.Cntype == "audio") { cnlistMusicInfo.Add (v); var cnrow = new RowLayout { Height = Application.GetRealHeight (100), LineColor = SkinStyle.Current.MusicRowLayoutLineColor, }; 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 = System.IO.File.Exists (v.Image) ? v.Image : "MusicIcon/musicplay1.png", //UnSelectedImagePath = "MusicIcon/PlayMusic.png", }; cnrow.AddChidren (title); var cntext = new Button { Text = v.Title, X = Application.GetRealWidth (100), Width = Application.GetRealWidth (400), TextAlignment = TextAlignment.CenterLeft, Gravity = Gravity.CenterVertical, Tag = v, TextColor=SkinStyle.Current.MusicTextColor, }; cnrow.AddChidren (cntext); EventHandler audiosources = (sen, e) => { cnrow.BackgroundColor = 0xffFE5E00; System.Threading.Tasks.Task.Run (() => { System.Threading.Thread.Sleep (50); Application.RunOnMainThread (() => { for (int i = MainPage.MainFrameLayout.ChildrenCount - 1; 0 <= i; i--) { var view = MainPage.MainFrameLayout.GetChildren (i); if (view.GetType () == typeof (A31CnList)) { 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); a31.CnRadioInfoList = cnlistMusicInfo; A31PlayMusicPage a31PlayMusic = new A31PlayMusicPage (); MainPage.MainFrameLayout.AddChidren (a31PlayMusic); a31PlayMusic.Show (new List ()); //a31PlayMusic.Source = "RADIO-NETWORK"; a31.A31PlayStatus.Title = cntext.Text; System.Threading.Tasks.Task.Run (() => { pushList (a31, cntext.Tag as MusicInfo, Name); //MyMusic.dateTime = System.DateTime.Now.AddSeconds (-5); }); }); }); }; title.MouseUpEventHandler += audiosources; cntext.MouseUpEventHandler += audiosources; } #endregion #region Link if (v.Cntype == "link") { var cnrowlink = new RowLayout { Height = Application.GetRealHeight (100), LineColor = SkinStyle.Current.MusicRowLayoutLineColor, }; middle.AddChidren (cnrowlink); 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, }; cnrowlink.AddChidren (radius); var cntextlink = new Button { Gravity = Gravity.CenterVertical, Text = v.Title.Replace ("&", "/"), X = Application.GetRealWidth (80), TextAlignment = TextAlignment.CenterLeft, Tag = v.URL, TextColor = SkinStyle.Current.MusicTextColor, }; cnrowlink.AddChidren (cntextlink); var nextSD = new Button { Width = Application.GetRealWidth (87), Height = Application.GetRealHeight (100), UnSelectedImagePath = "MusicIcon/Next.png", SelectedImagePath = "MusicIcon/NextSelecte.png", X = Application.GetRealWidth (550), }; cnrowlink.AddChidren (nextSD); EventHandler CNresources = (sender, e) => { MainPage.Loading.Start (Language.StringByID (MyInternationalizationString.load)); System.Threading.Tasks.Task.Run (() => { try { var redString = readRadioList (cntextlink.Tag.ToString ()); if (redString == null) { return; } int startIndex2 = redString.IndexOf (""); int endIndex = redString.IndexOf ("") + "".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; } a31.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; a31.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; a31.CnRadioInfoList.Add (musicInfoCN); } } } } catch { } finally { Application.RunOnMainThread (() => { MainPage.Loading.Hide (); A31CnList a31CnPlay = new A31CnList (); MainPage.MainFrameLayout.AddChidren (a31CnPlay); a31CnPlay.Show (a31, cntextlink.Text); }); } }); }; cntextlink.MouseUpEventHandler += CNresources; nextSD.MouseUpEventHandler += CNresources; } #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>http://opml.radiotime.com/Tune.ashx?id=s112201&amp;partnerId=yvcOjvJP</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 = readRadioList (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 (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 { } } /// /// 读取电台信息 /// /// 读取到的电台组列表信息,读取不到反馈为null string readRadioList (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)); return System.Text.Encoding.UTF8.GetString (recevieBytes, 0, recevieBytes.Length); } catch (Exception e) { return null; } } } }