You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
246 lines
4.4 KiB
246 lines
4.4 KiB
/*jslint laxbreak:true */
|
|
/*jslint laxcomma:true */
|
|
/*jslint loopfunc:true */
|
|
/*jslint strict:true */
|
|
/*jslint browser:true */
|
|
/*jslint devel:true */
|
|
define([
|
|
"underscore"
|
|
, "jquery"
|
|
, "toolbox"
|
|
, "backbone"
|
|
]
|
|
, function (
|
|
_
|
|
, $
|
|
, Toolbox
|
|
, Backbone
|
|
) {
|
|
"use strict";
|
|
|
|
var WebAudio = Toolbox.Base.extend(_.extend({
|
|
_defaultOpts: {
|
|
autoplay: "autoplay"
|
|
, preload: "auto"
|
|
, controls: false
|
|
, loop: false
|
|
, src: false
|
|
}
|
|
|
|
, obj: new Audio()
|
|
|
|
, $obj: null
|
|
|
|
, _states: {
|
|
stopped: 0
|
|
, error: 1
|
|
, loading: 2
|
|
, playing: 3
|
|
, paused: 4
|
|
}
|
|
|
|
, _state: 0
|
|
|
|
, constructor: function () {
|
|
var that = this
|
|
;
|
|
|
|
_.bindAll(this
|
|
// Public
|
|
, "loadMedia"
|
|
// Private
|
|
, "_bindAudio"
|
|
// Events
|
|
, "__audioProgress"
|
|
, "__audioError"
|
|
, "__audioPause"
|
|
, "__audioPlay"
|
|
, "__audioSeeking"
|
|
, "__audioSeeked"
|
|
, "__audioTimeUpdate"
|
|
, "__audioLoadStart"
|
|
, "__audioCanPlay"
|
|
, "__audioEnded"
|
|
);
|
|
|
|
that.$obj = $(that.obj);
|
|
|
|
_.each(that._defaultOpts, function (value, key) {
|
|
if (value !== false) {
|
|
that.obj.setAttribute(key, value);
|
|
}
|
|
});
|
|
|
|
that._bindAudio();
|
|
|
|
return that;
|
|
}
|
|
/**
|
|
* PUBLIC
|
|
**/
|
|
, loadMedia: function (uri, mime, callback) {
|
|
var that = this
|
|
, audioSource = document.createElement('source')
|
|
;
|
|
|
|
audioSource.src = uri;
|
|
that.obj.src = uri;
|
|
audioSource.type = mime;
|
|
that.obj.type = mime;
|
|
|
|
that.obj.innerHTML = "";
|
|
|
|
that.obj.appendChild(audioSource);
|
|
|
|
console.debug("Loading media: " + uri + "\t type: " + mime);
|
|
|
|
that._state = that._states.loading;
|
|
|
|
that.obj.load();
|
|
|
|
return callback && callback();
|
|
}
|
|
/**
|
|
* PRIVATE
|
|
**/
|
|
, _bindAudio: function () {
|
|
var that = this
|
|
;
|
|
|
|
that.$obj.unbind();
|
|
|
|
that.$obj.on("loadstart", that.__audioLoadStart);
|
|
|
|
that.$obj.on("progress", that.__audioProgress);
|
|
|
|
//that.$obj.on("suspend", that.__audioSuspend);
|
|
|
|
that.$obj.on("abort", that.__audioError);
|
|
|
|
that.$obj.on("error", that.__audioError);
|
|
|
|
//that.$obj.on("emptied", that.__audioEmptied);
|
|
|
|
//that.$obj.on("stalled", that.__audioStalled);
|
|
that.$obj.on("pause", that.__audioPause);
|
|
|
|
that.$obj.on("play", that.__audioPlay);
|
|
|
|
//that.$obj.on("loadedmetadata", that.__audioLoadedMetaData);
|
|
|
|
//that.$obj.on("loadeddata", that.__audioLoadedData);
|
|
|
|
//that.$obj.on("waiting", that.__audioWaiting);
|
|
|
|
//that.$obj.on("playing", that.__audioPlaying);
|
|
|
|
that.$obj.on("canplay", that.__audioCanPlay);
|
|
|
|
//that.$obj.on("canplaythrough", that.__audioCanPlayThrough);
|
|
|
|
that.$obj.on("seeking", that.__audioSeeking);
|
|
|
|
that.$obj.on("seeked", that.__audioSeeked);
|
|
|
|
that.$obj.on("timeupdate", that.__audioTimeUpdate);
|
|
|
|
that.$obj.on("ended", that.__audioEnded);
|
|
|
|
//that.$obj.on("ratechange", that.__audioRateChanged);
|
|
|
|
//that.$obj.on("durationchange", that.__audioDurationChange); // TODO: We probably need to implement this!
|
|
|
|
//that.$obj.on("volumechange", that.__audioVolumeChange);
|
|
}
|
|
|
|
/**
|
|
* EVENTS
|
|
**/
|
|
, __audioProgress: function (event) {
|
|
var that = this
|
|
;
|
|
|
|
that.trigger("progress", event);
|
|
}
|
|
|
|
, __audioError: function (event) {
|
|
var that = this
|
|
;
|
|
|
|
that._state = that._states.stopped;
|
|
|
|
that.trigger.apply(that, ["state", that._state]);
|
|
}
|
|
|
|
, __audioPause: function (event) {
|
|
var that = this
|
|
;
|
|
|
|
that._state = that._states.paused;
|
|
|
|
that.trigger.apply(that, ["state", that._state]);
|
|
}
|
|
|
|
, __audioPlay: function (event) {
|
|
var that = this
|
|
;
|
|
|
|
that._state = that._states.playing;
|
|
|
|
that.trigger.apply(that, ["state", that._state]);
|
|
}
|
|
|
|
, __audioSeeking: function (event) {
|
|
var that = this
|
|
;
|
|
|
|
that._state = that._states.loading;
|
|
|
|
that.trigger.apply(that, ["state", that._state]);
|
|
}
|
|
|
|
, __audioSeeked: function (event) {
|
|
var that = this
|
|
;
|
|
|
|
that.obj.play();
|
|
|
|
that._state = that._states.playing;
|
|
|
|
that.trigger.apply(that, ["state", that._state]);
|
|
}
|
|
|
|
, __audioTimeUpdate: function (event) {
|
|
var that = this
|
|
;
|
|
|
|
that.trigger("timeupdate", event);
|
|
}
|
|
|
|
, __audioLoadStart: function (event) {
|
|
var that = this
|
|
;
|
|
|
|
that._state = that._states.loading;
|
|
|
|
that.trigger.apply(that, ["state", that._state]);
|
|
}
|
|
|
|
, __audioCanPlay: function (event) {
|
|
var that = this
|
|
;
|
|
|
|
}
|
|
|
|
, __audioEnded: function (event) {
|
|
var that = this
|
|
;
|
|
|
|
that._state = that._states.stopped;
|
|
|
|
that.trigger("ended", event);
|
|
}
|
|
}, Backbone.Events));
|
|
|
|
return WebAudio;
|
|
}); |