Permalink
Browse files

Fixed using XHR to load sounds and video. Also updated tests. fixes #160

  • Loading branch information...
wdamien committed Oct 21, 2015
1 parent b51f76b commit bcc8413695304a48b65e1f222a76c5e3debdd2c8
Showing with 62 additions and 16 deletions.
  1. +20 −1 src/preloadjs/loaders/AbstractMediaLoader.js
  2. +42 −15 tests/spec/LoadQueueSpec.js
@@ -56,6 +56,8 @@ this.createjs = this.createjs || {};
// protected properties
this._tagSrcAttribute = "src";
this.on("initialize", this._updateXHR, this);
};
var p = createjs.extend(AbstractMediaLoader, createjs.AbstractLoader);
@@ -91,6 +93,20 @@ this.createjs = this.createjs || {};
}
};
// protected methods
/**
* Before the item loads, set its mimeType and responseType.
* @property _updateXHR
* @param {Event} event
* @private
*/
p._updateXHR = function (event) {
// Only exists for XHR
if (event.loader.setResponseType) {
event.loader.setResponseType("blob");
}
};
/**
* The result formatter for media files.
* @method _formatResult
@@ -102,7 +118,10 @@ this.createjs = this.createjs || {};
this._tag.removeEventListener && this._tag.removeEventListener("canplaythrough", this._loadedHandler);
this._tag.onstalled = null;
if (this._preferXHR) {
loader.getTag().src = loader.getResult(true);
var URL = window.URL || window.webkitURL;
var result = loader.getResult(true);
loader.getTag().src = URL.createObjectURL(result);
}
return loader.getTag();
};
@@ -84,8 +84,11 @@ describe("PreloadJS.LoadQueue", function () {
it("should load video", function (done) {
this.queue.addEventListener("fileload", function (evt) {
expect(evt.result).toEqual(jasmine.any(HTMLMediaElement));
done();
evt.result.addEventListener("playing", function() {
expect(evt.result).toEqual(jasmine.any(HTMLMediaElement));
done();
});
evt.result.play();
});
this.loadFile({
@@ -108,8 +111,12 @@ describe("PreloadJS.LoadQueue", function () {
it("should load an existing sound tag", function (done) {
this.queue.addEventListener("fileload", function (evt) {
expect(evt.result).toEqual(tag);
done();
evt.result.addEventListener("playing", function() {
expect(evt.result).toEqual(tag);
done();
})
evt.result.play();
});
var tag = document.createElement("audio");
@@ -257,17 +264,37 @@ describe("PreloadJS.LoadQueue", function () {
this.loadFile({src: "art/gbot.svg", type: createjs.LoadQueue.TEXT});
});
it("should load sounds (xhr)", function (done) {
this.queue.addEventListener("fileload", function (evt) {
expect(evt.result).toEqual(jasmine.any(HTMLMediaElement));
done();
});
this.loadFile({
src: "audio/Thunder.mp3",
type: createjs.AbstractLoader.SOUND
}, true);
});
describe("MediaElement Loading", function() {
it("should load sounds (xhr)", function (done) {
this.queue.addEventListener("fileload", function (evt) {
evt.result.addEventListener("playing", function() {
expect(evt.result).toEqual(jasmine.any(HTMLMediaElement));
done();
});
evt.result.play();
});
this.loadFile({
src: "audio/Thunder.mp3",
type: createjs.AbstractLoader.SOUND
}, true);
});
it("should load video (xhr)", function (done) {
this.queue.addEventListener("fileload", function (evt) {
evt.result.addEventListener("playing", function() {
expect(evt.result).toEqual(jasmine.any(HTMLMediaElement));
done();
});
evt.result.play();
});
this.loadFile({
src: "static/video.mp4",
type: createjs.AbstractLoader.VIDEO
}, true);
});
});
});
// This fails in Opera and IE (expected, as crossOrigin is not supported)

0 comments on commit bcc8413

Please sign in to comment.