/// <summary> /// 更改音乐播放状态 /// </summary> /// <param name="musicViewModel"></param> /// <param name="tb"></param> private void ChangeMusicPlayState(QMInfoViewModel musicViewModel, TextBlock tb) { if (musicViewModel != null) { _nextPlayingTbObject = new KeyValuePair <string, TextBlock>(musicViewModel.Id, tb); var isToPlay = true; if (tb != null) { isToPlay = !tb.Text.Equals("\ue69d"); } Task.Factory.StartNew(() => { var htmlWeb = new HtmlWeb() { BrowserTimeout = TimeSpan.FromSeconds(10) }; var doc = htmlWeb.Load(musicViewModel.SourcePath); var photoUrl = doc.DocumentNode.SelectNodes("//img[@class='data__photo']")?.FirstOrDefault()?.Attributes["src"].Value?.ToHttpUrl(); var musicInfo = new QMPlayInfoModel() { Id = musicViewModel.Id, Name = musicViewModel.Name, SingerName = musicViewModel.SingerName, TimeDuration = musicViewModel.TimeDuration, Uri = new Uri($"http://thirdparty.gtimg.com/C100{musicViewModel.Id}.m4a?fromtag=38", UriKind.Absolute), PhotoUri = photoUrl == null ? null : new Uri(photoUrl) }; MusicPlayStateChangedUtil.InvokeFromQM(musicInfo, isToPlay); }); } }
/// <summary> /// 歌曲播放失败 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MediaPlayer_MediaFailed(object sender, ExceptionEventArgs e) { _isOpenFailedOfCurrentPlayingMusic = true; //为了同步播放按钮的状态 if (_musicSourceType == MusicSourceType.JM) { MusicPlayStateChangedUtil.InvokeFromJM(null, false, false); } else { MusicPlayStateChangedUtil.InvokeFromQM(null, false, false); } JMMessageBox.Show("歌曲失效", "因歌曲文件失效导致播放失败,请欣赏其他歌曲", JMMessageBoxButtonType.OK, JMMessageBoxIconType.Error); }
private void MediaPlayer_MediaOpened(object sender, EventArgs e) { if (_musicSourceType == MusicSourceType.JM) { MusicPlayStateChangedUtil.InvokeFromJM(null, true, false); } else { MusicPlayStateChangedUtil.InvokeFromQM(null, true, false); } MusicProgressSlider.Maximum = _mediaPlayer.NaturalDuration.TimeSpan.TotalSeconds; SliderVolume.Maximum = _mediaPlayer.Volume; _isOpenFailedOfCurrentPlayingMusic = false; }
private async void Song_Click(object sender, RoutedEventArgs e) { var id = Convert.ToInt32((sender as FrameworkElement).Tag); var song = await Task.Factory.StartNew(() => { return(_musicService.GetMusicById(id)); }); var model = new MusicModel() { Id = song.Id, SingerId = song.SingerId, AlbumId = song.AlbumId, Name = song.Name, SingerName = song.SingerName, AlbumName = song.AlbumName, FileUri = new Uri(song.FileUrl, UriKind.Relative), Duration = song.Duration }; JMApp.CurrentPlayingMusicsInfo = new CurrentPlayingMusicsInfo() { PlayingListMusics = new List <MusicModel>() { model }, IsCurrentPlayingPage = true, }; if (JMApp.CurrentPlayingMusicsInfo.CurrentPlayingMusic?.Id.Equals(id) != true) { JMApp.CurrentPlayingMusicsInfo.CurrentPlayingMusic = model; } var isToPlay = _currentPlayingMusicId != model.Id; await Task.Factory.StartNew(() => MusicPlayStateChangedUtil.InvokeFromJM(model, isToPlay)); _currentPlayingMusicId = isToPlay ? model.Id : 0; }
/// <summary> /// 播放按钮点击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnPlay_Click(object sender, RoutedEventArgs e) { if (_musicSourceType.Equals(MusicSourceType.JM)) { if (_mediaPlayer.Source == null) { PlayNewMusic(_defaultMusicUri); _musicPlayerMenuViewModel.MusicName = "好想再爱你"; _musicPlayerMenuViewModel.SingerName = "潘广益"; _musicPlayerMenuViewModel.PhotoUri = _defaultPhotoUri; _musicSourceType = MusicSourceType.JM; } else { MusicPlayStateChangedUtil.InvokeFromJM(null, !_isPlaying, false); } } else if (_musicSourceType == MusicSourceType.QM) { MusicPlayStateChangedUtil.InvokeFromQM(null, !_isPlaying, false); } }