Пример #1
0
 public static MenuFlyoutSubItem AppendRecentAddedItem(MenuFlyoutSubItem parent, object title, List <Music> songs, int limit)
 {
     if (songs.Count > 0)
     {
         var recenItem = new MenuFlyoutItem()
         {
             Text = title is string stringTitle?Helper.LocalizeMessage(stringTitle) : title.ToString()
         };
         recenItem.Click += (sender, args) =>
         {
             MediaHelper.SetPlaylistAndPlay(songs.RandItems(limit));
         };
         parent.Items.Add(recenItem);
     }
     return(parent);
 }
Пример #2
0
        public static async Task <bool> UpdateMusicLibrary(StorageFolder folder, string message = null)
        {
            MainPage.Instance.Loader.ShowDeterminant(message ?? "LoadMusicLibrary", true);
            loadingTree = new FolderTree();
            if (!await loadingTree.Init(folder, (treeFolder, file, progress, max) =>
            {
                bool isDeterminant = max != 0;
                if (MainPage.Instance.Loader.IsDeterminant != isDeterminant)
                {
                    MainPage.Instance.Loader.IsDeterminant = isDeterminant;
                }
                if (isDeterminant)
                {
                    MainPage.Instance.Loader.Max = max;
                    MainPage.Instance.Loader.Progress = progress;
                    MainPage.Instance.Loader.Text = message ?? file;
                }
            }))
            {
                return(false);
            }
            MainPage.Instance.Loader.SetLocalizedText(message ?? "UpdateMusicLibrary");
            Helper.CurrentFolder = folder;
            await Task.Run(() =>
            {
                loadingTree.MergeFrom(Settings.settings.Tree);
                Settings.settings.Tree     = loadingTree;
                Settings.settings.RootPath = folder.Path;
            });

            MusicLibraryPage.SortAndSetAllSongs(await Task.Run(Settings.settings.Tree.Flatten));
            MainPage.Instance.Loader.Progress = 0;
            MainPage.Instance.Loader.Max      = listeners.Count;
            for (int i = 0; i < listeners.Count;)
            {
                var listener = listeners[i];
                listener.PathSet(folder.Path);
                MainPage.Instance.Loader.Progress = ++i;
            }
            MediaHelper.RemoveBadMusic();
            App.Save();
            MainPage.Instance.Loader.Hide();
            return(true);
        }
Пример #3
0
 private void ClearButton_Click(object sender, RoutedEventArgs e)
 {
     MediaHelper.Clear();
     SetEnabled();
 }
Пример #4
0
        private void PlayAllButton_Click(object sender, RoutedEventArgs e)
        {
            var data = (sender as Button).DataContext as AlbumView;

            MediaHelper.ShuffleAndPlay(data.Songs);
        }
Пример #5
0
 void IMultiSelectListener.Play(MultiSelectCommandBar commandBar)
 {
     MediaHelper.SetMusicAndPlay(GetSelectItems());
 }
Пример #6
0
        private void LocalShuffleItem_Tapped(object sender, TappedRoutedEventArgs e)
        {
            var tree = History.Peek();

            MediaHelper.ShuffleAndPlay(LocalFrame.SourcePageType == typeof(LocalMusicPage) ? tree.Files : tree.Flatten());
        }
