public void SetAudioClient(IAudioClient client) { AudioClient = client; if (DiscordStream != null) { DiscordStream.Flush(); DiscordStream.Dispose(); DiscordStream.Close(); } DiscordStream = client.CreatePCMStream(AudioApplication.Music); }
private void Dispose() { DiscordStream.Flush(); if (PlayingTrack != null) { PlayingTrack.SourceStream.Flush(); PlayingTrack.SourceStream.Dispose(); PlayingTrack.SourceStream.Close(); PlayingTrack.FFmpegProcess.Dispose(); PlayingTrack.FFmpegProcess.Close(); } //PlayingTrack = null; }
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(); }); }
private void StartAudioStream() { isFinishedPlaying = false; isStopped = false; isPaused = false; audioEvent.OnTrackStart(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 = PlayingTrack.SourceStream.Read(buffer, 0, buffer.Length); if (read > 0) { // Write queued Buffers to DiscordStream. if (isVolumeAdjusted) { DiscordStream.Write(AdjustVolume(buffer, Volume), 0, read); } else { DiscordStream.Write(buffer, 0, read); } //Thread.Sleep(10); } else { isFinishedPlaying = true; isStopped = true; } } // Paused if (isPaused && !isStopped && !isFinishedPlaying) { Task.Delay(2000); continue; } } // If finished playing or stopped: OnTrackEnd DiscordStream.Flush(); audioEvent.OnTrackEnd(PlayingTrack); }