forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTML5AudioFile.js
More file actions
142 lines (112 loc) · 3.29 KB
/
HTML5AudioFile.js
File metadata and controls
142 lines (112 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var Class = require('../../utils/Class');
var File = require('../File');
var GetFastValue = require('../../utils/object/GetFastValue');
var GetURL = require('../GetURL');
/**
* @classdesc
* [description]
*
* @class HTML5AudioFile
* @extends Phaser.Loader.File
* @memberOf Phaser.Loader.FileTypes
* @constructor
* @since 3.0.0
*
* @param {string} key - [description]
* @param {string} url - [description]
* @param {string} path - [description]
* @param {object} config - [description]
* @param {boolean} locked - [description]
*/
var HTML5AudioFile = new Class({
Extends: File,
initialize:
function HTML5AudioFile (key, url, path, config, locked)
{
this.locked = locked;
this.loaded = false;
var fileConfig = {
type: 'audio',
extension: GetFastValue(url, 'type', ''),
key: key,
url: GetFastValue(url, 'uri', url),
path: path,
config: config
};
File.call(this, fileConfig);
},
onLoad: function ()
{
if(this.loaded)
{
return;
}
this.loaded = true;
this.loader.nextFile(this, true);
},
onError: function ()
{
for (var i = 0; i < this.data.length; i++)
{
var audio = this.data[i];
audio.oncanplaythrough = null;
audio.onerror = null;
}
this.loader.nextFile(this, false);
},
onProgress: function (event)
{
var audio = event.target;
audio.oncanplaythrough = null;
audio.onerror = null;
this.filesLoaded++;
this.percentComplete = Math.min((this.filesLoaded / this.filesTotal), 1);
this.loader.emit('fileprogress', this, this.percentComplete);
if(this.filesLoaded === this.filesTotal)
{
this.onLoad();
}
},
// Called by the Loader, starts the actual file downloading
load: function (loader)
{
this.loader = loader;
this.data = [];
var instances = (this.config && this.config.instances) || 1;
this.filesTotal = instances;
this.filesLoaded = 0;
this.percentComplete = 0;
for(var i = 0; i < instances; i++)
{
var audio = new Audio();
audio.dataset.name = this.key + ('0' + i).slice(-2); // Useful for debugging
audio.dataset.used = 'false';
if (!this.locked)
{
audio.preload = 'auto';
audio.oncanplaythrough = this.onProgress.bind(this);
audio.onerror = this.onError.bind(this);
}
this.data.push(audio);
}
for (i = 0; i < this.data.length; i++)
{
audio = this.data[i];
audio.src = GetURL(this, loader.baseURL);
if (!this.locked)
{
audio.load();
}
}
if (this.locked)
{
setTimeout(this.onLoad.bind(this));
}
}
});
module.exports = HTML5AudioFile;