Пример #7
0
        private async void OnLaunched(LaunchActivatedEventArgs e, Music music)
        {
            await Settings.Init();

            await MusicLibraryPage.Init();

            if (Settings.settings.LastPage == "Albums")
            {
                await AlbumsPage.Init();
            }
            await Helper.Init();

            await UpdateHelper.Init();

            Frame rootFrame = Window.Current.Content as Frame;

            // 不要在窗口已包含内容时重复应用程序初始化,
            // 只需确保窗口处于活动状态
            if (rootFrame == null)
            {
                // 创建要充当导航上下文的框架,并导航到第一页
                rootFrame = new Frame();

                rootFrame.NavigationFailed += OnNavigationFailed;

                if (e?.PreviousExecutionState == ApplicationExecutionState.Terminated)
                {
                    //TODO: 从之前挂起的应用程序加载状态
                }

                // 将框架放在当前窗口中
                Window.Current.Content = rootFrame;
            }

            if (e == null || e.PrelaunchActivated == false)
            {
                if (Windows.Foundation.Metadata.ApiInformation.IsMethodPresent("Windows.ApplicationModel.Core.CoreApplication", "EnablePrelaunch"))
                {
                    Windows.ApplicationModel.Core.CoreApplication.EnablePrelaunch(true);
                }
                if (rootFrame.Content == null)
                {
                    // 当导航堆栈尚未还原时,导航到第一页,
                    // 并通过将所需信息作为导航参数传入来配置
                    // 参数
                    rootFrame.Navigate(typeof(MainPage), e?.Arguments);
                    if (e != null && e.TileId != "App")
                    {
                        var tileId = System.Net.WebUtility.UrlDecode(e.TileId);
                        MainPage.Instance.NavigateToPage(bool.Parse(e.Arguments) ? typeof(PlaylistsPage) :
                                                         tileId.StartsWith(Helper.Localize(MenuFlyoutHelper.MyFavorites)) ? typeof(MyFavoritesPage) : typeof(AlbumPage), tileId);
                    }
                }
                // 确保当前窗口处于活动状态
                Window.Current.Activate();
            }
            Windows.ApplicationModel.Core.CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true;
            MediaHelper.Init(music);
            await AlbumsPage.Init();

            await RecentPage.Init();

            ToastHelper.Init();
            //LaunchVoiceAssistant();

            foreach (var listener in LoadedListeners)
            {
                listener.Invoke();
            }
            Inited = true;
            // If background task is already registered, do nothing
            if (BackgroundTaskRegistration.AllTasks.Any(i => i.Value.Name.Equals(ToastHelper.ToastTaskName)))
            {
                return;
            }
            // Otherwise request access
            BackgroundAccessStatus status = await BackgroundExecutionManager.RequestAccessAsync();

            // Create the background task
            BackgroundTaskBuilder builder = new BackgroundTaskBuilder()
            {
                Name = ToastHelper.ToastTaskName
            };

            // Assign the toast action trigger
            builder.SetTrigger(new ToastNotificationActionTrigger());

            // And register the task
            BackgroundTaskRegistration registration = builder.Register();
        }
Пример #8
0
 void IMultiSelectListener.Play(MultiSelectCommandBar commandBar)
 {
     MediaHelper.SetMusicAndPlay(SelectedSongs);
 }
