private async void loadPlaylists() { try { ObservableCollection <Playlist> lists = await DataIO.ReadPlaylists(); Helper.executeInUiThread(() => { this.selectedPlaylist = lists[0]; this.MainListView.ItemsSource = songListObservable; songListObservable.Clear(); PlaylistList.Items.Clear(); foreach (Playlist pl in lists) { PlaylistList.Items.Add(pl); } this.PlaylistList.SelectedIndex = 0; if (lists[0].Songlist == null) { return; } foreach (Song s in this.selectedPlaylist.Songlist) { this.songListObservable.Add(s); } }); } catch (NullReferenceException) { return; } }
private void Scrollviewer_Loaded(object sender, RoutedEventArgs e) { scrollTimer = new Timer(); scrollTimer.Interval = 50; var forward = true; scrollTimer.Elapsed += (source, args) => { Helper.executeInUiThread(() => { if (forward) { scrollviewer.ScrollToHorizontalOffset(scrollviewer.HorizontalOffset + 0.5); } else { scrollviewer.ScrollToHorizontalOffset(scrollviewer.HorizontalOffset - 0.5); } if (scrollviewer.HorizontalOffset == scrollviewer.ScrollableWidth) { forward = false; } else if (scrollviewer.HorizontalOffset == 0) { forward = true; } }); }; scrollTimer.Start(); }
private async void DeleteButton_Click(object sender, RoutedEventArgs e) { await DataIO.RemovePlaylist(this.selectedPlaylist.Title); Helper.executeInUiThread(() => this.PlaylistList.Items.Clear()); this.loadPlaylists(); }
private async void loadSettings() { var settings = await DataIO.readSettings(); Helper.executeInUiThread(() => { this.VolumeSlider.Value = settings.volumePerc; }); }
public void onTimerEvent(object source, ElapsedEventArgs e) { if (this.mPlayer.PlaybackSession.PlaybackState == MediaPlaybackState.Playing) { Helper.executeInUiThread(() => { this.currentTime.Text = $"{this.mPlayer.PlaybackSession.Position.Minutes}:{this.mPlayer.PlaybackSession.Position.Seconds.ToString("D2")}"; this.currentDuration.Text = $"{this.mPlayer.PlaybackSession.NaturalDuration.Minutes}:{this.mPlayer.PlaybackSession.NaturalDuration.Seconds.ToString("D2")}"; try { this.timeSlider.Value = this.mPlayer.PlaybackSession.Position.TotalSeconds / (this.mPlayer.PlaybackSession.NaturalDuration.TotalSeconds / 100.0); } catch (Exception) { } }); } }
public static async void ErrorDialog(string title, string content) { Helper.executeInUiThread(async() => { ContentDialog dialog = new ContentDialog() { Title = title, Content = content, CloseButtonText = "Ok" }; try { await dialog.ShowAsync(); } catch (Exception) { }; }); }
public void skipSong(bool direction = true) { if (currentPlaylist == null) { return; } if (random) { var randInt = new Random().Next(0, currentPlaylist.Songlist.Count); this.currentSong = currentPlaylist.Songlist[randInt]; this.Play(); this.MainListView.SelectedItem = currentPlaylist.Songlist[randInt]; return; } var temp = currentSong; var i = currentPlaylist.Songlist.IndexOf(currentPlaylist.Songlist.Where(x => x == temp).FirstOrDefault()); //forward if (direction) { if (i == currentPlaylist.Songlist.Count - 1) { i = -1; } this.currentSong = currentPlaylist.Songlist[++i]; this.Play(); } //backward else { if (i == 0) { i = 1; } this.currentSong = currentPlaylist.Songlist[--i]; this.Play(); } currentSong = currentPlaylist.Songlist[i]; temp = currentSong; Helper.executeInUiThread(() => { this.MainListView.SelectedItem = temp; }); }
public MusicPlayer(TextBlock CurrentDuration, TextBlock CurrentTime, Slider TimeSlider, ListView mainList, Button playButton, Rectangle currRect, TextBlock currLabel, StackPanel startlogostackpanel) { this.currentDuration = CurrentDuration; this.currentTime = CurrentTime; this.timeSlider = TimeSlider; this.MainListView = mainList; this.playButton = playButton; this.currPlayingRect = currRect; this.currPlayingLabel = currLabel; this.StartLogoStackPanel = startlogostackpanel; this.mPlayer.MediaEnded += (sender, eventArgs) => { if (!loop) { skipSong(true); } else { this.Play(); } }; this.mPlayer.CurrentStateChanged += (player, obj) => { Helper.executeInUiThread(() => { if (player.PlaybackSession.PlaybackState == MediaPlaybackState.Paused || player.PlaybackSession.PlaybackState == MediaPlaybackState.Opening || player.PlaybackSession.PlaybackState == MediaPlaybackState.None) { this.playButton.Content = "\uE768"; } else { this.playButton.Content = "\uE769"; } }); switch (this.mPlayer.PlaybackSession.PlaybackState) { case MediaPlaybackState.Playing: smtc.PlaybackStatus = MediaPlaybackStatus.Playing; break; case MediaPlaybackState.Paused: smtc.PlaybackStatus = MediaPlaybackStatus.Paused; break; default: break; } }; this.smtc = SystemMediaTransportControls.GetForCurrentView(); this.smtc.IsNextEnabled = false; this.smtc.IsPlayEnabled = false; this.smtc.IsPauseEnabled = false; this.smtc.IsPreviousEnabled = false; this.smtc.ButtonPressed += Smtc_ButtonPressed; this.clock.Elapsed += new ElapsedEventHandler(onTimerEvent); this.clock.Interval = 500; this.clock.Enabled = true; }
public async void Play() { if (currentSong == null && currentPlaylist == null) { return; } if (currentPlaylist != null && currentSong == null) { this.currentSong = currentPlaylist.Songlist.FirstOrDefault(); } if (currentSong.isDownloaded) { try { StorageFolder sf = await StorageFolder.GetFolderFromPathAsync(currentSong.downloadPath); var storageFile = await sf.GetFileAsync(currentSong.DownloadTitle + ".mp3"); this.mPlayer.Source = MediaSource.CreateFromStorageFile(storageFile); this.mPlayer.Play(); } catch (FileNotFoundException) { Helper.ErrorDialog("Song not found!", "File was probably deleted. Please download it again."); var i = currentPlaylist.Songlist.IndexOf(currentSong); currentSong.isDownloaded = false; currentPlaylist.Songlist.RemoveAt(i); currentPlaylist.Songlist.Insert(i, currentSong); await DataIO.SavePlaylist(currentPlaylist); skipSong(); //TODO: update UI return; } } else { //if (!Helper.checkIfOnline()) // return; try { var stream = await this.GetAudioStream(currentSong.SongURL); this.mPlayer.Source = MediaSource.CreateFromUri(new Uri(stream)); this.mPlayer.Play(); } catch (System.Net.Http.HttpRequestException) { return; } catch (NullReferenceException) { //happens if "video" is not playable for some reason Helper.ErrorDialog("Cant Play this Song", "For some reason this song can't be played."); skipSong(); return; } } Helper.executeInUiThread(() => { this.StartLogoStackPanel.Visibility = Windows.UI.Xaml.Visibility.Collapsed; this.currPlayingRect.Fill = new ImageBrush { ImageSource = Helper.base64toBmp(currentSong.Thumbnail) }; this.currPlayingLabel.Text = currentSong.Title; }); this.smtc.IsNextEnabled = true; this.smtc.IsPlayEnabled = true; this.smtc.IsPauseEnabled = true; this.smtc.IsPreviousEnabled = true; this.smtc.DisplayUpdater.Thumbnail = RandomAccessStreamReference.CreateFromStream(await Helper.base64toStream(currentSong.Thumbnail)); this.smtc.DisplayUpdater.Update(); }