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(); } } } }
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; } }