From 74e425a9ed392e537a5b17330ee792311d303c91 Mon Sep 17 00:00:00 2001 From: binary1248 Date: Fri, 4 Jul 2014 22:24:48 +0200 Subject: [PATCH] Made sure SoundStream adhered to its documented behavior, added a hint to SoundStream and Sound documentation regarding setting the offset while stopped. --- include/SFML/Audio/Sound.hpp | 4 +++- include/SFML/Audio/SoundStream.hpp | 4 +++- src/SFML/Audio/SoundStream.cpp | 37 ++++++++++++++++++++++++------ 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/include/SFML/Audio/Sound.hpp b/include/SFML/Audio/Sound.hpp index 74b88269f..6c346ba02 100644 --- a/include/SFML/Audio/Sound.hpp +++ b/include/SFML/Audio/Sound.hpp @@ -144,7 +144,9 @@ public : /// \brief Change the current playing position of the sound /// /// The playing position can be changed when the sound is - /// either paused or playing. + /// either paused or playing. Changing the playing position + /// when the sound is stopped has no effect, since playing + /// the sound would reset its position. /// /// \param timeOffset New playing position, from the beginning of the sound /// diff --git a/include/SFML/Audio/SoundStream.hpp b/include/SFML/Audio/SoundStream.hpp index 293623e42..de36f6202 100644 --- a/include/SFML/Audio/SoundStream.hpp +++ b/include/SFML/Audio/SoundStream.hpp @@ -132,7 +132,9 @@ public : /// \brief Change the current playing position of the stream /// /// The playing position can be changed when the stream is - /// either paused or playing. + /// either paused or playing. Changing the playing position + /// when the stream is stopped has no effect, since playing + /// the stream would reset its position. /// /// \param timeOffset New playing position, from the beginning of the stream /// diff --git a/src/SFML/Audio/SoundStream.cpp b/src/SFML/Audio/SoundStream.cpp index d21b36055..1c37d3ee5 100644 --- a/src/SFML/Audio/SoundStream.cpp +++ b/src/SFML/Audio/SoundStream.cpp @@ -92,17 +92,29 @@ void SoundStream::play() return; } + bool isStreaming = false; + Status threadStartState = Stopped; + { Lock lock(m_threadMutex); - // If the sound is already playing (probably paused), just resume it - if (m_isStreaming) - { - alCheck(alSourcePlay(m_source)); - return; - } + isStreaming = m_isStreaming; + threadStartState = m_threadStartState; + } - m_isStreaming = true; + + if (isStreaming && (threadStartState == Paused)) + { + // If the sound is paused, resume it + Lock lock(m_threadMutex); + m_threadStartState = Playing; + alCheck(alSourcePlay(m_source)); + return; + } + else if (isStreaming && (threadStartState == Playing)) + { + // If the sound is playing, stop it and continue as if it was stopped + stop(); } // Move to the beginning @@ -110,6 +122,7 @@ void SoundStream::play() // Start updating the stream in a separate thread to avoid blocking the application m_samplesProcessed = 0; + m_isStreaming = true; m_threadStartState = Playing; m_thread.launch(); } @@ -121,6 +134,10 @@ void SoundStream::pause() // Handle pause() being called before the thread has started { Lock lock(m_threadMutex); + + if (!m_isStreaming) + return; + m_threadStartState = Paused; } @@ -139,6 +156,12 @@ void SoundStream::stop() // Wait for the thread to terminate m_thread.wait(); + + // Move to the beginning + onSeek(Time::Zero); + + // Reset the playing position + m_samplesProcessed = 0; }