Skip to content

Commit 3dd82c4

Browse files
committed
Working through the Video loader
1 parent edf6375 commit 3dd82c4

1 file changed

Lines changed: 157 additions & 69 deletions

File tree

src/loader/filetypes/VideoFile.js

Lines changed: 157 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,8 @@ var VideoFile = new Class({
3838
initialize:
3939

4040
// URL is an object created by VideoFile.findAudioURL
41-
function VideoFile (loader, key, urlConfig, xhrSettings, audioContext)
41+
function VideoFile (loader, key, urlConfig, loadEvent, asBlob, xhrSettings)
4242
{
43-
if (IsPlainObject(key))
44-
{
45-
var config = key;
46-
47-
key = GetFastValue(config, 'key');
48-
xhrSettings = GetFastValue(config, 'xhrSettings');
49-
audioContext = GetFastValue(config, 'context', audioContext);
50-
}
51-
5243
var fileConfig = {
5344
type: 'video',
5445
cache: loader.cacheManager.video,
@@ -57,81 +48,188 @@ var VideoFile = new Class({
5748
key: key,
5849
url: urlConfig.url,
5950
xhrSettings: xhrSettings,
60-
config: { context: audioContext }
51+
config: {
52+
loadEvent: loadEvent,
53+
asBlob: asBlob
54+
}
6155
};
6256

6357
File.call(this, loader, fileConfig);
58+
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;
6464
},
6565

6666
/**
6767
* Called automatically by Loader.nextFile.
6868
* This method controls what extra work this File does with its loaded data.
6969
*
70-
* @method Phaser.Loader.FileTypes.VideoFile#onProcess
71-
* @since 3.20.0
70+
* @method Phaser.Loader.FileTypes.BinaryFile#onProcess
71+
* @since 3.7.0
7272
*/
7373
onProcess: function ()
7474
{
7575
this.state = CONST.FILE_PROCESSING;
7676

77-
var _this = this;
77+
// var ctor = this.config.dataType;
7878

79-
// interesting read https://github.com/WebAudio/web-audio-api/issues/1305
80-
this.config.context.decodeAudioData(this.xhrLoader.response,
81-
function (audioBuffer)
82-
{
83-
_this.data = audioBuffer;
79+
// this.data = (ctor) ? new ctor(this.xhrLoader.response) : this.xhrLoader.response;
80+
81+
this.data = this.xhrLoader.response;
82+
83+
this.onProcessComplete();
84+
}
85+
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+
}
97+
98+
this.loaded = true;
99+
100+
this.loader.nextFile(this, true);
101+
},
102+
*/
103+
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++)
112+
{
113+
var audio = this.data[i];
114+
115+
audio.oncanplaythrough = null;
116+
audio.onerror = null;
117+
}
118+
119+
this.loader.nextFile(this, false);
120+
},
121+
*/
122+
123+
/**
124+
* Called during the file load progress. Is sent a DOM ProgressEvent.
125+
*
126+
* @method Phaser.Loader.FileTypes.HTML5AudioFile#onProgress
127+
* @fires Phaser.Loader.Events#FILE_PROGRESS
128+
* @since 3.0.0
129+
onProgress: function (event)
130+
{
131+
var audio = event.target;
84132
85-
_this.onProcessComplete();
86-
},
87-
function (e)
133+
audio.oncanplaythrough = null;
134+
audio.onerror = null;
135+
136+
this.filesLoaded++;
137+
138+
this.percentComplete = Math.min((this.filesLoaded / this.filesTotal), 1);
139+
140+
this.loader.emit(Events.FILE_PROGRESS, this, this.percentComplete);
141+
142+
if (this.filesLoaded === this.filesTotal)
143+
{
144+
this.onLoad();
145+
}
146+
},
147+
*/
148+
149+
/**
150+
* Called by the Loader, starts the actual file downloading.
151+
* During the load the methods onLoad, onError and onProgress are called, based on the XHR events.
152+
* You shouldn't normally call this method directly, it's meant to be invoked by the Loader.
153+
*
154+
* @method Phaser.Loader.FileTypes.HTML5AudioFile#load
155+
* @since 3.0.0
156+
load: function ()
157+
{
158+
this.data = [];
159+
160+
var instances = (this.config && this.config.instances) || 1;
161+
162+
this.filesTotal = instances;
163+
this.filesLoaded = 0;
164+
this.percentComplete = 0;
165+
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';
172+
173+
if (this.locked)
88174
{
89-
// eslint-disable-next-line no-console
90-
console.error('Error decoding audio: ' + this.key + ' - ', e ? e.message : null);
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+
}
185+
186+
this.data.push(audio);
187+
}
188+
189+
for (i = 0; i < this.data.length; i++)
190+
{
191+
audio = this.data[i];
192+
audio.src = GetURL(this, this.loader.baseURL);
91193
92-
_this.onProcessError();
194+
if (!this.locked)
195+
{
196+
audio.load();
93197
}
94-
);
198+
}
95199
96-
this.config.context = null;
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));
205+
}
97206
}
207+
*/
98208

99209
});
100210

