Workaround for a bug with stopped SoundSource not being able to resume playing
authorChristoph Sommer <mail@christoph-sommer.de>
Fri, 26 May 2006 12:19:15 +0000 (12:19 +0000)
committerChristoph Sommer <mail@christoph-sommer.de>
Fri, 26 May 2006 12:19:15 +0000 (12:19 +0000)
SVN-Revision: 3591

src/audio/managed_sound_source.cpp
src/audio/managed_sound_source.hpp

index 3644298..167608f 100644 (file)
 #include "log.hpp"
 
 ManagedSoundSource::ManagedSoundSource(SoundManager* sound_manager, const std::string& name)
-  : name(name), soundManager(sound_manager)
+  : name(name), soundSource(0), soundManager(sound_manager)
 {
-  soundSource = soundManager->create_sound_source(name);
-  if(!soundSource) {
-    log_warning << "Couldn't create managed sound source for \"" << name << "\"" << std::endl;
-    return;
-  }
 }
 
 ManagedSoundSource::ManagedSoundSource(const ManagedSoundSource& other) 
-       : name(other.name), soundManager(other.soundManager)
+       : name(other.name), soundSource(0), soundManager(other.soundManager)
 {
-  soundSource = soundManager->create_sound_source(name);
 }
 
 ManagedSoundSource::~ManagedSoundSource()
@@ -43,3 +37,44 @@ ManagedSoundSource::~ManagedSoundSource()
   delete soundSource;
 }
 
+bool
+ManagedSoundSource::preload()
+{
+  if (soundSource) return true;
+  soundSource = soundManager->create_sound_source(name);
+  return (soundSource != 0);
+}
+
+void
+ManagedSoundSource::release()
+{
+  if (!soundSource) return;
+  if (playing()) soundSource->stop();
+  delete soundSource;
+  soundSource = 0;
+}
+
+void
+ManagedSoundSource::play()
+{
+  if (!preload()) {
+    log_warning << "Couldn't play \"" << name << "\"" << std::endl;
+    return;
+  }
+  soundSource->play();
+}
+
+void
+ManagedSoundSource::stop()
+{
+  // FIXME: calling release() instead of stop() seems necessary due to an unconfirmed sound bug
+  release();
+}
+
+bool
+ManagedSoundSource::playing()
+{
+  if (!soundSource) return false;
+  return soundSource->playing();
+}
+
index d22b72a..cebf6ad 100644 (file)
@@ -40,17 +40,17 @@ public:
    * pre-loads SoundSource and indicates success.
    * If pre-loading failed, the SoundSource will be loaded at first use
    */
-  bool preload() { return (soundSource != 0); /* TODO: implement */ }
+  bool preload();
 
   /**
    * stops playing and temporarily releases memory for the SoundSource.
    * Memory will be re-allocated on next use
    */
-  void release() { if (playing()) stop(); /* TODO: implement*/ }
+  void release();
 
-  void play() { if (preload()) soundSource->play(); }
-  void stop() { if (preload()) soundSource->stop(); }
-  bool playing() { return ((preload())?(soundSource->playing()):(false)); }
+  void play();
+  void stop();
+  bool playing();
   void set_looping(bool looping) { if (preload()) soundSource->set_looping(looping); }
   void set_gain(float gain) { if (preload()) soundSource->set_gain(gain); }
   void set_pitch(float pitch) { if (preload()) soundSource->set_pitch(pitch); }