public
Created

  • Download Gist
sound_player_view.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
Ember.SoundPlayerManager = Ember.StateManager.extend({
initialState: 'preparing',
 
preparing: Ember.State.create({
ready: function(manager, context) {
manager.transitionTo('unloaded');
}
}),
 
unloaded: Ember.State.create({
loaded: function(manager, context) {
manager.transitionTo('stopped');
}
}),
 
stopped: Ember.State.create({
play: function(manager, context) {
manager.transitionTo('started.playing');
}
}),
 
started: Ember.State.create({
stop: function(manager, context) {
manager.transitionTo('stopped');
},
 
paused: Ember.State.create({
play: function(manager, context) {
manager.transitionTo('playing');
}
}),
 
playing: Ember.State.create({
pause: function(manager, context) {
manager.transitionTo('paused')
}
})
})
});
 
Ember.SoundPlayerView = Ember.View.extend({
init: function() {
var manager = Ember.SoundPlayerManager.create();
var self = this;
 
this.set("stateManager", manager);
soundManager.onready(function() {
manager.send('ready');
self.loadSound();
});
this._super();
},
 
soundLoaded: function() {
this.get('stateManager').send('loaded');
this.set('position', 0);
this.set('duration', this.get('sound').duration);
},
 
loadSound: function() {
var self = this;
 
if (!this.get('isStopped')) {
this.stop();
}
 
if (!this.soundObject) {
this.soundObject = soundManager.createSound({
url: this.get('url'),
onload: function() { self.soundLoaded(); },
whileplaying: function() { self.tick(); },
onfinish: function() { self.finish(); }
});
 
this.soundObject.load();
}
},
 
sound: function() {
this.loadSound();
return this.soundObject;
}.property('url'),
 
play: function() {
if (this.get('isStopped')) {
this.get('sound').play();
} else {
this.get('sound').resume();
}
this.get('stateManager').send('play');
},
 
pause: function() {
this.get('stateManager').send('pause');
this.get('sound').pause();
},
 
toggle: function() {
if (this.get('isPlaying')) {
this.pause();
} else {
this.play();
}
},
 
stop: function() {
this.get('stateManager').send('stop');
this.get('sound').stop();
this.set('position', 0);
},
 
finish: function() {
this.get('stateManager').send('stop');
this.set('position', 0);
},
 
tick: function() {
this.set('position', this.get('sound').position);
},
 
isLoading: function() {
return this.get('stateManager.currentState.name') === 'unloaded';
}.property('stateManager.currentState'),
 
isStopped: function() {
return !/^started\./.test(this.get('stateManager.currentState.path'));
}.property('stateManager.currentState'),
 
isPlaying: function() {
return this.get('stateManager.currentState.name') === 'playing'
}.property('stateManager.currentState')
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.