Пример #9
0
        public static MenuFlyout GetShuffleMenu(int limit = 100)
        {
            var flyout     = new MenuFlyout();
            var nowPlaying = new MenuFlyoutItem()
            {
                Text = Helper.Localize("NowPlaying")
            };

            nowPlaying.Click += (sender, args) =>
            {
                MediaHelper.ShuffleAndPlay();
            };
            flyout.Items.Add(nowPlaying);
            flyout.Items.Add(new MenuFlyoutSeparator());
            var musicLibrary = new MenuFlyoutItem()
            {
                Text = Helper.Localize("Music Library")
            };

            musicLibrary.Click += (sender, args) =>
            {
                MediaHelper.SetPlaylistAndPlay(MusicLibraryPage.AllSongs.RandItems(limit));
            };
            flyout.Items.Add(musicLibrary);
            var artist = new MenuFlyoutItem()
            {
                Text = Helper.Localize("Artist")
            };

            artist.Click += (sender, args) =>
            {
                var rArtist = MusicLibraryPage.AllSongs.GroupBy(m => m.Artist).RandItem();
                MediaHelper.SetPlaylistAndPlay(rArtist.Shuffle());
                Helper.ShowNotificationWithoutLocalization(Helper.LocalizeMessage("PlayRandomArtist", rArtist.Key));
            };
            flyout.Items.Add(artist);
            var album = new MenuFlyoutItem()
            {
                Text = Helper.Localize("Album")
            };

            album.Click += (sender, args) =>
            {
                var rAlbum = MusicLibraryPage.AllSongs.GroupBy(m => m.Album).RandItem();
                MediaHelper.SetPlaylistAndPlay(rAlbum.Shuffle());
                Helper.ShowNotificationWithoutLocalization(Helper.LocalizeMessage("PlayRandomAlbum", rAlbum.Key));
            };
            flyout.Items.Add(album);
            if (Settings.settings.Playlists.Count > 0)
            {
                var playlist = new MenuFlyoutItem()
                {
                    Text = Helper.Localize("Playlist")
                };
                playlist.Click += (sender, args) =>
                {
                    var rPlaylist = Settings.settings.Playlists.RandItem();
                    MediaHelper.SetPlaylistAndPlay(rPlaylist.Songs.RandItems(limit));
                    Helper.ShowNotificationWithoutLocalization(Helper.LocalizeMessage("PlayRandomPlaylist", rPlaylist.Name));
                };
                flyout.Items.Add(playlist);
            }
            if (!string.IsNullOrEmpty(Settings.settings.RootPath))
            {
                var localFolder = new MenuFlyoutItem()
                {
                    Text = Helper.Localize("Local Folder")
                };
                localFolder.Click += (sender, args) =>
                {
                    var rLocalFolder = Settings.settings.Tree.GetAllTrees().Where(tree => tree.Files.Count > 0).RandItem();
                    MediaHelper.SetMusicAndPlay(rLocalFolder.Files.RandItems(limit));
                    Helper.ShowNotificationWithoutLocalization(Helper.LocalizeMessage("PlayRandomLocalFolder", rLocalFolder.Directory));
                };
                flyout.Items.Add(localFolder);
            }
            if (RecentPage.RecentAdded?.Count > 0)
            {
                var recentAdded = new MenuFlyoutItem()
                {
                    Text = Helper.Localize("Recent Added")
                };
                recentAdded.Click += (sender, args) =>
                {
                    MediaHelper.SetMusicAndPlay(RecentPage.RecentAdded.TimeLine.RandItems(limit));
                };
                flyout.Items.Add(recentAdded);
            }
            if (Settings.settings.RecentPlayed.Count > 0)
            {
                var recentPlayed = new MenuFlyoutItem()
                {
                    Text = Helper.Localize("Recent Played")
                };
                recentPlayed.Click += (sender, args) =>
                {
                    MediaHelper.SetMusicAndPlay(Settings.settings.RecentPlayed.ToMusicList());
                };
                flyout.Items.Add(recentPlayed);
            }
            if (MusicLibraryPage.SongCount > limit)
            {
                flyout.Items.Add(new MenuFlyoutSeparator());
                var mostPlayed = new MenuFlyoutItem()
                {
                    Text = Helper.Localize("Most Played")
                };
                mostPlayed.Click += (sender, args) =>
                {
                    List <Music> list = new List <Music>();
                    foreach (var group in MusicLibraryPage.AllSongs.GroupBy(m => m.PlayCount).OrderByDescending(g => g.Key))
                    {
                        if (list.Count > limit)
                        {
                            break;
                        }
                        list.AddRange(group);
                    }
                    MediaHelper.SetPlaylistAndPlay(list.Shuffle().Take(limit));
                };
                flyout.Items.Add(mostPlayed);
                var leastPlayed = new MenuFlyoutItem()
                {
                    Text = Helper.Localize("Least Played")
                };
                leastPlayed.Click += (sender, args) =>
                {
                    List <Music> list = new List <Music>();
                    foreach (var group in MusicLibraryPage.AllSongs.GroupBy(m => m.PlayCount).OrderBy(g => g.Key))
                    {
                        if (list.Count > limit)
                        {
                            break;
                        }
                        list.AddRange(group);
                    }
                    MediaHelper.SetPlaylistAndPlay(list.Shuffle().Take(limit));
                };
                flyout.Items.Add(leastPlayed);
            }
            return(flyout);
        }
