using System; using System.Collections.Generic; using System.Text; using Shared; namespace HDL_ON.UI.Music { public class A31LoveList : FrameLayout { public A31LoveList() { Tag = "Music"; } VerticalScrolViewLayout middViewLayout; public void Show() { #region 界面布局------ this.BackgroundColor = Color.ViewColor; var topView = new TopView(); this.AddChidren(topView.TopFLayoutView()); topView.topNameBtn.TextID = StringId.myLove; topView.clickBackBtn.MouseUpEventHandler += (sender, e) => { if (updateSelectedMusicThread != null) { try { if (updateSelectedMusicThread.IsAlive) { updateSelectedMusicThread.Abort(); ///关闭线程 } } catch { } } this.RemoveFromParent(); }; middViewLayout = new VerticalScrolViewLayout { BackgroundColor = Color.WhiteColor, Y = topView.fLayout.Bottom, Height = Application.GetRealHeight(H_W.H - H_W.T_Height), }; this.AddChidren(middViewLayout); FileView(); #endregion } /// /// 加载文件夹View的方法 /// void FileView() { for (int i = 0; i < 2; i++) { RowLayout addFlieRow = new RowLayout { Height = Application.GetRealHeight(104), LineColor = Color.WhiteColor, SubViewWidth = Application.GetRealWidth(90),//改变编辑控件宽度多少; }; middViewLayout.AddChidren(addFlieRow); //文件图标 Button fileIconBtn = new Button { X = Application.GetRealWidth(16), Y = Application.GetRealHeight(8), Width = Application.GetMinRealAverage(88), Height = Application.GetMinRealAverage(88), }; addFlieRow.AddChidren(fileIconBtn); //播放/暂停图标 Button playIconBtn = new Button { X = Application.GetRealWidth(78), Y = Application.GetRealHeight(70), Width = Application.GetMinRealAverage(24), Height = Application.GetMinRealAverage(24), UnSelectedImagePath = "MusicIcon/filePause.png", SelectedImagePath = "MusicIcon/filePlay.png", }; addFlieRow.AddChidren(playIconBtn); //文件名控件 Button fileNameBtn = new Button { X = fileIconBtn.Right + Application.GetRealWidth(16), Y = Application.GetRealHeight(42), Width = Application.GetRealWidth(217), Height = Application.GetRealHeight(20), TextColor = Color.TextColor, TextSize = TextSize.Text14, TextAlignment = TextAlignment.CenterLeft, }; addFlieRow.AddChidren(fileNameBtn); if (i == 0) { fileIconBtn.UnSelectedImagePath = "MusicIcon/loveMusic.png"; fileNameBtn.TextID = StringId.a31Music; playIconBtn.Tag = Language.StringByID(StringId.a31Music);//记录列表名 } else { fileIconBtn.UnSelectedImagePath = "MusicIcon/loveRadio.png"; fileNameBtn.TextID = StringId.radioLove; playIconBtn.Tag = Language.StringByID(StringId.radioLove);//记录列表名 } //点击按钮随机播放音乐 playIconBtn.MouseUpEventHandler += (sender, e) => { //点击按钮随机播放音乐 if (fileNameBtn.Text == Language.StringByID(StringId.a31Music)) { ///喜爱音乐列表 SelectedPaly(fileNameBtn.Text, A31MusicModel.Current.LoveMusicInfoList, playIconBtn); } else { ///喜爱电台列表 SelectedPaly(fileNameBtn.Text, A31MusicModel.Current.LoveRadioInfoList, playIconBtn); } }; Button clickBtn = new Button { X = fileIconBtn.Right + Application.GetRealWidth(16), Width = Application.GetRealWidth(375 - 138), Height = Application.GetRealHeight(104), }; addFlieRow.AddChidren(clickBtn); clickBtn.MouseUpEventHandler += (sender, e) => { if (fileNameBtn.Text == Language.StringByID(StringId.a31Music)) { ///喜爱音乐列表 A31LoveMusicList a31LoveMusicList = new A31LoveMusicList(); MainPage.BasePageView.AddChidren(a31LoveMusicList); MainPage.BasePageView.PageIndex = MainPage.BasePageView.ChildrenCount - 1; a31LoveMusicList.Show(); a31LoveMusicList.UpdateSelectedMusic(); } else { ///喜爱电台列表 A31LoveRadioList a31LoveRadioList = new A31LoveRadioList(); MainPage.BasePageView.AddChidren(a31LoveRadioList); MainPage.BasePageView.PageIndex = MainPage.BasePageView.ChildrenCount - 1; a31LoveRadioList.Show(); a31LoveRadioList.UpdateSelectedMusic(); } }; } } /// /// 暂停/播放图标点击播放 /// /// /// /// void SelectedPaly(string listName, List listMusic, Button playIconBtn) { ///查找当前播放音乐是否这个列表 if (playIconBtn.IsSelected) { if (listName == A31MusicModel.Current.A31PlayStatus.Artist && A31MusicModel.Current.A31PlayStatus.status == "play") { SendMethod.Pause(A31MusicModel.Current); A31MusicModel.Current.A31PlayStatus.status = "pause"; } } else { if (listMusic.Count == 0) { return; } if (listName == A31MusicModel.Current.A31PlayStatus.Artist && A31MusicModel.Current.A31PlayStatus.status != "play") { SendMethod.Play(A31MusicModel.Current); A31MusicModel.Current.A31PlayStatus.status = "play"; } else { ///取随机数; var ra = new Random { }.Next(0, listMusic.Count); ///查找当前的音乐; var songs = listMusic[ra]; //点击按钮随机播放音乐 System.Threading.Tasks.Task.Run(() => { System.Threading.Thread.Sleep(50); Application.RunOnMainThread(() => { //移除界面 System.Threading.Tasks.Task.Run(() => { PushList(songs, listName, listMusic); }); }); }); } } } System.Threading.Thread updateSelectedMusicThread; /// /// 定时更新文件夹播放状态 /// public void UpdateSelectedFile() { updateSelectedMusicThread = new System.Threading.Thread(() => { while (this.Parent != null) { System.Threading.Thread.Sleep(1000); Application.RunOnMainThread(() => { try { for (int i = 0; i < middViewLayout.ChildrenCount; i++) { RowLayout view = (RowLayout)middViewLayout.GetChildren(i); var playBtn = (Button)view.GetChildren(1);//直接FrameLayout父控件找到该控件Button ///查找当前播放音乐是否这个列表 if (A31MusicModel.Current.A31PlayStatus.Artist == playBtn.Tag.ToString() && A31MusicModel.Current.A31PlayStatus.status == "play") { playBtn.IsSelected = true; } else { playBtn.IsSelected = false; } } } catch { } }); } }) { IsBackground = true }; updateSelectedMusicThread.Start(); } /// /// 推送播放命令 /// /// 播放音乐 /// 列表名称 /// 推送列表(默认推50首) public void PushList(MusicInfo musicInfo, string listName, List musicList) { //最大发送的条数 int maxCount = 50; //找出当前点击音乐的索引 int statIndex = musicList.FindIndex((obj) => { return obj == musicInfo; }); //点击后面还有多少条音乐 int count = musicList.Count - statIndex; int endIndex = 0; if (maxCount < count) { //如果后面的音乐大于最大数,设置后面数据的最大索引 endIndex = statIndex + maxCount - 1; } else { //如果后面数据不多,就用总数减去最大条数,得到开始索引 statIndex = musicList.Count - maxCount - 1; endIndex = musicList.Count - 1; } //如果开始索引小于0,那就设置为第一条音乐 if (statIndex < 0) { statIndex = 0; } StringBuilder sb = new StringBuilder(); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine("<?xml version=\"1.0\"?>"); sb.AppendLine("<PlayList>"); sb.AppendLine("<ListName>DLNA" + listName + "</ListName>"); sb.AppendLine("<ListInfo>"); sb.AppendLine("<Radio>0</Radio>");// sb.AppendLine("<SourceName>UPnPServer</SourceName>");// sb.AppendLine("<MarkSearch>0</MarkSearch>"); sb.AppendLine("<TrackNumber>" + (endIndex - statIndex + 1) + "</TrackNumber>"); sb.AppendLine("<Quality>0</Quality>"); sb.AppendLine("<UpdateTime>5367</UpdateTime>"); sb.AppendLine("<LastPlayIndex>2</LastPlayIndex>"); sb.AppendLine("<SwitchPageMode>0</SwitchPageMode>"); sb.AppendLine("<CurrentPage>0</CurrentPage>"); sb.AppendLine("<TotalPages>0</TotalPages>"); sb.AppendLine("</ListInfo>"); sb.AppendLine("<Tracks>"); int number = 1; for (int i = statIndex, Tracknumber = 1; i <= endIndex; i++, Tracknumber++) { var tempMusicInfo = musicList[i]; if (musicInfo == tempMusicInfo) { number = Tracknumber; } if (tempMusicInfo.SourceType == "Local") { tempMusicInfo.URL = "http://" + new Shared.Net.NetWiFi().IpAddress + ":" + com.hdl.on.Server.Port + "/" + tempMusicInfo.ID; } sb.AppendLine("<Track" + Tracknumber + ">"); sb.AppendLine("<URL>" + tempMusicInfo.URL + "</URL>"); sb.AppendLine("<Metadata>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;"); 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 id=&quot;0&quot;&gt;"); sb.AppendLine("&lt;song:subid&gt;&lt;/song:subid&gt;"); sb.AppendLine("&lt;song:description&gt;unknown&lt;/song:description&gt;"); sb.AppendLine("&lt;song:skiplimit&gt;6&lt;/song:skiplimit&gt;"); sb.AppendLine("&lt;song:id&gt;" + tempMusicInfo.ID + "&lt;/song:id&gt;"); sb.AppendLine("&lt;song:like&gt;0&lt;/song:like&gt;"); sb.AppendLine("&lt;song:singerid&gt;0&lt;/song:singerid&gt;"); sb.AppendLine("&lt;song:albumid&gt;" + tempMusicInfo.AlbumId + "&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;" + "0:0:0.0" + "&quot;&gt;" + tempMusicInfo.URL + "&lt;/res&gt;"); sb.AppendLine("&lt;dc:title&gt;" + tempMusicInfo.Title + "&lt;/dc:title&gt;"); sb.AppendLine("&lt;dc:creator&gt;DJ Sanny J&lt;/dc:creator&gt;"); sb.AppendLine("&lt;upnp:artist&gt;" + listName + "&lt;/upnp:artist&gt;"); sb.AppendLine("&lt;upnp:album&gt;" + tempMusicInfo.Album + "&lt;/upnp:album&gt;"); sb.AppendLine("&lt;upnp:albumArtURI&gt;unknown&lt;/upnp:albumArtURI&gt;"); sb.AppendLine("&lt;/item&gt;"); sb.AppendLine("&lt;/DIDL-Lite&gt;"); sb.AppendLine("</Metadata>"); sb.AppendLine("<Id>" + tempMusicInfo.ID + "</Id>"); sb.AppendLine("<Source>STATION-NETWORK</Source>");//UPnPServer sb.AppendLine("<Key>" + tempMusicInfo.URL + "</Key>"); sb.AppendLine("</Track" + Tracknumber + ">"); } sb.AppendLine("</Tracks>"); sb.AppendLine("</PlayList>"); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); SendMusicLists(A31MusicModel.Current.IPAddress, A31MusicModel.Current.Port, "CreateQueue", sb.ToString()); StringBuilder playString = new StringBuilder(); playString.AppendLine(""); playString.AppendLine(""); playString.AppendLine(""); playString.AppendLine(""); playString.AppendLine("DLNA" + listName + ""); playString.AppendLine("" + number + ""); playString.AppendLine(""); playString.AppendLine(""); playString.AppendLine(""); Play(A31MusicModel.Current.IPAddress, A31MusicModel.Current.Port, playString.ToString()); } void SendMusicLists(string ip, int port, string soapAction, string listInfo) { System.Net.WebClient webClient = new System.Net.WebClient(); 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) { System.Net.WebClient webClient = new System.Net.WebClient(); 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 { } } } }