From 33033b0c961d2dbb122116a4670536d93a309179 Mon Sep 17 00:00:00 2001 From: faraphel Date: Tue, 22 Oct 2024 12:04:05 +0200 Subject: [PATCH] added a small fix : if a new audio shall be played now, immediately stop the previous audio and play the new one instead --- source/Client.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/source/Client.cpp b/source/Client.cpp index 36f22c6..6d36252 100644 --- a/source/Client.cpp +++ b/source/Client.cpp @@ -133,9 +133,16 @@ void Client::loopPlayer() { std::cout << "[Client] Playing: " << audioPacket.timePlay << std::endl; - std::cout << "playing: " << audioPacket.timePlay << std::endl; + // immediately stop playing music + // this avoids an offset created if this client's clock is too ahead of the others + // don't handle errors since audio might not be playing before + Pa_AbortStream(this->stream); - // play the audio buffer + // play the new audio data + if (const int error = Pa_StartStream(this->stream) != paNoError) + throw std::runtime_error("[Client] Could not start the PortAudio stream: " + std::string(Pa_GetErrorText(error))); + + // write the new audio data into the audio buffer const int error = Pa_WriteStream( this->stream, audioPacket.content.data(),