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