[iagno] Use GSound.



commit effaa989041c7dcd3e812bb19cacca70d27b11f8
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Wed Jul 24 15:28:03 2019 +0200

    Use GSound.
    
    Instead of Canberra.
    
    Fixes #9.

 meson.build     |  3 +--
 src/iagno.vala  | 51 +++++++++++++++++++++++++++++++++++++++++++--------
 src/meson.build |  3 +--
 3 files changed, 45 insertions(+), 12 deletions(-)
---
diff --git a/meson.build b/meson.build
index d7d7b70..f9fa69d 100644
--- a/meson.build
+++ b/meson.build
@@ -12,10 +12,9 @@ python3 = import('python3')
 cc = meson.get_compiler('c')
 valac = meson.get_compiler('vala')
 
-canberra_dependency = dependency('libcanberra')
-canberra_gtk3_dependency = dependency('libcanberra-gtk3', version: '>= 0.26')
 gio_dependency = dependency('gio-2.0', version: '>= 2.40.0')
 glib_dependency = dependency('glib-2.0', version: '>= 2.40.0')
+gsound_dependency = dependency('gsound', version: '>= 1.0.2')
 gtk_dependency = dependency('gtk+-3.0', version: '>= 3.24.0')
 libm_dependency = cc.find_library('m', required: false) # some platforms do not have libm separated from libc
 posix_dependency = valac.find_library('posix')
diff --git a/src/iagno.vala b/src/iagno.vala
index ee39c7b..01ca287 100644
--- a/src/iagno.vala
+++ b/src/iagno.vala
@@ -220,6 +220,9 @@ private class Iagno : Gtk.Application
         view.scoreboard = scoredrawing;
         view.theme = settings.get_string ("theme");
 
+        if (settings.get_boolean ("sound"))
+            init_sound ();
+
         /* Window */
         window = new GameWindow ("/org/gnome/Reversi/ui/iagno.css",
                                  PROGRAM_NAME,
@@ -549,19 +552,50 @@ private class Iagno : Gtk.Application
     * * Sound
     \*/
 
+    private GSound.Context sound_context;
+    private SoundContextState sound_context_state = SoundContextState.INITIAL;
+
     private enum Sound
     {
         FLIP,
         GAMEOVER;
     }
 
+    private enum SoundContextState
+    {
+        INITIAL,
+        WORKING,
+        ERRORED
+    }
+
+    private void init_sound ()
+     // requires (sound_context_state == SoundContextState.INITIAL)
+    {
+        try
+        {
+            sound_context = new GSound.Context ();
+            sound_context_state = SoundContextState.WORKING;
+        }
+        catch (Error e)
+        {
+            warning (e.message);
+            sound_context_state = SoundContextState.ERRORED;
+        }
+    }
+
     private void play_sound (Sound sound)
     {
         if (settings.get_boolean ("sound"))
-            _play_sound (sound, ref view);
+        {
+            if (sound_context_state == SoundContextState.INITIAL)
+                init_sound ();
+            if (sound_context_state == SoundContextState.WORKING)
+                _play_sound (sound, sound_context, ref view);
+        }
     }
 
-    private static void _play_sound (Sound sound, ref GameView view)
+    private static void _play_sound (Sound sound, GSound.Context sound_context, ref GameView view)
+     // requires (sound_context_state == SoundContextState.WORKING)
     {
         string name;
         switch (sound)
@@ -576,13 +610,14 @@ private class Iagno : Gtk.Application
                 return;
         }
         string path = Path.build_filename (SOUND_DIRECTORY, name);
-        int r = CanberraGtk.play_for_widget (view, 0,
-                                             Canberra.PROP_MEDIA_NAME, name,
-                                             Canberra.PROP_MEDIA_FILENAME, path);
-        if (r != 0)
+        try
+        {
+            sound_context.play_simple (null, GSound.Attribute.MEDIA_NAME, name,
+                                             GSound.Attribute.MEDIA_FILENAME, path);
+        }
+        catch (Error e)
         {
-            string? error = Canberra.strerror (r);
-            warning ("Error playing %s: %s", path, error ?? "unknown error");
+            warning (e.message);
         }
     }
 }
diff --git a/src/meson.build b/src/meson.build
index 9e1f403..8835d4b 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -38,10 +38,9 @@ executable(meson.project_name(),
         '--enable-experimental-non-null'
     ],
     dependencies: [
-        canberra_dependency,
-        canberra_gtk3_dependency,
         gio_dependency,
         glib_dependency,
+        gsound_dependency,
         gtk_dependency,
         libm_dependency,
         posix_dependency,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]