private static void MediaPlayerOnMediaFailed(object sender, Exception e) { if (CurrentAudio != null) { LoggingService.Log("Media failed " + CurrentAudio.Id + " " + CurrentAudio.Artist + " - " + CurrentAudio.Title + ". " + e); } if (e is InvalidWmpVersionException) { var flyout = new FlyoutControl(); flyout.FlyoutContent = new CommonMessageView() { Header = ErrorResources.AudioFailedErrorHeaderCommon, Message = ErrorResources.WmpMissingError }; flyout.Show(); return; } if (e is COMException) { var com = (COMException)e; if ((uint)com.ErrorCode == 0xC00D0035) //not found or connection problem { var flyout = new FlyoutControl(); flyout.FlyoutContent = new CommonMessageView() { Header = ErrorResources.AudioFailedErrorHeaderCommon, Message = ErrorResources.WmpMissingError }; flyout.Show(); return; } } _playFailsCount++; if (_playFailsCount < 5) { if (e is FileNotFoundException && CurrentAudio is VkAudio) { CurrentAudio.Source = null; PlayInternal(CurrentAudio, _cancellationToken.Token); } else { if (RadioService.CurrentRadio == null) { Next(); } else { RadioService.InvalidateCurrentSong(); } } } }
/// <summary> /// Переключиться на следующий трек. Обычно вызывается автоматически при окончании текущего трека. /// </summary> public static void SwitchNext() { if (RadioService.CurrentRadio == null) { Next(); } else { RadioService.SwitchNext(); } }
public static Task Load() { return(Task.Run(() => { try { if (!File.Exists("currentPlaylist.js")) { return; } var json = File.ReadAllText("currentPlaylist.js"); if (string.IsNullOrEmpty(json)) { return; } var o = JObject.Parse(json); if (o["currentAudio"] != null) { var audio = JsonConvert.DeserializeObject <Audio>(o["currentAudio"].ToString(), new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Objects }); Application.Current.Dispatcher.Invoke(() => CurrentAudio = audio); } if (o["currentPlaylist"] != null) { var playlist = JsonConvert.DeserializeObject <List <object> >(o["currentPlaylist"].ToString(), new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Objects }); if (playlist != null) { Application.Current.Dispatcher.Invoke(() => SetCurrentPlaylist(playlist.OfType <Audio>())); } } if (o["currentRadio"] != null && o["currentRadio"]["session"] != null) { var session = o["currentRadio"]["session"].Value <string>(); var radio = JsonConvert.DeserializeObject <RadioStation>(o["currentRadio"]["radio"].ToString()); RadioService.RestoreSession(session, radio); } } catch (Exception ex) { LoggingService.Log(ex); } })); }
public static void SetCurrentPlaylist(IEnumerable <Audio> playlist, bool radio = false) { if (playlist == null) { Playlist?.Clear(); } else { Playlist = new ObservableCollection <Audio>(playlist); } if (!radio) { RadioService.Stop(); } }
/// <summary> /// Перейти к следующему треку. Обычно вызывается при нажатии пользователем кнопки Next. /// </summary> public static void SkipNext() { //если прошло больше 1/3 трека, считаем, что трек послушали полностью if (CurrentAudioPosition.TotalSeconds > CurrentAudioDuration.TotalSeconds / 3) { SwitchNext(); } else if (RadioService.CurrentRadio == null) { Next(true); } else { RadioService.SkipNext(); } }
private async static void PlayInternal(Audio track, CancellationToken token) { if (CurrentAudio != null) { CurrentAudio.IsPlaying = false; Stop(); } track.IsPlaying = true; CurrentAudio = track; if (track.Source == null) { VkAudio vkAudio = null; try { vkAudio = await DataService.GetAudioByArtistAndTitle(track.Artist, track.Title); } catch (Exception ex) { LoggingService.Log(ex); } if (vkAudio != null) { vkAudio.IsPlaying = true; if (_playlist != null) { var playlistTrackIndex = _playlist.IndexOf(track); if (playlistTrackIndex >= 0) { var playlistTrack = (VkAudio)_playlist[_playlist.IndexOf(track)]; playlistTrack.Id = vkAudio.Id; playlistTrack.Source = vkAudio.Source; playlistTrack.OwnerId = vkAudio.OwnerId; playlistTrack.IsAddedByCurrentUser = vkAudio.IsAddedByCurrentUser; playlistTrack.AlbumId = vkAudio.AlbumId; playlistTrack.Title = vkAudio.Title; playlistTrack.Artist = vkAudio.Artist; playlistTrack.Duration = vkAudio.Duration; playlistTrack.GenreId = vkAudio.GenreId; playlistTrack.LyricsId = vkAudio.LyricsId; //_playlist[_playlist.IndexOf(track)] = vkAudio; //to fix radio vk scrobbling } } track = vkAudio; //_currentAudio = track; _playFailsCount = 0; } else { LoggingService.Log("Failed to find audio " + track.Artist + " - " + track.Title); _playFailsCount++; if (_playFailsCount > 5) { return; } if (RadioService.CurrentRadio == null) { Next(); } else { RadioService.InvalidateCurrentSong(); } return; } } #if DEBUG LoggingService.Log(string.Format("Playing: {0} {1} {2} {3}", track.Id, track.Artist, track.Title, track.Source)); #endif if (token.IsCancellationRequested) { return; } track.IsPlaying = true; //look like MediaElement doen't work with https, temporary hack var url = track.Source; if (!string.IsNullOrEmpty(url)) { if (!Settings.Instance.UseHttps) { url = url.Replace("https://", "http://"); } MediaPlayer.Source = new Uri(url); MediaPlayer.Play(); State = PlayerPlayState.Playing; } }