wxr
2020-08-11 2bec9c838d2d688025698de8ec1de401ffd7dd1f
HDL_ON/UI/UI2/FuntionControlView/Music/A31LoveMusicList.cs
@@ -21,30 +21,17 @@
        {
            #region   界面布局------
            this.BackgroundColor = Color.ViewColor;
            this.BackgroundColor = MusicColor.ViewColor;
            var topView = new TopView();
            this.AddChidren(topView.TopFLayoutView());
            topView.topNameBtn.TextID = StringId.myLove;//a31Music;
            topView.clickBackBtn.MouseUpEventHandler += (sender, e) =>
            {
                if (updateSelectedMusicThread != null)
                {
                    try
                    {
                        if (updateSelectedMusicThread.IsAlive)
                        {
                            updateSelectedMusicThread.Abort();
                            ///关闭线程
                        }
                    }
                    catch { }
                }
                this.RemoveFromParent();
            };
            middViewLayout = new VerticalRefreshLayout
            {
                BackgroundColor = Color.WhiteColor,
                BackgroundColor = MusicColor.WhiteColor,
                Y = topView.fLayout.Bottom,
                Height = Application.GetRealHeight(H_W.H - H_W.T_Height),
            };
@@ -62,356 +49,16 @@
        public void MusicView(string listName)
        {
            middViewLayout.RemoveAll();
            for (int i = 0; i < A31MusicModel.Current.LoveMusicInfoList.Count; i++)
            {
                var songs = A31MusicModel.Current.LoveMusicInfoList[i];
                if (string.IsNullOrEmpty(songs.Title))
                {
                    //防止歌曲名字为空抛异常
                    songs.Title = " ";
                }
                if (string.IsNullOrEmpty(songs.Artist))
                {
                    //防止歌手名字为空抛异常
                    songs.Artist = " ";
                }
                FrameLayout musicViewFl = new FrameLayout
                {
                    Width = Application.GetRealWidth(375),
                    Height = Application.GetRealHeight(44),
                };
                middViewLayout.AddChidren(musicViewFl);
                //列表当前播放音乐图标
                Button playIconBtn = new Button
                {
                    X = Application.GetRealWidth(16),
                    Y = Application.GetRealHeight(10),
                    Width = Application.GetMinRealAverage(24),
                    Height = Application.GetMinRealAverage(24),
                    UnSelectedImagePath = "MusicIcon/playStatus.png",
                    Visible = false,
                    Name = "playStatus",
                    Tag = songs.URL,
                };
                musicViewFl.AddChidren(playIconBtn);
                //歌曲控件
                Button songBtn = new Button
                {
                    X = Application.GetRealWidth(16),
                    Y = Application.GetRealHeight(11),
                    Width = Application.GetRealWidth(100),
                    Height = Application.GetRealHeight(22),
                    TextColor = Color.MusicTxet14Color,
                    TextSize = TextSize.Text16,
                    TextAlignment = TextAlignment.CenterLeft,
                    Name = "song",
                    Tag = songs.URL,
                    Text = songs.Title.Trim()
                };
                musicViewFl.AddChidren(songBtn);
                var wText = songBtn.GetTextWidth();//获取文本宽度
                ///(歌曲+歌手)宽度最大值时280
                if (wText > 280)
                {
                    songBtn.Width = Application.GetRealWidth(280);
                }
                else
                {
                    songBtn.Width = wText + 5;
                }
                //歌手控件
                Button singerBtn = new Button
                {
                    X = songBtn.Right,
                    Y = Application.GetRealHeight(11),
                    Width = Application.GetRealWidth(280 - songBtn.Width),
                    Height = Application.GetRealHeight(22),
                    TextColor = Color.MusicNoTxetColor,
                    TextSize = TextSize.Text12,
                    TextAlignment = TextAlignment.CenterLeft,
                    //Text =("-" +songs.Artist).Trim(),
                    Name = "singer",
                    Tag = songs.URL,
                    Text = "-" + songs.Artist.Trim()
                };
                musicViewFl.AddChidren(singerBtn);
                if (wText > 280)
                {
                    ///歌名长度小于250才可以显示歌手出来
                    singerBtn.Width = Application.GetRealWidth(0);
                }
                //添加喜爱音乐控件
                Button loveIcon = new Button
                {
                    X = Application.GetRealWidth(291),
                    Y = Application.GetRealHeight(8),
                    Width = Application.GetMinRealAverage(28),
                    Height = Application.GetMinRealAverage(28),
                    UnSelectedImagePath = "MusicIcon/loveSelected.png",
                };
                musicViewFl.AddChidren(loveIcon);
                loveIcon.MouseUpEventHandler += (sender, e) =>
                {
                    var music= A31MusicModel.Current.LoveMusicInfoList.Find((m) => m.URL == songs.URL);
                    if (music!= null)
                    {
                        ///存在就删除
                        A31MusicModel.Current.LoveMusicInfoList.Remove(music);
                    }
                    A31MusicModel.Save();
                    musicViewFl.RemoveFromParent();
                };
                //添加到我的列表
                Button addIcon = new Button
                {
                    X = Application.GetRealWidth(335),
                    Y = Application.GetRealHeight(8),
                    Width = Application.GetMinRealAverage(28),
                    Height = Application.GetMinRealAverage(28),
                    UnSelectedImagePath = "MusicIcon/addMusic1.png",
                };
                musicViewFl.AddChidren(addIcon);
                addIcon.MouseUpEventHandler = (sender, e) =>
                {
                    new View.DialogView { }.FieListView(songs);
                };
                //点击控件
                Button clickBtn = new Button
                {
                    Width = Application.GetRealWidth(280),
                    Height = Application.GetRealHeight(44),
                    Tag = songs,//标记播放哪一首歌曲
                };
                musicViewFl.AddChidren(clickBtn);
                ///点击播放事件
                clickBtn.MouseUpEventHandler += (sender, e) =>
                {
                    System.Threading.Tasks.Task.Run(() =>
                    {
                        System.Threading.Thread.Sleep(50);
                        Application.RunOnMainThread(() =>
                        {
                            //移除界面
                            A31MusicModel.Current.A31PlayStatus.Title = songBtn.Text;
                            MainPage.BasePageView.RemoveViewByTag("Music");
                            System.Threading.Tasks.Task.Run(() =>
                            {
                                PushList(songs, listName, A31MusicModel.Current.LoveMusicInfoList);
                            });
                        });
                    });
                };
            }
            UI2.FuntionControlView.Music.UpdateThread.playMusuc(middViewLayout, "我的最爱", listName, A31MusicModel.Current.LoveMusicInfoList, A31MusicModel.Current);
        }
        /// <summary>
        /// 定时更新当前播放音乐
        /// </summary>
        System.Threading.Thread updateSelectedMusicThread;
        public void UpdateSelectedMusic()
        {
            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++)
                            {
                                FrameLayout view = (FrameLayout)middViewLayout.GetChildren(i);
                                var volIconBtn = (Button)view.GetChildren(0);//直接FrameLayout父控件找到该控件Button
                                var songNameBtn = (Button)view.GetChildren(1);//直接FrameLayout父控件找到该控件Button
                                var artistNameBtn = (Button)view.GetChildren(2);//直接FrameLayout父控件找到该控件Button
                                if (A31MusicModel.Current.A31PlayStatus.TrackURL == songNameBtn.Tag.ToString())
                                {
                                    volIconBtn.Visible = true;
                                    songNameBtn.TextColor = Color.SelectedColor;
                                    songNameBtn.X = Application.GetRealWidth(48);
                                    artistNameBtn.X = songNameBtn.Right;
                                    artistNameBtn.TextColor = Color.SelectedColor;
                                }
                                else
                                {
                                    volIconBtn.Visible = false;
                                    songNameBtn.TextColor = Color.MusicTxet14Color;
                                    songNameBtn.X = Application.GetRealWidth(16);
                                    artistNameBtn.X = songNameBtn.Right;
                                    artistNameBtn.TextColor = Color.MusicNoTxetColor;
                                }
                            }
                        }
                        catch { }
                    });
                }
            });
            updateSelectedMusicThread.Start();
           UI2.FuntionControlView.Music.UpdateThread.updateThread(this, middViewLayout,A31MusicModel.Current);
        }
        /// <summary>
        /// 推送播放命令
        /// </summary>
        /// <param name="musicInfo">播放音乐</param>
        /// <param name="listName">列表名称</param>
        /// <param name="musicList">推送列表(默认推50首)</param>
        public void PushList(MusicInfo musicInfo, string listName, List<MusicInfo> 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("<?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;DLNA" + listName + "&lt;/ListName&gt;");
            sb.AppendLine("&lt;ListInfo&gt;");
            sb.AppendLine("&lt;Radio&gt;0&lt;/Radio&gt;");//
            sb.AppendLine("&lt;SourceName&gt;UPnPServer&lt;/SourceName&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 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 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;" + tempMusicInfo.ID + "&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;" + "0:0:0.0" + "&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;" + listName + "&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;" + tempMusicInfo.ID + "&lt;/Id&gt;");
                sb.AppendLine("&lt;Source&gt;STATION-NETWORK&lt;/Source&gt;");//UPnPServer
                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(A31MusicModel.Current.IPAddress, A31MusicModel.Current.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>DLNA" + listName + "</QueueName>");
            playString.AppendLine("<Index>" + number + "</Index>");
            playString.AppendLine("</u:PlayQueueWithIndex>");
            playString.AppendLine("</s:Body>");
            playString.AppendLine("</s:Envelope>");
            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
            {
            }
        }
    }
}