using System; using System.Collections.Generic; using System.Text; using Shared; using System.Xml; using Shared.SimpleControl.Pad.Music; using Shared.SimpleControl; using Shared.SimpleControl.R; namespace SmartHome.UI.SimpleControl.Pad.Music { class A31Dlna : FrameLayout { public void show (DLNAServer dlnaServer, List list,FrameLayout MusicSourcePage,FrameLayout PalyMusicPage) { var topFrameLayout = new FrameLayout { Height = Application.GetRealHeight (100), BackgroundColor = SkinStyle.Current.MainColor, }; AddChidren (topFrameLayout); var RadiolistName = new Button { TextID = MyInternationalizationString.Musicdlna, }; topFrameLayout.AddChidren (RadiolistName); 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 (); }; FrameLayout TtopFrameLayout = new FrameLayout (); TtopFrameLayout.Width = LayoutParams.MatchParent; TtopFrameLayout.Height = Application.GetRealHeight (100); TtopFrameLayout.Y = topFrameLayout.Bottom; TtopFrameLayout.BackgroundColor = 0xff121212; AddChidren (TtopFrameLayout); var sourcephoto = new Button { Width = Application.GetRealWidth (80), Height = Application.GetRealHeight (76), UnSelectedImagePath = "MusicIcon/dlna.png", TextAlignment = TextAlignment.CenterLeft, X = Application.GetRealWidth (30), Gravity = Gravity.CenterVertical, }; TtopFrameLayout.AddChidren (sourcephoto); var dlnamusic = new Button { Height = Application.GetRealHeight (100), TextID = MyInternationalizationString.Musicmediaplayer, TextAlignment = TextAlignment.CenterLeft, X = Application.GetRealWidth (130), Gravity = Gravity.CenterVertical, }; TtopFrameLayout.AddChidren (dlnamusic); Button dropdown = new Button { Width = Application.GetRealWidth (65), Height = Application.GetRealHeight (49), UnSelectedImagePath = "MusicIcon/Homepagexiala.png", X = Application.GetRealWidth (480), Gravity = Gravity.CenterVertical, }; TtopFrameLayout.AddChidren (dropdown); var middle = new VerticalScrolViewLayout (); middle.Y = TtopFrameLayout.Bottom; middle.Height = Application.GetRealHeight (Application.DesignHeight - 100 - 100-150); middle.BackgroundColor = 0xff2F2F2F; AddChidren (middle); for (int i = 0; i < list.Count; i++) { var music = list [i]; var dlnarowLayout = new RowLayout { Height = Application.GetRealHeight (100), }; middle.AddChidren (dlnarowLayout); 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, }; dlnarowLayout.AddChidren (radius); var listnext = new Button { Width = Application.GetRealWidth (87), Height = Application.GetRealHeight (100), UnSelectedImagePath = "MusicIcon/Next.png", X = Application.GetRealWidth (480), }; dlnarowLayout.AddChidren (listnext); var dlnalists = new Button { TextAlignment = TextAlignment.CenterLeft, X = Application.GetRealWidth (80), Text = music.dlnalistName, }; dlnarowLayout.AddChidren (dlnalists); EventHandler A31dlnaserver = (sender, e) => { MainPage.Loading.Start (Language.StringByID (MyInternationalizationString.load) + "..."); System.Threading.Tasks.Task.Run (() => { try { var dlnaString = readDlnaList (dlnaServer, music.ID); if (dlnaString == null) { return; } list.Clear (); dlnaString = dlnaString.Replace ("&", "&").Replace ("<", "<").Replace (">", ">").Replace (""", "\"").Replace ("", "").Replace ("&", ""); int StartIndex = dlnaString.IndexOf ("") + "".Length; if (endIndex <= StartIndex) { return; } var ss = dlnaString.Substring (StartIndex, endIndex - StartIndex); var xml = new XmlDocument (); xml.LoadXml (ss); foreach (XmlNode node in xml.ChildNodes [0].ChildNodes) { MusicInfo musicInfo = new MusicInfo (); musicInfo.ID = node.Attributes ["id"].Value; foreach (XmlNode temnode in node.ChildNodes) { switch (temnode.Name) { case "dc:title": musicInfo.dlnalistName = temnode.InnerText; break; } } list.Add (musicInfo); } } catch { } finally { Application.RunOnMainThread (() => { MainPage.Loading.Hide (); A31DlnaList a31DlnaList = new A31DlnaList (); MusicSourcePage.AddChidren (a31DlnaList); a31DlnaList.show (dlnaServer, music.dlnalistName, list,MusicSourcePage,PalyMusicPage); }); } }); }; dlnalists.MouseUpEventHandler += A31dlnaserver; listnext.MouseUpEventHandler += A31dlnaserver; radius.MouseUpEventHandler += A31dlnaserver; } } /// /// 读取DLNA的音乐列表 /// /// 列表ID /// 读取到的音乐列表信息,读取不到反馈为null string readDlnaList (DLNAServer dlnaServer, string id) { string tempString = "" + id + "BrowseDirectChildren*0999+dc:title"; Shared.Net.MyWebClient webClient = new Shared.Net.MyWebClient (); webClient.Proxy = null; webClient.Headers.Add ("Content-type", "text/xml;charset=\"utf-8\""); webClient.Headers.Add ("Soapaction", "\"urn:schemas-upnp-org:service:ContentDirectory:1#Browse\""); try { byte [] recevieBytes = webClient.UploadData (new Uri ("http://" + dlnaServer.URL + dlnaServer.ControlURL), "POST", System.Text.Encoding.UTF8.GetBytes (tempString)); return System.Text.Encoding.UTF8.GetString (recevieBytes, 0, recevieBytes.Length); } catch (Exception e) { return null; } } } }