Пример #10
0
        public MenuFlyout GetMusicMenuFlyout(IMenuFlyoutItemClickListener listener = null, MenuFlyoutOption option = null)
        {
            if (option == null)
            {
                option = new MenuFlyoutOption();
            }
            Music music         = (Data as IMusicable).ToMusic();
            var   flyout        = new MenuFlyout();
            var   localizedPlay = Helper.Localize("Play");
            var   playItem      = new MenuFlyoutItem()
            {
                Icon = new SymbolIcon(Symbol.Play),
                Text = localizedPlay,
                Name = MusicMenuName
            };

            playItem.SetToolTip(localizedPlay + Helper.LocalizeMessage("MusicName", music.Name));
            playItem.Click += (s, args) =>
            {
                MediaHelper.SetMusicAndPlay(music);
            };
            flyout.Items.Add(playItem);
            if (MediaHelper.CurrentMusic != null && music != MediaHelper.CurrentMusic)
            {
                var playNextItem = new MenuFlyoutItem()
                {
                    Text = Helper.LocalizeText("PlayNext"),
                    Icon = new SymbolIcon(Symbol.Upload)
                };
                playNextItem.Click += (s, args) =>
                {
                    int index = MediaHelper.CurrentMusic.Index + 1;
                    if (music.Index >= 0)
                    {
                        MediaHelper.MoveMusic(music.Index, index);
                    }
                    else
                    {
                        MediaHelper.AddMusic(music, index);
                        listener?.AddTo(music, MediaHelper.CurrentPlaylist, index, AddToCollectionType.NowPlaying);
                        foreach (var clickListener in ClickListeners)
                        {
                            clickListener.AddTo(music, MediaHelper.CurrentPlaylist, index, AddToCollectionType.NowPlaying);
                        }
                    }
                    Helper.ShowNotificationWithoutLocalization(Helper.LocalizeMessage("SetPlayNext", music.Name));
                };
                flyout.Items.Add(playNextItem);
            }
            flyout.Items.Add(GetAddToMenuFlyoutSubItem(listener));
            if (option.ShowRemove)
            {
                flyout.Items.Add(GetRemovableMenuFlyoutItem(music, listener));
            }
            if (option.ShowSelect || option.MultiSelectOption != null)
            {
                flyout.Items.Add(GetSelectItem(listener, option.MultiSelectOption));
            }
            flyout.Items.Add(GetShowInExplorerItem(music.Path, StorageItemTypes.File));
            var deleteItem = new MenuFlyoutItem()
            {
                Icon = new SymbolIcon(Symbol.Delete),
                Text = Helper.Localize("Delete From Disk")
            };

            deleteItem.Click += async(s, args) =>
            {
                await new RemoveDialog()
                {
                    Message = Helper.LocalizeMessage("DeleteMusicMessage", music.Name),
                    Confirm = async() =>
                    {
                        MainPage.Instance?.Loader.ShowIndeterminant("ProcessRequest");
                        MusicLibraryPage.AllSongs.Remove(music);
                        Settings.settings.RemoveMusic(music);
                        MediaHelper.DeleteMusic(music);
                        listener?.Delete(music);
                        if (!await Helper.FileNotExist(music.Path))
                        {
                            StorageFile file = await StorageFile.GetFileFromPathAsync(music.Path);

                            await file.DeleteAsync();
                        }
                        MainPage.Instance?.Loader.Hide();
                        Helper.ShowNotification(Helper.LocalizeMessage("MusicDeleted", music.Name));
                    }
                }.ShowAsync();
            };
            deleteItem.SetToolTip(Helper.LocalizeMessage("DeleteMusic", music.Name), false);
            flyout.Items.Add(deleteItem);
            foreach (var item in GetMusicPropertiesMenuFlyout(option.ShowSeeArtistsAndSeeAlbum).Items)
            {
                flyout.Items.Add(item);
            }
            return(flyout);
        }
