[the-board/sound-thing] more sound thing



commit 99cf09992b050932e56ae7a6ee012aeb77493211
Author: Lucas Rocha <lucasr gnome org>
Date:   Wed Jan 5 14:42:31 2011 +0000

    more sound thing

 src/js/ui/things/sound.js |  184 ++++++++++-----------------------------------
 1 files changed, 39 insertions(+), 145 deletions(-)
---
diff --git a/src/js/ui/things/sound.js b/src/js/ui/things/sound.js
index bf9d9fb..50e2fac 100644
--- a/src/js/ui/things/sound.js
+++ b/src/js/ui/things/sound.js
@@ -47,7 +47,7 @@ SoundThing.prototype = {
         this._style.load_from_file(STYLE);
 
         this._createSoundBox();
-        this._createPlaybackBox();
+        this._createSoundPlayer();
         this._createCaptionText();
 
         Thing.Thing.prototype._init.apply(this, [args]);
@@ -66,6 +66,14 @@ SoundThing.prototype = {
         this._createContentBox();
     },
 
+    _createSoundPlayer : function() {
+        this._player = new Tb.SoundPlayer();
+
+        this._player.connect("notify::state",
+                             Lang.bind(this,
+                                       this._onPlayerStateChanged));
+    },
+
     _createCasseteBg : function() {
         this._casseteBg =
             new Clutter.Texture({ filename: _CASSETE_IMAGE });
@@ -87,22 +95,9 @@ SoundThing.prototype = {
 
         this._contentBox.set_style(this._style);
 
-        this._soundBox.append(this._contentBox,
-                                Tb.BoxPackFlags.EXPAND);
-    },
-
-    _createPlaybackBox : function() {
-        this._playbackBox =
-            new Tb.Box({ orientation: Tb.BoxOrientation.VERTICAL,
-                         xAlign: Tb.BoxAlignment.FILL,
-                         yAlign: Tb.BoxAlignment.FILL,
-                         name: "sound-thing-playback-box" });
-
-        this._playbackBox.set_style(this._style);
-
         this._createSoundControlsBox();
 
-        this._contentBox.append(this._playbackBox,
+        this._soundBox.append(this._contentBox,
                                 Tb.BoxPackFlags.EXPAND);
     },
 
@@ -134,12 +129,12 @@ SoundThing.prototype = {
         controlsBox.append(this._soundControlsBox,
                            Tb.BoxPackFlags.EXPAND);
 
-        this._playbackBox.append(controlsBox,
-                                 Tb.BoxPackFlags.FIXED);
+        this._contentBox.append(controlsBox,
+                                Tb.BoxPackFlags.FIXED);
 
-        this._playbackBox.set_fixed_child_align(controlsBox,
-                                                Tb.BoxAlignment.FILL,
-                                                Tb.BoxAlignment.END);
+        this._contentBox.set_fixed_child_align(controlsBox,
+                                               Tb.BoxAlignment.FILL,
+                                               Tb.BoxAlignment.END);
     },
 
     _createPlayButton : function() {
@@ -218,43 +213,6 @@ SoundThing.prototype = {
                                 Tb.BoxPackFlags.NONE);
     },
 
-    _connectSoundSignals : function(fromState) {
-        this._disconnectSoundSignals();
-
-        this._soundEosId =
-            this._sound.connect("eos",
-                                Lang.bind(this,
-                                          this._onSoundEos));
-
-        this._soundErrorId =
-            this._sound.connect("error",
-                                Lang.bind(this,
-                                          this._onSoundError));
-
-        this._soundSizeChangeId =
-            this._sound.connect("size-change",
-                                Lang.bind(this,
-                                          this._onSoundSizeChange,
-                                          fromState));
-    },
-
-    _disconnectSoundSignals : function() {
-        if (this._soundEosId) {
-            this._sound.disconnect(this._soundEosId);
-            delete this._soundEosId
-        }
-
-        if (this._soundErrorId) {
-            this._sound.disconnect(this._soundErrorId);
-            delete this._soundErrorId;
-        }
-
-        if (this._soundSizeChangeId) {
-            this._sound.disconnect(this._soundSizeChangeId);
-            delete this._soundSizeChangeId
-        }
-    },
-
     _updateSpinner : function() {
         // FIXME: show/hide spinner depending on the
         // loading state of the sound
@@ -268,26 +226,14 @@ SoundThing.prototype = {
         this._soundFilename = soundFilename;
 
         if (this._soundFilename) {
-            this._connectSoundSignals(fromState);
             this._updateSpinner();
 
-            // hide sound while loading the new sound file
-            this._sound.opacity = 0;
-
             // start loading the new sound file
-            this._sound.set_filename(this._soundFilename);
+            this._player.filename = this._soundFilename;
 
-            this._sound.playing = true;
-            this._playButton.toggled = true;
-
-            if (fromState) {
-                let pauseSound = function() {
-                    this._sound.playing = false;
-                    this._playButton.toggled = false;
-                };
-
-                Mainloop.timeout_add(500,
-                                     Lang.bind(this, pauseSound));
+            if (!fromState) {
+                this._player.playing = true;
+                this._playButton.toggled = true;
             }
         }
 
@@ -296,28 +242,6 @@ SoundThing.prototype = {
         }
     },
 
-    _updateInitialSize : function(width, height) {
-        let aspectRatio = width / height;
-
-        let newWidth;
-        let newHeight;
-
-        // FIXME: the sound might be actually smaller
-        // than the space available. What do we do in
-        // that case?
-
-        if (width >= height) {
-            newWidth = this._initialWidth;
-            newHeight = newWidth / aspectRatio;
-        } else {
-            newHeight = this._initialHeight;
-            newWidth = newHeight * aspectRatio;
-        }
-
-        this._soundWidth = newWidth;
-        this._soundHeight = newHeight;
-    },
-
     _showSoundControlsBox : function() {
         Tweener.addTween(this._soundControlsBox,
                          { opacity: 255,
@@ -349,7 +273,7 @@ SoundThing.prototype = {
         //soundFilter.set_name('Sounds');
         //chooser.add_filter(soundFilter);
 
-        let soundsDir = GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_VIDEOS);
+        let soundsDir = GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_MUSIC);
         chooser.set_current_folder(soundsDir);
 
         chooser.set_transient_for(this.context.gtkWindow);
@@ -374,34 +298,8 @@ SoundThing.prototype = {
                                   false /* not from state*/);
     },
 
-    _updateForSoundLoaded : function(fromState) {
-        this._disconnectSoundSignals();
+    _updateForSoundLoaded : function() {
         this._updateSpinner();
-
-        [minTextHeight, naturalTextHeight] =
-            this._captionLabel.get_preferred_height(-1);
-
-        let thingWidth = this._soundWidth;
-        let thingHeight = this._soundHeight + naturalTextHeight;
-
-        this._minWidth = thingWidth;
-        this._minHeight = thingHeight;
-
-        if (!fromState) {
-            Tweener.addTween(this.actor,
-                             { width: thingWidth,
-                               height: thingHeight,
-                               time: fromState ? 0 : _UPDATE_SIZE_TIME,
-                               transition: _UPDATE_SIZE_TRANSITION });
-        }
-
-        Tweener.addTween(this._sound,
-                         { opacity: 255,
-                           delay: fromState ? 0 : _UPDATE_SIZE_TIME,
-                           time: fromState ? 0 : _UPDATE_SIZE_TIME });
-
-        delete this._soundWidth;
-        delete this._soundHeight;
     },
 
     _updateProgressBar : function() {
@@ -417,7 +315,7 @@ SoundThing.prototype = {
     _updateTimeLabel : function() {
         return;
         let currentTime =
-            Math.floor(this._sound.duration * this._sound.get_progress());
+            Math.floor(this._player.duration * this._player.get_progress());
 
         let hours = Math.floor(currentTime / 3600);
         currentTime -= hours * 3600;
@@ -452,7 +350,7 @@ SoundThing.prototype = {
     },
 
     _onPlayButtonToggled : function() {
-        this._sound.playing = this._playButton.toggled;
+        this._player.playing = this._playButton.toggled;
     },
 
     _onProgressBarButtonPressEvent : function(progressBar, event) {
@@ -462,29 +360,30 @@ SoundThing.prototype = {
             this._progressBar.get_transformed_position();
 
         let progress = (eventX - transformedX) / this._progressBar.width;
-        this._sound.set_progress(progress);
+        this._player.set_progress(progress);
     },
 
-    _onSoundProgressChanged : function() {
+    _onPlayerProgressChanged : function() {
         this._updateProgressBar();
         this._updateTimeLabel();
     },
 
-    _onSoundEos : function(sound) {
-        print('VIDEO EOS');
-    },
+    _onPlayerStateChanged : function() {
+        switch(this._player.state) {
+        case Tb.SoundPlayerState.IDLE:
+            this._updateForSoundLoaded();
+            break;
 
-    _onSoundError : function(sound) {
-        print('VIDEO ERROR');
-    },
+        case Tb.SoundPlayerState.DONE:
+            break;
 
-    _onSoundSizeChange : function(sound, width, height, fromState) {
-        if (this._soundWidth > 0 && this._soundHeight > 0) {
-            return;
-        }
+        case Tb.SoundPlayerState.ERROR:
+            // FIXME: show error message in the UI
+            break;
 
-        this._updateInitialSize(width, height);
-        this._updateForSoundLoaded(fromState);
+        default:
+            // do nothing
+        }
     },
 
     enter : function() {
@@ -546,12 +445,7 @@ SoundThing.prototype = {
     },
 
     destroy : function() {
-        if (this._playButtonClickedId) {
-            this._playButton.disconnect(this._playButtonClickedId);
-            delete this._playButtonClickedId;
-        }
-
-        this._disconnectSoundSignals();
+        this._player.destroy();
 
         if (this._soundBox) {
             this._soundBox.destroy();



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