Skip to content

Commit 7ef1b06

Browse files
committed
Supports blob and url loading
1 parent 9989f27 commit 7ef1b06

1 file changed

Lines changed: 94 additions & 95 deletions

File tree

src/loader/filetypes/VideoFile.js

Lines changed: 94 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var Class = require('../../utils/Class');
88
var CONST = require('../../const');
99
var File = require('../File');
1010
var FileTypesManager = require('../FileTypesManager');
11+
var GetURL = require('../GetURL');
1112
var GetFastValue = require('../../utils/object/GetFastValue');
1213
var IsPlainObject = require('../../utils/object/IsPlainObject');
1314

@@ -37,14 +38,17 @@ var VideoFile = new Class({
3738

3839
initialize:
3940

40-
// URL is an object created by VideoFile.findAudioURL
41+
// URL is an object created by VideoFile.getVideoURL
4142
function VideoFile (loader, key, urlConfig, loadEvent, asBlob, xhrSettings)
4243
{
44+
if (loadEvent === undefined) { loadEvent = 'canplaythrough'; }
45+
if (asBlob === undefined) { asBlob = false; }
46+
4347
var fileConfig = {
4448
type: 'video',
4549
cache: loader.cacheManager.video,
4650
extension: urlConfig.type,
47-
responseType: 'arraybuffer',
51+
responseType: 'blob',
4852
key: key,
4953
url: urlConfig.url,
5054
xhrSettings: xhrSettings,
@@ -54,13 +58,9 @@ var VideoFile = new Class({
5458
}
5559
};
5660

57-
File.call(this, loader, fileConfig);
61+
console.log(fileConfig);
5862

59-
// New properties specific to this class
60-
// this.locked = 'ontouchstart' in window;
61-
// this.loaded = false;
62-
// this.filesLoaded = 0;
63-
// this.filesTotal = 0;
63+
File.call(this, loader, fileConfig);
6464
},
6565

6666
/**
@@ -72,77 +72,73 @@ var VideoFile = new Class({
7272
*/
7373
onProcess: function ()
7474
{
75+
console.log('Video.onProcess', this.config.asBlob);
76+
7577
this.state = CONST.FILE_PROCESSING;
7678

77-
// var ctor = this.config.dataType;
79+
if (!this.config.asBlob)
80+
{
81+
this.onProcessComplete();
7882

79-
// this.data = (ctor) ? new ctor(this.xhrLoader.response) : this.xhrLoader.response;
83+
return;
84+
}
8085

81-
this.data = this.xhrLoader.response;
86+
var video = document.createElement('video');
87+
video.controls = false;
88+
video.canplay = true;
89+
video.setAttribute('autoplay', 'autoplay');
90+
video.setAttribute('playsinline', 'playsinline');
8291

83-
this.onProcessComplete();
84-
}
92+
this.data = video;
8593

86-
/**
87-
* Called when the file finishes loading.
88-
*
89-
* @method Phaser.Loader.FileTypes.HTML5AudioFile#onLoad
90-
* @since 3.0.0
91-
onLoad: function ()
92-
{
93-
if (this.loaded)
94-
{
95-
return;
96-
}
94+
var _this = this;
9795

98-
this.loaded = true;
96+
this.data.onloadeddata = function ()
97+
{
98+
console.log('data.onloadeddata');
99+
100+
File.revokeObjectURL(_this.data);
99101

100-
this.loader.nextFile(this, true);
101-
},
102-
*/
102+
_this.onProcessComplete();
103+
};
103104

104-
/**
105-
* Called if the file errors while loading.
106-
*
107-
* @method Phaser.Loader.FileTypes.HTML5AudioFile#onError
108-
* @since 3.0.0
109-
onError: function ()
110-
{
111-
for (var i = 0; i < this.data.length; i++)
105+
this.data.onerror = function ()
112106
{
113-
var audio = this.data[i];
107+
console.log('data.onerror');
114108

115-
audio.oncanplaythrough = null;
116-
audio.onerror = null;
117-
}
109+
File.revokeObjectURL(_this.data);
118110

119-
this.loader.nextFile(this, false);
111+
_this.onProcessError();
112+
};
113+
114+
console.log('onProcess createURL');
115+
116+
File.createObjectURL(this.data, this.xhrLoader.response, '');
120117
},
121-
*/
122118

123119
/**
124-
* Called during the file load progress. Is sent a DOM ProgressEvent.
120+
* Called when the file finishes loading, is sent a DOM ProgressEvent.
125121
*
126-
* @method Phaser.Loader.FileTypes.HTML5AudioFile#onProgress
127-
* @fires Phaser.Loader.Events#FILE_PROGRESS
122+
* @method Phaser.Loader.File#onLoad
128123
* @since 3.0.0
129-
onProgress: function (event)
124+
*
125+
* @param {XMLHttpRequest} xhr - The XMLHttpRequest that caused this onload event.
126+
* @param {ProgressEvent} event - The DOM ProgressEvent that resulted from this load.
127+
onLoadVideo: function (event)
130128
{
131-
var audio = event.target;
129+
console.log('onLoadVideo');
130+
console.log(event);
131+
console.log(this);
132132
133-
audio.oncanplaythrough = null;
134-
audio.onerror = null;
133+
var video = event.target;
135134
136-
this.filesLoaded++;
135+
video.removeEventListener(this.config.loadEvent, this.onLoadVideo, true);
137136
138-
this.percentComplete = Math.min((this.filesLoaded / this.filesTotal), 1);
137+
this.data = video;
139138
140-
this.loader.emit(Events.FILE_PROGRESS, this, this.percentComplete);
139+
this.resetXHR();
141140
142-
if (this.filesLoaded === this.filesTotal)
143-
{
144-
this.onLoad();
145-
}
141+
this.loader.nextFile(this, true);
146142
},
147143
*/
148144

@@ -153,58 +149,59 @@ var VideoFile = new Class({
153149
*
154150
* @method Phaser.Loader.FileTypes.HTML5AudioFile#load
155151
* @since 3.0.0
152+
*/
156153
load: function ()
157154
{
158-
this.data = [];
155+
var loadEvent = this.config.loadEvent;
156+
var asBlob = this.config.asBlob;
159157

160-
var instances = (this.config && this.config.instances) || 1;
158+
if (asBlob)
159+
{
160+
console.log('Passing load over to File.load');
161+
162+
File.prototype.load.call(this);
163+
}
164+
else
165+
{
166+
console.log('Loading as Video tag');
161167

162-
this.filesTotal = instances;
163-
this.filesLoaded = 0;
164-
this.percentComplete = 0;
168+
this.percentComplete = 0;
165169

166-
for (var i = 0; i < instances; i++)
167-
{
168-
var audio = new Audio();
169-
audio.dataset = {};
170-
audio.dataset.name = this.key + ('0' + i).slice(-2);
171-
audio.dataset.used = 'false';
170+
var video = document.createElement('video');
171+
172+
video.controls = false;
173+
174+
video.setAttribute('autoplay', 'autoplay');
175+
video.setAttribute('playsinline', 'playsinline');
172176

173-
if (this.locked)
177+
var _this = this;
178+
179+
this.onVideoLoadHandler = function (event)
174180
{
175-
audio.dataset.locked = 'true';
176-
}
177-
else
178-
{
179-
audio.dataset.locked = 'false';
180-
181-
audio.preload = 'auto';
182-
audio.oncanplaythrough = this.onProgress.bind(this);
183-
audio.onerror = this.onError.bind(this);
184-
}
181+
console.log('onVideoLoadHandler');
182+
console.log(event);
183+
console.log(_this.config.loadEvent);
184+
185+
var video = event.target;
186+
187+
video.removeEventListener(_this.config.loadEvent, _this.onVideoLoadHandler, true);
188+
189+
_this.data = video;
190+
191+
_this.resetXHR();
192+
193+
_this.loader.nextFile(_this, true);
194+
};
185195

186-
this.data.push(audio);
187-
}
196+
video.addEventListener(loadEvent, this.onVideoLoadHandler, true);
188197

189-
for (i = 0; i < this.data.length; i++)
190-
{
191-
audio = this.data[i];
192-
audio.src = GetURL(this, this.loader.baseURL);
198+
video.src = GetURL(this, this.loader.baseURL);
193199

194-
if (!this.locked)
195-
{
196-
audio.load();
197-
}
198-
}
200+
video.canplay = true;
199201

200-
if (this.locked)
201-
{
202-
// This is super-dangerous but works. Race condition potential high.
203-
// Is there another way?
204-
setTimeout(this.onLoad.bind(this));
202+
video.load();
205203
}
206204
}
207-
*/
208205

209206
});
210207

@@ -223,6 +220,8 @@ VideoFile.create = function (loader, key, urls, loadEvent, asBlob, xhrSettings)
223220

224221
var urlConfig = VideoFile.getVideoURL(game, urls);
225222

223+
console.log(urlConfig);
224+
226225
if (urlConfig)
227226
{
228227
return new VideoFile(loader, key, urlConfig, loadEvent, asBlob, xhrSettings);

0 commit comments

Comments
 (0)