コード例 #1
0
        public bool Play(Music music = null, NetMusic netMusic = null)
        {
            var tmpIndex = CurrentIndex;

            if (music != null)
            {
                CurrentIndex = CurrentMusicList.Add(music);
            }
            else if (netMusic != null)
            {
                CurrentIndex = CurrentMusicList.Add(new Music(netMusic));
            }
            if (CurrentIndex >= 0 && CurrentIndex < CurrentMusicList.Count && CurrentMusicList[CurrentIndex].Origin != NetMusicType.LocalMusic)
            {
                var url = "";
                if (NetMusicHelper.CheckLink(CurrentMusicList[CurrentIndex].Path))
                {
                    url = CurrentMusicList[CurrentIndex].Path;
                }
                else
                {
                    url = NetMusicHelper.GetUrlByNetMusic(CurrentMusicList[CurrentIndex]);
                    if (!NetMusicHelper.CheckLink(url))
                    {
                        CurrentMusicList.RemoveAt(CurrentIndex);
                        CurrentIndex = tmpIndex;
                        Play();
                        return(false);
                    }
                    CurrentMusicList[CurrentIndex].Path = url;
                }
                if (url.Length <= 0)
                {
                    CurrentMusicList.Remove(CurrentMusicList[CurrentIndex]);
                    CurrentIndex = tmpIndex;
                    return(false);
                }
            }
            else if (CurrentMusicList.Count > 0 && (CurrentIndex < 0 || CurrentIndex >= CurrentMusicList.Count))
            {
                CurrentIndex = 0;
            }
            if (CurrentIndex >= 0 && CurrentIndex < CurrentMusicList.Count)
            {
                if (Regex.IsMatch(CurrentMusicList[CurrentIndex].Path, "[http|https]://") && !NetMusicHelper.CheckLink(CurrentMusicList[CurrentIndex].Path))
                {
                    CurrentMusicList[CurrentIndex].Path = string.Empty;
                    return(false);
                }
                bsp.Stop();
                bsp.FileName = CurrentMusicList[CurrentIndex].Path;
                bsp.Play();
                CurrentMusicList[CurrentIndex].Duration = bsp.TotalTime;
                PlayMusicButton.Visibility  = Visibility.Hidden;
                PauseMusicButton.Visibility = Visibility.Visible;
                Title           = CurrentMusicList[CurrentIndex].Title + " - " + CurrentMusicList[CurrentIndex].Singer;
                notifyIcon.Text = Title.Length >= 63 ? Title.Substring(0, 60) + "..." : Title;
                HistoryMusicList.Add(new MusicHistory(CurrentMusicList[CurrentIndex]));
                SetMiniLable(CurrentMusicList[CurrentIndex]);
            }
            return(true);
        }
コード例 #2
0
        private void RefreshLocalListThread()
        {
            Dispatcher.Invoke(new Action(() =>
            {
                RefreshMusicCavas.Visibility = Visibility.Visible;
            }));
            var           count    = 0;
            List <string> pathList = new List <string>();

            foreach (var item in FolderList)
            {
                DirectoryInfo TheFolder = new DirectoryInfo(item);
                foreach (FileInfo NextFile in TheFolder.GetFiles())
                {
                    var pattern = ".+?(\\.mp3|\\.wav|\\.flac|\\.wma|\\.ape|\\.m4a)$";
                    if (Regex.IsMatch(NextFile.Name, pattern, RegexOptions.IgnoreCase))
                    {
                        pathList.Add(item + NextFile.Name);
                        count++;
                    }
                }
            }
            List <Music> WillBeRemove = new List <Music>();

            foreach (var item in LocalMusicList)
            {
                if (pathList.Contains(item.Path))
                {
                    pathList.Remove(item.Path);
                }
                else
                {
                    WillBeRemove.Add(item);
                }
            }

            for (int i = 0; i < WillBeRemove.Count; i++)
            {
                Dispatcher.Invoke(new Action(() =>
                {
                    LocalMusicList.Remove(WillBeRemove[i]);
                }));
            }

            foreach (var item in pathList)
            {
                if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
                {
                    var music = new Music(item);
                    Dispatcher.Invoke(new Action(() => {
                        LocalMusicList.Add(music);
                    }));
                }
                else
                {
                    object[] args      = new object[] { LocalMusicList, item };
                    Thread   staThread = new Thread(new ParameterizedThreadStart(RefreshLocalListSTA));
                    staThread.SetApartmentState(ApartmentState.STA);
                    staThread.Start(args);
                    staThread.Join();
                }
                Dispatcher.Invoke(new Action(() =>
                {
                    RefreshMusicCountLable.Content = LocalMusicList.Count + "/" + count;
                }));
            }
            Dispatcher.Invoke(new Action(() => {
                RefreshMusicCavas.Visibility = Visibility.Hidden;
            }));
        }