101-
VideoFile.create = function (loader, key, urls, config, xhrSettings)
211+
VideoFile.create = function (loader, key, urls, loadEvent, asBlob, xhrSettings)
102212
{
103213
var game = loader.systems.game;
104-
var audioConfig = game.config.audio;
105-
var deviceAudio = game.device.audio;
106214

107215
// url may be inside key, which may be an object
108216
if (IsPlainObject(key))
109217
{
110218
urls = GetFastValue(key, 'url', []);
111-
config = GetFastValue(key, 'config', {});
219+
loadEvent = GetFastValue(key, 'loadEvent', 'canplaythrough');
220+
asBlob = GetFastValue(key, 'asBlob', false);
221+
xhrSettings = GetFastValue(key, 'xhrSettings');
112222
}
113223

114-
var urlConfig = VideoFile.getAudioURL(game, urls);
224+
var urlConfig = VideoFile.getVideoURL(game, urls);
115225

116-
if (!urlConfig)
226+
if (urlConfig)
117227
{
118-
return null;
228+
return new VideoFile(loader, key, urlConfig, loadEvent, asBlob, xhrSettings);
119229
}
120-
121-
// https://developers.google.com/web/updates/2012/02/HTML5-audio-and-the-Web-Audio-API-are-BFFs
122-
// var stream = GetFastValue(config, 'stream', false);
123-
124-
// if (deviceAudio.webAudio && !(audioConfig && audioConfig.disableWebAudio))
125-
// {
126-
// return new VideoFile(loader, key, urlConfig, xhrSettings, game.sound.context);
127-
// }
128-
// else
129-
// {
130-
// return new HTML5VideoFile(loader, key, urlConfig, config);
131-
// }
132230
};
133231

134-
VideoFile.getAudioURL = function (game, urls)
232+
VideoFile.getVideoURL = function (game, urls)
135233
{
136234
if (!Array.isArray(urls))
137235
{
@@ -147,15 +245,15 @@ VideoFile.getAudioURL = function (game, urls)
147245
return url;
148246
}
149247

150-
var audioType = url.match(/\.([a-zA-Z0-9]+)($|\?)/);
248+
var videoType = url.match(/\.([a-zA-Z0-9]+)($|\?)/);
151249

152-
audioType = GetFastValue(urls[i], 'type', (audioType) ? audioType[1] : '').toLowerCase();
250+
videoType = GetFastValue(urls[i], 'type', (videoType) ? videoType[1] : '').toLowerCase();
153251

154-
if (game.device.audio[audioType])
252+
if (game.device.video[videoType])
155253
{
156254
return {
157255
url: url,
158-
type: audioType
256+
type: videoType
159257
};
160258
}
161259
}
@@ -221,40 +319,30 @@ VideoFile.getAudioURL = function (game, urls)
221319
*
222320
* @return {Phaser.Loader.LoaderPlugin} The Loader instance.
223321
*/
224-
FileTypesManager.register('video', function (key, urls, config, xhrSettings)
322+
FileTypesManager.register('video', function (key, urls, loadEvent, asBlob, xhrSettings)
225323
{
226-
// var game = this.systems.game;
227-
// var audioConfig = game.config.audio;
228-
// var deviceAudio = game.device.audio;
229-
230-
// if ((audioConfig && audioConfig.noAudio) || (!deviceAudio.webAudio && !deviceAudio.audioData))
231-
// {
232-
// Sounds are disabled, so skip loading audio
233-
// return this;
234-
// }
235-
236-
var audioFile;
324+
var videoFile;
237325

238326
if (Array.isArray(key))
239327
{
240328
for (var i = 0; i < key.length; i++)
241329
{
242330
// If it's an array it has to be an array of Objects, so we get everything out of the 'key' object
243-
audioFile = VideoFile.create(this, key[i]);
331+
videoFile = VideoFile.create(this, key[i]);
244332

245-
if (audioFile)
333+
if (videoFile)
246334
{
247-
this.addFile(audioFile);
335+
this.addFile(videoFile);
248336
}
249337
}
250338
}
251339
else
252340
{
253-
audioFile = VideoFile.create(this, key, urls, config, xhrSettings);
341+
videoFile = VideoFile.create(this, key, urls, loadEvent, asBlob, xhrSettings);
254342

255-
if (audioFile)
343+
if (videoFile)
256344
{
257-
this.addFile(audioFile);
345+
this.addFile(videoFile);
258346
}
259347
}
260348

0 commit comments

Comments
 (0)