Skip to content

Commit

Permalink
converted music player to use custom control bar with soundmanager
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Matthys committed May 10, 2012
1 parent 8364021 commit 83a8e1d
Show file tree
Hide file tree
Showing 28 changed files with 6,391 additions and 4,161 deletions.
960 changes: 306 additions & 654 deletions app/css/plex.css

Large diffs are not rendered by default.

Binary file removed app/img/controls.png
Binary file not shown.
Binary file added app/img/lens_controls.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/img/lens_play.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/img/lens_replay.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2,986 changes: 140 additions & 2,846 deletions app/js/libs/mediaelement-2.8.1.js

Large diffs are not rendered by default.

5,026 changes: 5,026 additions & 0 deletions app/js/libs/soundmanager2.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions app/js/main.js
Expand Up @@ -17,6 +17,7 @@ require.config({
'base64': 'libs/base64', 'base64': 'libs/base64',
'sha256': 'libs/jssha256-0.1', 'sha256': 'libs/jssha256-0.1',
'mediaelement': 'libs/mediaelement-2.8.1', 'mediaelement': 'libs/mediaelement-2.8.1',
'soundmanager': 'libs/soundmanager2',


// Require // Require
'use': 'libs/require/use-0.2.0', 'use': 'libs/require/use-0.2.0',
Expand Down Expand Up @@ -53,6 +54,7 @@ require.config({
base64: {}, base64: {},
sha256: {}, sha256: {},
mediaelement: {}, mediaelement: {},
soundmanager: {},


// Bootstrap // Bootstrap
transition: { deps: ['jquery'] }, transition: { deps: ['jquery'] },
Expand Down
1 change: 1 addition & 0 deletions app/js/plex/app.js
Expand Up @@ -6,6 +6,7 @@ define(
// Globals // Globals
'plex/control/init/BackboneInit', 'plex/control/init/BackboneInit',
'plex/control/init/HandlebarsInit', 'plex/control/init/HandlebarsInit',
'plex/control/init/SoundManagerInit',
'use!date', 'use!date',
'use!button', 'use!button',
'use!dropdown', 'use!dropdown',
Expand Down
16 changes: 16 additions & 0 deletions app/js/plex/control/init/SoundManagerInit.js
@@ -0,0 +1,16 @@
// Declare global variable to let SoundManager know we are deferring init
var SM2_DEFER = true;

define(
[
// Globals
'use!soundmanager'
],

function () {
soundManager = new SoundManager();
soundManager.url = 'swf/';
soundManager.flashVersion = 9;
soundManager.beginDelayedInit();
}
);
39 changes: 39 additions & 0 deletions app/js/plex/model/PlayerModel.js
@@ -0,0 +1,39 @@
define(
[
// Globals
'use!backbone'
],

function () {
var PlayerModel = Backbone.Model.extend({
defaults: {
currentTime: 0,
duration: 0,
formattedTime: '0:00',
formattedDuration: '0:00',
volume: 0,
paused: true,
ended: false,
buffering: false,
startBuffer: 0,
endBuffer: 0,
fullscreen: false
},

reset: function () {
this.set(this.defaults);
},

secondsToHms: function (seconds) {
seconds = Number(seconds);
var h = Math.floor(seconds / 3600);
var m = Math.floor(seconds % 3600 / 60);
var s = Math.floor(seconds % 3600 % 60);

return ((h > 0 ? h + ':' : '') + (m > 0 ? (h > 0 && m < 10 ? '0' : '') + m + ':' : '0:') + (s < 10 ? '0' : '') + s);
}
});

return PlayerModel;
}
);
123 changes: 88 additions & 35 deletions app/js/plex/view/players/MusicPlayerView.js
@@ -1,34 +1,52 @@
define( define(
[ [
'text!templates/players/MusicPlayerView.tpl',
'plex/control/Dispatcher', 'plex/control/Dispatcher',
'plex/control/utils/Transcoder', 'plex/control/utils/Transcoder',
'plex/model/AppModel', 'plex/model/AppModel',
'plex/model/PlayerModel',
'plex/view/BaseView', 'plex/view/BaseView',
'plex/view/players/core/ControlsView',


// Globals // Globals
'use!backbone', 'use!backbone',
'use!handlebars', 'use!handlebars',
'use!mediaelement' 'use!soundmanager'
], ],


function (template, dispatcher, transcoder, appModel, BaseView) { function (dispatcher, transcoder, appModel, PlayerModel, BaseView, ControlsView) {

var tpl = Handlebars.compile(template);


var MusicPlayerView = BaseView.extend({ var MusicPlayerView = BaseView.extend({
id: 'music-player', id: 'music-player',
className: 'animated slideDown', className: 'animated slideDown',


playerModel: undefined,
player: undefined, player: undefined,

sound: undefined,
volume: 1, // Used to persist volume from song to song

nextTrack: undefined, nextTrack: undefined,


initialize: function () { initialize: function () {
_.bindAll(this, 'onEnded'); _.bindAll(this, 'onPlay', 'whileLoading', 'whitePlaying', 'onFinish');

this.playerModel = new PlayerModel();

this.player = this.registerView(new ControlsView({
model: this.playerModel,
hidePlaybackRate: true,
hideFullscreen: true
}));

this.addBinding(this.player, 'playPause', this.onControlsPlayPause);
this.addBinding(this.player, 'seek', this.onControlsSeek);
this.addBinding(this.player, 'change:volume', this.onControlsVolumeChange);
}, },


render: function () { render: function () {
this.$el.html(tpl()); this.$el.html('');

this.$el.append(this.player.render().el);


return this; return this;
}, },
Expand All @@ -37,36 +55,21 @@ define(
var file = transcoder.file(this.model.get('Media').Part.key); var file = transcoder.file(this.model.get('Media').Part.key);


this.findNextTrack(); this.findNextTrack();
this.playerModel.reset();


this.$('.now-playing-title').html(this.model.get('title')); this.$('.now-playing-title').html(this.model.get('title'));


if (typeof(this.nextTrack) !== 'undefined') { this.sound = soundManager.createSound({
this.$('.next-title').html(this.nextTrack.get('title')); id: 'track_' + this.model.id,
this.$('.next-track').show(); url: file,
} else { autoLoad: true,
this.$('.next-track').hide(); autoPlay: true,
} volume: this.volume * 100,

onplay: this.onPlay,
if (typeof(this.player) === 'undefined') { whileloading: this.whileLoading,
this.$('audio').attr('src', file); whileplaying: this.whitePlaying,

onfinish: this.onFinish
this.player = new MediaElementPlayer('#music-player audio', { });
//mode: 'shim',
plugins: ['flash'],
pluginPath: 'swf/',
flashName: 'flashmediaelement.swf',
startVolume: 1,
success: function (player, element) {
player.play();
}
});

this.player.$media.on('ended', this.onEnded);
} else {
this.player.play();
}

console.log('now playing ' + file);
}, },


findNextTrack: function () { findNextTrack: function () {
Expand All @@ -80,7 +83,57 @@ define(
} }
}, },


onEnded: function (event) { onControlsPlayPause: function () {
var paused = this.playerModel.get('paused');

this.sound.togglePause();

console.log(paused);

this.playerModel.set('paused', !paused);
},

onControlsSeek: function (position) {
this.sound.setPosition(position * 1000);

this.playerModel.set({
currentTime: position,
formattedTime: this.playerModel.secondsToHms(position)
});
},

onControlsVolumeChange: function (volume) {
this.sound.setVolume(volume * 100);

this.volume = volume;
this.playerModel.set('volume', volume);
},

onPlay: function () {
this.playerModel.set({
paused: false,
volume: this.volume
});
},

whileLoading: function () {
this.playerModel.set({
endBuffer: this.sound.bytesLoaded / this.sound.bytesTotal * (this.sound.durationEstimate / 1000),
duration: this.sound.durationEstimate / 1000,
formattedDuration: this.playerModel.secondsToHms(this.sound.durationEstimate / 1000)
});
},

whitePlaying: function () {
this.playerModel.set({
currentTime: this.sound.position / 1000,
formattedTime: this.playerModel.secondsToHms(this.sound.position / 1000)
});
},

onFinish: function () {
this.sound.destruct();

if (typeof(this.nextTrack) !== 'undefined') { if (typeof(this.nextTrack) !== 'undefined') {
this.model = this.nextTrack; this.model = this.nextTrack;


Expand Down

0 comments on commit 83a8e1d

Please sign in to comment.