Commit 7e6d2192 authored by Mukul's avatar Mukul

[erp5_mutlimedia] Adds functionality to reuse the same player controller for audio files.

parent 910ebf35
...@@ -45,7 +45,7 @@ ...@@ -45,7 +45,7 @@
play_button.classList.add('ui-icon-play'); play_button.classList.add('ui-icon-play');
play_button.classList.remove('ui-icon-pause'); play_button.classList.remove('ui-icon-pause');
} }
return gadget.getDeclaredGadget('controller') return gadget.getDeclaredGadget(gadget.params.scope)
.push(function (controller) { .push(function (controller) {
return controller.handlePlayPause(gadget.state.play); return controller.handlePlayPause(gadget.state.play);
}); });
...@@ -62,7 +62,7 @@ ...@@ -62,7 +62,7 @@
volume_button.classList.remove('ui-icon-volume-off'); volume_button.classList.remove('ui-icon-volume-off');
volume_button.classList.add('ui-icon-volume-up'); volume_button.classList.add('ui-icon-volume-up');
} }
return gadget.getDeclaredGadget('controller') return gadget.getDeclaredGadget(gadget.params.scope)
.push(function (controller) { .push(function (controller) {
return controller.handleSound(gadget.state.mute); return controller.handleSound(gadget.state.mute);
}); });
...@@ -75,14 +75,24 @@ ...@@ -75,14 +75,24 @@
var name_array = doc.title.split('.'), var name_array = doc.title.split('.'),
type = name_array[name_array.length - 1]; type = name_array[name_array.length - 1];
if (type === 'mp3' && MediaSource.isTypeSupported('audio/mpeg')) { if (type === 'mp3' && MediaSource.isTypeSupported('audio/mpeg')) {
gadget.params.scope = 'controller';
if (gadget.params.controller) {
return gadget.getDeclaredGadget('controller');
}
gadget.params.controller = true;
return gadget.declareGadget('gadget_custom_player_controller.html', { return gadget.declareGadget('gadget_custom_player_controller.html', {
element: gadget.element.querySelector('.controller'), element: gadget.element.querySelector('.controller'),
scope: 'controller' scope: 'controller'
}); });
} }
gadget.params.scope = 'controller_fallback';
if (gadget.params.contoller_fallback) {
return gadget.getDeclaredGadget('controller_fallback');
}
gadget.params.controller_fallback = true;
return gadget.declareGadget('gadget_custom_player_controller_fallback.html', { return gadget.declareGadget('gadget_custom_player_controller_fallback.html', {
element: gadget.element.querySelector('.controller'), element: gadget.element.querySelector('.controller'),
scope: 'controller' scope: 'controller_fallback'
}); });
}) })
.push(function (controller) { .push(function (controller) {
...@@ -93,18 +103,21 @@ ...@@ -93,18 +103,21 @@
}) })
.push(function () { .push(function () {
if (params.auto_play) { if (params.auto_play) {
return gadget.changeState({ play: true, auto_play: true }); return gadget.changeState({ play: true, auto_play: true});
} }
}).push(function() {
return RSVP.all([
gadget.togglePlayPause(gadget.state.play),
gadget.toggleSound(gadget.state.mute)
]);
}); });
}) })
.onStateChange(function (modification_dict) { .ready(function () {
if (modification_dict.hasOwnProperty('play')) { this.params = {
return this.togglePlayPause(modification_dict.play); controller: false,
} controller_fallback: false
if (modification_dict.hasOwnProperty('mute')) { };
return this.toggleSound(modification_dict.mute);
}
}) })
.declareService(function () { .declareService(function () {
...@@ -114,7 +127,10 @@ ...@@ -114,7 +127,10 @@
'click', 'click',
false, false,
function () { function () {
return gadget.changeState({ play: !gadget.state.play }); return gadget.changeState({ play: !gadget.state.play })
.push(function () {
return gadget.togglePlayPause(gadget.state.play);
});
}, },
true true
); );
...@@ -127,7 +143,10 @@ ...@@ -127,7 +143,10 @@
'click', 'click',
false, false,
function () { function () {
return gadget.changeState({ mute: !gadget.state.mute }); return gadget.changeState({ mute: !gadget.state.mute })
.push(function () {
return gadget.toggleSound(gadget.state.mute);
});
}, },
true true
); );
......
...@@ -49,8 +49,8 @@ ...@@ -49,8 +49,8 @@
return buffer; return buffer;
}, function (error) { }, function (error) {
if ((error.constructor.name === 'jIOError' && error.status_code === 404) || if ((error.constructor.name === 'jIOError' && error.status_code === 404) ||
(error.target && error.target.result === undefined)) { (error.target && error.target.error.name === "NotReadableError")) {
return gadget.notifySubmitted({message: error.message || error.target.error, status: 'fail'}); return gadget.notifySubmitted({message: error.message || error.target.error.message, status: 'fail'});
} }
throw error; throw error;
}); });
...@@ -97,10 +97,16 @@ ...@@ -97,10 +97,16 @@
.declareMethod('render', function (params) { .declareMethod('render', function (params) {
var gadget = this, var gadget = this,
audio = this.element.querySelector('audio'),
queue = new RSVP.Queue(); queue = new RSVP.Queue();
gadget.params.id = params.id; gadget.params.id = params.id;
gadget.params.name = params.name; gadget.params.name = params.name;
gadget.params.end = false;
gadget.params.index = 0; gadget.params.index = 0;
gadget.time_offset = 0;
gadget.params.mediaSource = new MediaSource();
audio.src = URL.createObjectURL(gadget.params.mediaSource);
return queue return queue
.push(function () { .push(function () {
...@@ -126,15 +132,12 @@ ...@@ -126,15 +132,12 @@
var audioContext = new AudioContext(), var audioContext = new AudioContext(),
audio = this.element.querySelector('audio'), audio = this.element.querySelector('audio'),
gain = audioContext.createGain(), gain = audioContext.createGain(),
source = audioContext.createMediaElementSource(audio), source = audioContext.createMediaElementSource(audio);
mediaSource = new MediaSource();
audio.src = URL.createObjectURL(mediaSource);
this.params = { this.params = {
audioContext: audioContext, audioContext: audioContext,
gain: gain, gain: gain,
source: source, source: source
mediaSource: mediaSource
}; };
}) })
...@@ -171,7 +174,7 @@ ...@@ -171,7 +174,7 @@
'timeupdate', 'timeupdate',
false, false,
function () { function () {
if ((gadget.params.sourceBuffer.timestampOffset - audio.currentTime) < 10 && !gadget.params.requested) { if (((gadget.params.sourceBuffer.timestampOffset + gadget.params.time_offset) - audio.currentTime) < 10 && !gadget.params.requested) {
gadget.params.requested = true; gadget.params.requested = true;
return gadget.requestChunk(); return gadget.requestChunk();
} }
......
...@@ -33,8 +33,8 @@ ...@@ -33,8 +33,8 @@
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
if ((error.constructor.name === 'jIOError' && error.status_code === 404) || if ((error.constructor.name === 'jIOError' && error.status_code === 404) ||
(error.target && error.target.result === undefined)) { (error.target && error.target.error.name === "NotReadableError")) {
return gadget.notifySubmitted({message: error.message || error.target.error, status: 'fail'}); return gadget.notifySubmitted({message: error.message || error.target.error.message, status: 'fail'});
} }
throw error; throw error;
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment