/// <summary> /// Popup a dialog for adding music to a collection. /// </summary> private async void BtnLike_Click(object sender, RoutedEventArgs e) { var entry = BeatmapQuery.FilterByIdentity(InstanceManage.GetInstance <PlayerList>().CurrentIdentity); //var entry = App.PlayerList?.CurrentInfo.Beatmap; if (entry == null) { MsgBox.Show(this, "该图不存在于该osu!db中。", Title, MessageBoxButton.OK, MessageBoxImage.Exclamation); return; } if (!ViewModel.IsMiniMode) { FramePop.Navigate(new SelectCollectionPage(this, entry)); } else { var collection = DbOperate.GetCollections().First(k => k.Locked); if (InstanceManage.GetInstance <PlayerList>().CurrentInfo.IsFavorite) { DbOperate.RemoveMapFromCollection(entry, collection); InstanceManage.GetInstance <PlayerList>().CurrentInfo.IsFavorite = false; } else { await SelectCollectionPage.AddToCollectionAsync(collection, entry); InstanceManage.GetInstance <PlayerList>().CurrentInfo.IsFavorite = true; } } IsMapFavorite(InstanceManage.GetInstance <PlayerList>().CurrentInfo.Identity); }
/// <summary> /// Play a new file by file path. /// </summary> private async Task PlayNewFile(string path, bool play) { var sw = Stopwatch.StartNew(); var playerInst = InstanceManage.GetInstance <PlayersInst>(); var dbInst = InstanceManage.GetInstance <OsuDbInst>(); ComponentPlayer audioPlayer = null; if (path == null) { return; } if (File.Exists(path)) { try { var osuFile = await OsuFile.ReadFromFileAsync(path); //50 ms var fi = new FileInfo(path); if (!fi.Exists) { throw new FileNotFoundException("Cannot locate.", fi.FullName); } var dir = fi.Directory.FullName; /* Clear */ ClearHitsoundPlayer(); /* Set new hitsound player*/ playerInst.SetAudioPlayer(path, osuFile); audioPlayer = playerInst.AudioPlayer; SignUpPlayerEvent(audioPlayer); await audioPlayer.InitializeAsync(); //700 ms /* Set Meta */ var nowIdentity = new MapIdentity(fi.Directory.Name, osuFile.Metadata.Version); MapInfo mapInfo = DbOperate.GetMapFromDb(nowIdentity); Beatmap beatmap = BeatmapQuery.FilterByIdentity(nowIdentity); bool isFavorite = IsMapFavorite(mapInfo); //50 ms audioPlayer.HitsoundOffset = mapInfo.Offset; Offset.Value = audioPlayer.HitsoundOffset; var currentInfo = new CurrentInfo( osuFile.Metadata.Artist, osuFile.Metadata.ArtistUnicode, osuFile.Metadata.Title, osuFile.Metadata.TitleUnicode, osuFile.Metadata.Creator, osuFile.Metadata.Source, osuFile.Metadata.TagList, osuFile.Metadata.BeatmapId, osuFile.Metadata.BeatmapSetId, beatmap?.DiffSrNoneStandard ?? 0, osuFile.Difficulty.HpDrainRate, osuFile.Difficulty.CircleSize, osuFile.Difficulty.ApproachRate, osuFile.Difficulty.OverallDifficulty, audioPlayer.Duration, nowIdentity, mapInfo, beatmap, isFavorite); // 20 ms InstanceManage.GetInstance <PlayerList>().CurrentInfo = currentInfo; ViewModel.Player.CurrentInfo = currentInfo; /*start of ui*/ LblTitle.Content = osuFile.Metadata.TitleMeta.ToUnicodeString(); LblArtist.Content = osuFile.Metadata.ArtistMeta.ToUnicodeString(); ((ToolTip)NotifyIcon.TrayToolTip).Content = (string)LblArtist.Content + " - " + (string)LblTitle.Content; /*end of ui*/ /* Set Lyric */ SetLyricSynchronously(); /* Set Progress */ PlayProgress.Maximum = audioPlayer.Duration; PlayProgress.Value = 0; PlayerViewModel.Current.Duration = InstanceManage.GetInstance <PlayersInst>().AudioPlayer.Duration; /* Set Storyboard */ if (true) { // Todo: Set Storyboard } /* Set Video */ bool showVideo = PlayerViewModel.Current.EnableVideo && !ViewModel.IsMiniMode; if (VideoElement != null) { await SafelyRecreateVideoElement(showVideo); if (showVideo) { var videoName = osuFile.Events.VideoInfo?.Filename; if (videoName == null) { VideoElement.Source = null; VideoElementBorder.Visibility = Visibility.Hidden; } else { var vPath = Path.Combine(dir, videoName); if (File.Exists(vPath)) { VideoElement.Source = new Uri(vPath); _videoOffset = -(osuFile.Events.VideoInfo.Offset); if (_videoOffset >= 0) { _waitAction = () => { }; _position = TimeSpan.FromMilliseconds(_videoOffset); } else { _waitAction = () => { Thread.Sleep(TimeSpan.FromMilliseconds(-_videoOffset)); }; } } else { VideoElement.Source = null; VideoElementBorder.Visibility = Visibility.Hidden; } } } } /* Set Background */ if (osuFile.Events.BackgroundInfo != null) { var bgPath = Path.Combine(dir, osuFile.Events.BackgroundInfo.Filename); BlurScene.Source = File.Exists(bgPath) ? new BitmapImage(new Uri(bgPath)) : null; Thumb.Source = File.Exists(bgPath) ? new BitmapImage(new Uri(bgPath)) : null; } else { BlurScene.Source = null; } /* Start Play */ switch (MainFrame.Content) { case RecentPlayPage recentPlayPage: var item = recentPlayPage.DataModels.FirstOrDefault(k => k.GetIdentity().Equals(nowIdentity)); recentPlayPage.RecentList.SelectedItem = item; break; case CollectionPage collectionPage: collectionPage.MapList.SelectedItem = collectionPage.ViewModel.Beatmaps.FirstOrDefault(k => k.GetIdentity().Equals(nowIdentity)); break; } _videoPlay = play; if (play) { if (showVideo && VideoElement?.Source != null) { // use event to control here. //VideoPlay(); //App.HitsoundPlayer.Play(); } else { audioPlayer.Play(); } } //if (!App.PlayerList.Entries.Any(k => k.GetIdentity().Equals(nowIdentity))) // App.PlayerList.Entries.Add(entry); PlayerConfig.Current.CurrentPath = path; PlayerConfig.SaveCurrent(); DbOperate.UpdateMap(nowIdentity); } catch (Exception ex) { var result = MsgBox.Show(this, @"发生未处理的异常问题:" + (ex.InnerException ?? ex), Title, MessageBoxButton.OK, MessageBoxImage.Error); if (result == MessageBoxResult.OK) { if (audioPlayer == null) { return; } if (audioPlayer.PlayerStatus != PlayerStatus.Playing) { await PlayNextAsync(false, true); } } Console.WriteLine(ex); } } else { MsgBox.Show(this, @"所选文件不存在,可能是db没有及时更新。请关闭此播放器或osu后重试。", Title, MessageBoxButton.OK, MessageBoxImage.Warning); } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); }