Пример #11
0
        public MenuFlyoutSubItem GetAddToMenuFlyoutSubItem(IMenuFlyoutItemClickListener listener = null)
        {
            MenuFlyoutSubItem addToItem = new MenuFlyoutSubItem()
            {
                Text = Helper.Localize("Add To"),
                Name = AddToSubItemName
            };

            addToItem.SetToolTip("Add To Playlist");
            if (CurrentPlaylistName != NowPlaying)
            {
                var nowPlayingItem = new MenuFlyoutItem()
                {
                    Icon = new FontIcon()
                    {
                        Glyph = "\uEC4F"
                    },
                    Text = NowPlaying
                };
                nowPlayingItem.Click += async(sender, args) =>
                {
                    if (Data is IMusicable musicable)
                    {
                        Music music = musicable.ToMusic();
                        if (await Helper.FileNotExist(music.Path))
                        {
                            Helper.ShowMusicNotFoundNotification(music.Name);
                            return;
                        }
                        MediaHelper.AddMusic(music);
                        listener?.AddTo(Data, MediaHelper.CurrentPlaylist, MediaHelper.CurrentPlaylist.Count - 1, AddToCollectionType.NowPlaying);
                        foreach (var clickListener in ClickListeners)
                        {
                            clickListener.AddTo(Data, MediaHelper.CurrentPlaylist, MediaHelper.CurrentPlaylist.Count - 1, AddToCollectionType.NowPlaying);
                        }
                        Helper.ShowCancelableNotificationWithoutLocalization(Helper.LocalizeMessage("SongAddedTo", music.Name, NowPlaying), () =>
                        {
                            MediaHelper.RemoveMusic(music);
                        });
                    }
                    else if (Data is IEnumerable <IMusicable> songs)
                    {
                        if (songs.Count() == 0)
                        {
                            return;
                        }
                        foreach (var song in songs)
                        {
                            MediaHelper.AddMusic(song);
                        }
                        string message = songs.Count() == 1 ? Helper.LocalizeMessage("SongAddedTo", songs.ElementAt(0).ToMusic().Name, NowPlaying) :
                                         Helper.LocalizeMessage("SongsAddedTo", songs.Count(), NowPlaying);
                        listener?.AddTo(Data, MediaHelper.CurrentPlaylist, MediaHelper.CurrentPlaylist.Count - songs.Count() - 1, AddToCollectionType.NowPlaying);
                        foreach (var clickListener in ClickListeners)
                        {
                            clickListener.AddTo(Data, MediaHelper.CurrentPlaylist, MediaHelper.CurrentPlaylist.Count - songs.Count() - 1, AddToCollectionType.NowPlaying);
                        }
                        Helper.ShowCancelableNotificationWithoutLocalization(message, () =>
                        {
                            foreach (var song in songs)
                            {
                                MediaHelper.RemoveMusic(song.ToMusic());
                            }
                        });
                    }
                    else
                    {
                        return;
                    }
                };
                addToItem.Items.Add(nowPlayingItem);
            }
            if (CurrentPlaylistName != MyFavorites && ((Data is IMusicable m && !Settings.settings.MyFavorites.Contains(m.ToMusic())) ||
                                                       (Data is IEnumerable <IMusicable> list &&
                                                        list.Any(music => !Settings.settings.MyFavorites.Contains(music.ToMusic())))))
            {
                var favItem = new MenuFlyoutItem()
                {
                    Icon = new FontIcon()
                    {
                        Glyph = "\uEB51"
                    },
                    Text = MyFavorites
                };
                favItem.Click += async(sender, args) =>
                {
                    if (Data is IMusicable musicable)
                    {
                        Music music = musicable.ToMusic();
                        if (await Helper.FileNotExist(music.Path))
                        {
                            Helper.ShowMusicNotFoundNotification(music.Name);
                            return;
                        }
                        Settings.settings.LikeMusic(music);
                        Helper.ShowCancelableNotificationWithoutLocalization(Helper.LocalizeMessage("SongAddedTo", music.Name, MyFavorites), () =>
                        {
                            Settings.settings.DislikeMusic(music);
                        });
                    }
                    else if (Data is IEnumerable <IMusicable> songs)
                    {
                        Settings.settings.LikeMusic(songs);
                        string message = songs.Count() == 1 ? Helper.LocalizeMessage("SongAddedTo", songs.ElementAt(0).ToMusic().Name, MyFavorites) :
                                         Helper.LocalizeMessage("SongsAddedTo", songs.Count(), MyFavorites);
                        Helper.ShowCancelableNotificationWithoutLocalization(message, () =>
                        {
                            foreach (var song in songs)
                            {
                                Settings.settings.DislikeMusic(song.ToMusic());
                            }
                        });
                    }
                    else
                    {
                        return;
                    }
                    listener?.Favorite(Data);
                };
                addToItem.Items.Add(favItem);
            }
            if (addToItem.Items.Count > 0)
            {
                addToItem.Items.Add(new MenuFlyoutSeparator());
            }
            foreach (var item in GetAddToPlaylistsMenuFlyout(listener).Items)
            {
                addToItem.Items.Add(item);
            }
            return(addToItem);
        }
Пример #12
0
 public async void MusicSwitching(Music current, Music next, MediaPlaybackItemChangedReason reason)
 {
     await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => MediaHelper.FindMusicAndSetPlaying(Songs, current, next));
 }