Daniel@0: "use strict"; Daniel@0: App.module("PlayerModule", function(PlayerModule, App, Backbone, Marionette, $, _, Logger) { Daniel@0: // Prevent auto start Daniel@0: PlayerModule.startWithParent = false; Daniel@0: Daniel@0: // Define options Daniel@0: var defaultModuleOptions = { Daniel@0: }; Daniel@0: var moduleOptions; Daniel@0: Daniel@0: // Define private variables Daniel@0: var logger = null; Daniel@0: Daniel@0: /** Daniel@0: * Initialization checker Daniel@0: */ Daniel@0: var assertModuleIsInitialized = function() { Daniel@0: if (!$notifications) { Daniel@0: throw "PlayerModule has not been initialized"; Daniel@0: } Daniel@0: }; Daniel@0: Daniel@0: /** Daniel@0: * Module initializer Daniel@0: * Daniel@0: */ Daniel@0: PlayerModule.addInitializer(function(options){ Daniel@0: moduleOptions = _.extend(defaultModuleOptions, options); Daniel@0: Daniel@0: logger = Logger.get("PlayerModule"); Daniel@0: //logger.setLevel(Logger.DEBUG); Daniel@0: Daniel@0: logger.debug("Begin init"); Daniel@0: Daniel@0: PlayerModule.$player = $(Marionette.TemplateCache.get("#player")()); Daniel@0: PlayerModule.$playerTimeAfter = PlayerModule.$player.find(".player__time_type_after"); Daniel@0: PlayerModule.$player.hide().appendTo("body"); Daniel@0: Daniel@0: PlayerModule.$jPlayer = $("
").hide().appendTo("body"); Daniel@0: Daniel@0: var volume = App.DataModule.Storage.getStrCache(PlayerModule, "volume"); Daniel@0: if (volume !== undefined) { Daniel@0: volume = parseFloat(volume); Daniel@0: } Daniel@0: if (!_.isNumber(volume) || _.isNaN(volume) || volume < 0 && volume > 1) { Daniel@0: volume = undefined; Daniel@0: } Daniel@0: var muted = App.DataModule.Storage.getStrCache(PlayerModule, "muted") ? true : false; Daniel@0: Daniel@0: // The player itself (jPlayer) Daniel@0: PlayerModule.$jPlayer.jPlayer({ Daniel@0: swfPath: "./$/jquery.jplayer.swf", Daniel@0: supplied: "mp3", Daniel@0: wmode: "window", Daniel@0: cssSelectorAncestor: '.player__body', Daniel@0: useStateClassSkin: true, Daniel@0: autoBlur: false, Daniel@0: smoothPlayBar: false, Daniel@0: keyEnabled: false, Daniel@0: remainingDuration: true, Daniel@0: toggleDuration: false, Daniel@0: volume: volume, Daniel@0: solution: "html,flash", Daniel@0: muted: muted, Daniel@0: cssSelector: { Daniel@0: play: ".player__command_action_play", Daniel@0: pause: ".player__command_action_pause", Daniel@0: mute: ".player__command_action_volume-mute", Daniel@0: unmute: ".player__command_action_volume-down", Daniel@0: volumeMax: ".player__command_action_volume-up", Daniel@0: volumeBar: ".player__slider_type_volume", Daniel@0: volumeBarValue: ".player__slider-head_type_volume", Daniel@0: seekBar: ".player__slider_type_time", Daniel@0: playBar: ".player__slider-head_type_time", Daniel@0: currentTime: ".player__time_type_before", Daniel@0: duration: ".player__time_type_after", Daniel@0: } Daniel@0: }); Daniel@0: Daniel@0: PlayerModule.$jPlayer.on($.jPlayer.event.volumechange, function(event) { Daniel@0: var volume = PlayerModule.$jPlayer.jPlayer('option', 'volume'); Daniel@0: var muted = PlayerModule.$jPlayer.jPlayer('option', 'muted'); Daniel@0: Daniel@0: App.DataModule.Storage.setStrCache(PlayerModule, "volume", volume + ""); Daniel@0: App.DataModule.Storage.setStrCache(PlayerModule, "muted", muted ? "1" : ""); Daniel@0: Daniel@0: }); Daniel@0: Daniel@0: // A view that sits on top of the player Daniel@0: PlayerModule.PlayerView = Backbone.View.extend({ Daniel@0: Daniel@0: el: PlayerModule.$player, Daniel@0: Daniel@0: initialize: function() { Daniel@0: var _this = this; Daniel@0: //var $elHTML = $("#player"); Daniel@0: _this.$body = _this.$el.find('.player__body'); Daniel@0: App.TooltipModule.convertTitlesToTooltips(_this.$el); Daniel@0: Daniel@0: // This is needed to initialize the jplayer instance Daniel@0: //$elHTML.hide().appendTo("body"); Daniel@0: Daniel@0: //_this.$el.html($elHTML); Daniel@0: //$elHTML.show(); Daniel@0: _this.$el.show(); Daniel@0: Daniel@0: _this.$commandDownload = _this.$('.player__command_action_download'); Daniel@0: _this.$commandDownload.click(function(event) { Daniel@0: PlayerModule.$jPlayer.jPlayer("pause"); Daniel@0: }); Daniel@0: _this.$label1 = _this.$('.player__label_row_1'); Daniel@0: _this.$label2 = _this.$('.player__label_row_2'); Daniel@0: Daniel@0: _this.$cover = _this.$('.player__cover'); Daniel@0: _this.$messageError = _this.$('.player__message_type_error'); Daniel@0: _this.$messageLoading = _this.$('.player__message_type_loading'); Daniel@0: _this.$messageNoRecording = _this.$('.player__message_type_no-recording'); Daniel@0: _this.$messageNoAudio = _this.$('.player__message_type_no-audio'); Daniel@0: Daniel@0: _this._startPlayingAfterRecordingIsLoaded = false; Daniel@0: Daniel@0: _this._config = new App.ContextModule.Config(); Daniel@0: _this._dynamicDefinitionForRecording = App.dynamicDefinitionProviderForRecordings.get(_this._config); Daniel@0: Daniel@0: _this.listenTo(_this._dynamicDefinitionForRecording, "change", _this.render); Daniel@0: _this.listenTo(_this._config, "change", _this.render); Daniel@0: Daniel@0: Daniel@0: Daniel@0: this._cachedLabel = "initial"; Daniel@0: _this.render(); Daniel@0: }, Daniel@0: Daniel@0: render: function() { Daniel@0: var _this = this; Daniel@0: var recordingAttributes = _this._dynamicDefinitionForRecording.attributes; Daniel@0: var label = recordingAttributes.label; Daniel@0: var audio = recordingAttributes.audio ? recordingAttributes.audio[0] : undefined; Daniel@0: Daniel@0: // Update layout Daniel@0: if (!_.isEqual(_this._cachedRecordingAttributes, recordingAttributes)) { Daniel@0: if (!_this._dynamicDefinitionForRecording.attributes.label || !audio) { Daniel@0: _this.$body.hide(); Daniel@0: _this.$cover.show(); Daniel@0: _this.$cover.children().hide(); Daniel@0: switch (label) { Daniel@0: case false: Daniel@0: _this.$messageError.show(); Daniel@0: break; Daniel@0: case null: Daniel@0: _this.$messageLoading.show(); Daniel@0: break; Daniel@0: default: Daniel@0: if (label === undefined) { Daniel@0: _this.$messageNoRecording.show(); Daniel@0: } else { Daniel@0: _this.$messageNoAudio.show(); Daniel@0: } Daniel@0: } Daniel@0: PlayerModule.$jPlayer.jPlayer("stop"); Daniel@0: } else { Daniel@0: Daniel@0: PlayerModule.$jPlayer.jPlayer("setMedia", { Daniel@0: mp3: audio, Daniel@0: }); Daniel@0: Daniel@0: _this.$cover.hide(); Daniel@0: _this.$body.show(); Daniel@0: _this.$label1.text(recordingAttributes.label); Daniel@0: _this.$label2.text(recordingAttributes.composer); Daniel@0: _this.$commandDownload.attr("href", audio); Daniel@0: Daniel@0: if (_this._startPlayingAfterRecordingIsLoaded) { Daniel@0: PlayerModule.$jPlayer.jPlayer("play"); Daniel@0: _this._startPlayingAfterRecordingIsLoaded = false; Daniel@0: } Daniel@0: Daniel@0: } Daniel@0: } Daniel@0: Daniel@0: if (label) { Daniel@0: Daniel@0: } Daniel@0: _this._cachedRecordingAttributes = recordingAttributes; Daniel@0: }, Daniel@0: Daniel@0: play: function(recordingURI, time) { Daniel@0: var _this = this; Daniel@0: if (_this._config.getParameterValue("recordingURI") != recordingURI) { Daniel@0: _this._startPlayingAfterRecordingIsLoaded = true; Daniel@0: _this._config.updateParameter("recordingURI", recordingURI); Daniel@0: } else { Daniel@0: PlayerModule.$jPlayer.jPlayer("play", time ? time : 0); Daniel@0: } Daniel@0: }, Daniel@0: Daniel@0: stop: function(recordingURI) { Daniel@0: var _this = this; Daniel@0: PlayerModule.$jPlayer.jPlayer("pause"); Daniel@0: } Daniel@0: }); Daniel@0: Daniel@0: Daniel@0: PlayerModule.playerView = new PlayerModule.PlayerView(); Daniel@0: }); Daniel@0: Daniel@0: PlayerModule.play = function(recordingURI, time) { Daniel@0: // try { Daniel@0: PlayerModule.playerView.play(recordingURI, time); Daniel@0: // } catch (e) { Daniel@0: // console.log(e); Daniel@0: // } Daniel@0: App.NotificationsModule.show({ Daniel@0: id: "player", Daniel@0: content: PlayerModule.playerView.$el, Daniel@0: onClose: function() {PlayerModule.playerView.stop();}, Daniel@0: modifiers: ["ttl_ever", "no-padding"], Daniel@0: keepContentInMemoryAfterRemoval: true Daniel@0: }); Daniel@0: }; Daniel@0: Daniel@0: }, Logger);