public async Task StartAsync() { while (true) { if (Session.State == MediaSessionState.Authenticated && Tracks.Count > 0) { MusicTrack currentTrack = Tracks[0]; if (currentTrack.DownloadTask != null) { await currentTrack.DownloadTask; } if (CurrentTrackStream == null) { CurrentTrackStream = DiscordVoiceUtils.GetAudioStream(currentTrack.FilePath); } try { SessionStream.CopyFrom(CurrentTrackStream); Tracks.RemoveAt(0); CurrentTrackStream.Flush(); CurrentTrackStream = null; } catch { } } else { await Task.Delay(100); } } }
private bool GetNextBatch(CancellationToken token) { if (CurrentTrackStream != null && CurrentTrackStream.MoveNext(token)) { var tracks = CurrentTrackStream.Current; foreach (var track in tracks) { _logger.Log("Adding " + track.Name + " - " + track.Artist + " to playlist", Category.Debug, Priority.Low); _trackQueue.Enqueue(track); _dispatcher.BeginInvoke(new Action <Track>(_trackQueuePublic.Add), track); } RaisePropertyChanged("CanGoToNextTrackStream"); return(true); } ITrackStream nextTrackStream; if (_trackStreamQueue.TryDequeue(out nextTrackStream)) { _logger.Log("Changing current track stream to " + nextTrackStream.Description, Category.Debug, Priority.Low); _dispatcher.BeginInvoke(new Func <ITrackStream, bool>(_trackStreamQueuePublic.Remove), nextTrackStream); CurrentTrackStream = nextTrackStream; RaisePropertyChanged("CanGoToNextTrackStream"); return(GetNextBatch(token)); } _logger.Log("No more track streams to play", Category.Debug, Priority.Low); CurrentTrackStream = null; RaisePropertyChanged("CanGoToNextTrackStream"); return(false); }