-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
audio-downloader.ts
118 lines (103 loc) · 3.96 KB
/
audio-downloader.ts
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
/*
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/**
* @category loader
*/
import { getError, log } from '../core/platform/debug';
import { sys } from '../core/platform/sys';
import { AudioClip, AudioType } from './assets/clip';
const __audioSupport = sys.__audioSupport;
const formatSupport = __audioSupport.format;
function loadDomAudio (item, callback) {
const dom = document.createElement('audio');
dom.src = item.url;
const clearEvent = () => {
clearTimeout(timer);
dom.removeEventListener('canplaythrough', success, false);
dom.removeEventListener('error', failure, false);
if (__audioSupport.USE_LOADER_EVENT) {
dom.removeEventListener(__audioSupport.USE_LOADER_EVENT, success, false);
}
};
const timer = setTimeout(() => {
if (dom.readyState === 0) {
failure();
} else {
success();
}
}, 8000);
const success = () => {
clearEvent();
callback(null, dom);
};
const failure = () => {
clearEvent();
const message = 'load audio failure - ' + item.url;
log(message);
callback(message);
};
dom.addEventListener('canplaythrough', success, false);
dom.addEventListener('error', failure, false);
if (__audioSupport.USE_LOADER_EVENT) {
dom.addEventListener(__audioSupport.USE_LOADER_EVENT, success, false);
}
}
function loadWebAudio (item, callback) {
const context = __audioSupport.context;
if (!context) {
callback(new Error(getError(4926)));
}
const request = cc.loader.getXMLHttpRequest();
request.open('GET', item.url, true);
request.responseType = 'arraybuffer';
// Our asynchronous callback
request.onload = () => {
context.decodeAudioData(request.response, (buffer) => {
// success
callback(null, buffer);
}, () => {
// error
callback('decode error - ' + item.id, null);
});
};
request.onerror = () => {
callback('request error - ' + item.id, null);
};
request.send();
}
export function downloadAudio (item, callback) {
if (formatSupport.length === 0) {
return new Error(getError(4927));
}
let audioLoader;
if (!__audioSupport.WEB_AUDIO) {
audioLoader = loadDomAudio; // If WebAudio is not supported, load using DOM mode
} else {
const loadByDeserializedAudio = item._owner instanceof AudioClip;
if (loadByDeserializedAudio) {
audioLoader = (item._owner.loadMode === AudioType.WEB_AUDIO) ? loadWebAudio : loadDomAudio;
} else {
audioLoader = (item.urlParam && item.urlParam.useDom) ? loadDomAudio : loadWebAudio;
}
}
audioLoader(item, callback);
}