I started to work on an issue that has been bugging me for quiet some time now:
issue #203. The issue is, that if a music is paused or stopped and you call setPlayingOffset() on it the music gets restarted.
The problem is not as simple as it might first look. My solution to the problem was to simply get the status of the music at the beginning of the function and restore it at the end. I tryed this:
void SoundStream::setPlayingOffset(Time timeOffset)
{
// Get old playing status
Status oldStatus = getStatus();
// Stop the stream
stop();
// Let the derived class update the current position
onSeek(timeOffset);
// Calculate new position
m_samplesProcessed = static_cast<Uint64>(timeOffset.asSeconds() * m_sampleRate * m_channelCount);
// Play
m_isStreaming = true;
m_thread.launch();
// Restore to old status
if(oldStatus == Stopped)
stop(); // stop
else if(oldStatus == Paused)
pause(); // paused
}
The problem with this code is that it's correct, but it doesn't work as expected, because if the music is paused it still restarts. After some investigation i found out, this is because the pause command is executed faster than the
play command in the threaded function. So the music is actually paused, but played right afterwards. If I insert a
sleep(milliseconds(20)); before restoring the old state (which is obviously not an option) everything works as expected.
I also tried this:
// Calculate new position
m_samplesProcessed = static_cast<Uint64>(timeOffset.asSeconds() * m_sampleRate * m_channelCount);
// Restore to old status
if(oldStatus != Stopped)
{
m_isStreaming = true; // paused
if(oldStatus == Playing)
m_thread.launch(); // played
}
At first it seemed to work fine, but when the music was in a paused state it is not possible to resume it afterwards, because the thread is never actually launched...
So I am a little stuck now and not really sure how to continue from here. So I thought I share what I found so far and ask if somebody has another idea. Because this seems like an annoying bug, that should be fixed
Anyways I have a debugging environment set up and am happy to help.
Foaly