OSDN Git Service

* Wait for fmod initialization to complete before exiting constructor
[radegast/radegast.git] / Radegast / Core / Media / BufferSound.cs
index fca9e9b..41adf77 100644 (file)
@@ -114,7 +114,9 @@ namespace Radegast.Media
             if (allBuffers.ContainsKey(id))\r
             {\r
                 BufferSound bs = allBuffers[id];\r
-                bs.StopSound();\r
+                ManualResetEvent done = new ManualResetEvent(false);\r
+                bs.StopSound(done);\r
+                done.WaitOne();\r
             }\r
         }\r
 \r
@@ -204,11 +206,11 @@ namespace Radegast.Media
                 invoke(new SoundDelegate(delegate\r
                 {\r
                     // Create an FMOD sound of this Ogg data.\r
-                    RESULT status = system.createSound(\r
+                    FMODExec(system.createSound(\r
                         data,\r
                         mode,\r
                         ref extraInfo,\r
-                        ref sound);\r
+                        ref sound));\r
 \r
                     // Register for callbacks.\r
                     RegisterSound(sound);\r
@@ -223,6 +225,9 @@ namespace Radegast.Media
 \r
         protected override RESULT NonBlockCallbackHandler(RESULT instatus)\r
         {\r
+            // Check if we killed the sound before it was decoded\r
+            if (sound == null) return RESULT.OK;\r
+\r
             if (instatus != RESULT.OK)\r
             {\r
                 Logger.Log("Error opening sound: " + instatus, Helpers.LogLevel.Error);\r
@@ -291,6 +296,11 @@ namespace Radegast.Media
 \r
         protected void StopSound()\r
         {\r
+            StopSound(null);\r
+        }\r
+\r
+        protected void StopSound(ManualResetEvent signal)\r
+        {\r
             finished = true;\r
 \r
             invoke(new SoundDelegate(delegate\r
@@ -301,7 +311,7 @@ namespace Radegast.Media
                 // Release the buffer to avoid a big memory leak.\r
                 if (channel != null)\r
                 {\r
-                    lock(allChannels)\r
+                    lock (allChannels)\r
                         allChannels.Remove(channel.getRaw());\r
                     chanStr = channel.getRaw().ToString("X");\r
                     channel.stop();\r
@@ -323,6 +333,9 @@ namespace Radegast.Media
 \r
                 lock (allBuffers)\r
                     allBuffers.Remove(ContainerId);\r
+\r
+                if (signal != null)\r
+                    signal.Set();\r
             }));\r
 \r
         }\r