|
5 | 5 | * @license {@link https://opensource.org/licenses/MIT|MIT License} |
6 | 6 | */ |
7 | 7 |
|
| 8 | +var Base64ToArrayBuffer = require('../../utils/base64/Base64ToArrayBuffer'); |
8 | 9 | var BaseSoundManager = require('../BaseSoundManager'); |
9 | 10 | var Class = require('../../utils/Class'); |
10 | 11 | var Events = require('../events'); |
@@ -133,6 +134,87 @@ var WebAudioSoundManager = new Class({ |
133 | 134 | return sound; |
134 | 135 | }, |
135 | 136 |
|
| 137 | + /** |
| 138 | + * Decode audio data into a format ready for playback via Web Audio. |
| 139 | + * |
| 140 | + * The audio data can be a base64 encoded string, an audio media-type data uri, or an ArrayBuffer instance. |
| 141 | + * |
| 142 | + * The `audioKey` is the key that will be used to save the decoded audio to the audio cache. |
| 143 | + * |
| 144 | + * Instead of passing a single entry you can instead pass an array of `Phaser.Types.Sound.DecodeAudioConfig` |
| 145 | + * objects as the first and only argument. |
| 146 | + * |
| 147 | + * Decoding is an async process, so be sure to listen for the events to know when decoding has completed. |
| 148 | + * |
| 149 | + * Once the audio has decoded it can be added to the Sound Manager or played via its key. |
| 150 | + * |
| 151 | + * @method Phaser.Sound.WebAudioSoundManager#decodeAudio |
| 152 | + * @fires Phaser.Sound.Events#DECODED |
| 153 | + * @fires Phaser.Sound.Events#DECODED_ALL |
| 154 | + * @since 3.18.0 |
| 155 | + * |
| 156 | + * @param {(Phaser.Types.Sound.DecodeAudioConfig[]|string)} [audioKey] - The string-based key to be used to reference the decoded audio in the audio cache, or an array of audio config objects. |
| 157 | + * @param {(ArrayBuffer|string)} [audioData] - The audio data, either a base64 encoded string, an audio media-type data uri, or an ArrayBuffer instance. |
| 158 | + */ |
| 159 | + decodeAudio: function (audioKey, audioData) |
| 160 | + { |
| 161 | + var audioFiles; |
| 162 | + |
| 163 | + if (!Array.isArray(audioKey)) |
| 164 | + { |
| 165 | + audioFiles = [ { key: audioKey, data: audioData } ]; |
| 166 | + } |
| 167 | + else |
| 168 | + { |
| 169 | + audioFiles = audioKey; |
| 170 | + } |
| 171 | + |
| 172 | + var cache = this.game.cache.audio; |
| 173 | + var remaining = audioFiles.length; |
| 174 | + |
| 175 | + for (var i = 0; i < audioFiles.length; i++) |
| 176 | + { |
| 177 | + var entry = audioFiles[i]; |
| 178 | + |
| 179 | + var key = entry.key; |
| 180 | + var data = entry.data; |
| 181 | + |
| 182 | + if (typeof data === 'string') |
| 183 | + { |
| 184 | + data = Base64ToArrayBuffer(data); |
| 185 | + } |
| 186 | + |
| 187 | + var success = function (key, audioBuffer) |
| 188 | + { |
| 189 | + cache.add(key, audioBuffer); |
| 190 | + |
| 191 | + this.emit(Events.DECODED, key); |
| 192 | + |
| 193 | + remaining--; |
| 194 | + |
| 195 | + if (remaining === 0) |
| 196 | + { |
| 197 | + this.emit(Events.DECODED_ALL); |
| 198 | + } |
| 199 | + }.bind(this, key); |
| 200 | + |
| 201 | + var failure = function (key, error) |
| 202 | + { |
| 203 | + // eslint-disable-next-line no-console |
| 204 | + console.error('Error decoding audio: ' + key + ' - ', error ? error.message : ''); |
| 205 | + |
| 206 | + remaining--; |
| 207 | + |
| 208 | + if (remaining === 0) |
| 209 | + { |
| 210 | + this.emit(Events.DECODED_ALL); |
| 211 | + } |
| 212 | + }.bind(this, key); |
| 213 | + |
| 214 | + this.context.decodeAudioData(data, success, failure); |
| 215 | + } |
| 216 | + }, |
| 217 | + |
136 | 218 | /** |
137 | 219 | * Unlocks Web Audio API on the initial input event. |
138 | 220 | * |
|
0 commit comments