public void PlayTrack(AudioTrack track) { if (audioEvent == null) { audioEvent = new DefaultAudioEventAdapter(); } // handle if still playing, FAIL if (IsPlaying()) { Stop(); Dispose(); } PlayingTrack = track; audioStreamThread = new Thread(StartAudioStream); audioStreamThread.Start(); }
public async Task PlayTrackAsync(AudioTrack track) { if (audioEvent == null) { audioEvent = new DefaultAudioEventAdapter(); } // handle if still playing, FAIL if (IsPlaying()) { Stop(); Dispose(); } PlayingTrack = track; // Load playing track source stream PlayingTrack.SourceStream = PlayingTrack.FFmpegProcess.StandardOutput.BaseStream; await Task.Factory.StartNew(async() => { isFinishedPlaying = false; isStopped = false; isPaused = false; // OnTrackStart Playing here await audioEvent.OnTrackStartAsync(PlayingTrack); byte[] buffer = new byte[164384]; int read; // Playing loop while (!isStopped) { // Event Loop while (!isPaused && !isStopped && !isFinishedPlaying) { if (DiscordStream == null) { audioEvent.OnTrackError(PlayingTrack, new TrackErrorException("Error when playing audio track: Stream gone.")); isFinishedPlaying = true; isStopped = true; Dispose(); PlayingTrack = null; return; } read = await PlayingTrack.SourceStream.ReadAsync(buffer, 0, buffer.Length); if (read > 0) { if (isVolumeAdjusted) { await DiscordStream.WriteAsync(AdjustVolume(buffer, Volume), 0, read); } else { await DiscordStream.WriteAsync(buffer, 0, read); } await Task.Delay(10); } else { isFinishedPlaying = true; isStopped = true; } } // Paused if (isPaused && !isStopped && !isFinishedPlaying) { await Task.Delay(2000); continue; } } await DiscordStream.FlushAsync(); audioEvent.OnTrackEnd(PlayingTrack); //Dispose(); }); }
public void RegisterEventAdapter(IAudioEventAdapter audioEvent) { this.audioEvent = audioEvent; }