|
| 1 | +/** |
| 2 | + * @author Richard Davey <rich@photonstorm.com> |
| 3 | + * @copyright 2019 Photon Storm Ltd. |
| 4 | + * @license {@link https://opensource.org/licenses/MIT|MIT License} |
| 5 | + */ |
| 6 | + |
| 7 | +var Class = require('../../utils/Class'); |
| 8 | +var CONST = require('../../const'); |
| 9 | +var File = require('../File'); |
| 10 | +var FileTypesManager = require('../FileTypesManager'); |
| 11 | +var GetFastValue = require('../../utils/object/GetFastValue'); |
| 12 | +var IsPlainObject = require('../../utils/object/IsPlainObject'); |
| 13 | + |
| 14 | +/** |
| 15 | + * @classdesc |
| 16 | + * A single Video File suitable for loading by the Loader. |
| 17 | + * |
| 18 | + * These are created when you use the Phaser.Loader.LoaderPlugin#video method and are not typically created directly. |
| 19 | + * |
| 20 | + * For documentation about what all the arguments and configuration options mean please see Phaser.Loader.LoaderPlugin#video. |
| 21 | + * |
| 22 | + * @class VideoFile |
| 23 | + * @extends Phaser.Loader.File |
| 24 | + * @memberof Phaser.Loader.FileTypes |
| 25 | + * @constructor |
| 26 | + * @since 3.20.0 |
| 27 | + * |
| 28 | + * @param {Phaser.Loader.LoaderPlugin} loader - A reference to the Loader that is responsible for this file. |
| 29 | + * @param {(string|Phaser.Types.Loader.FileTypes.VideoFileConfig)} key - The key to use for this file, or a file configuration object. |
| 30 | + * @param {any} [urlConfig] - The absolute or relative URL to load this file from in a config object. |
| 31 | + * @param {Phaser.Types.Loader.XHRSettingsObject} [xhrSettings] - Extra XHR Settings specifically for this file. |
| 32 | + * @param {AudioContext} [audioContext] - The AudioContext this file will use to process itself. |
| 33 | + */ |
| 34 | +var VideoFile = new Class({ |
| 35 | + |
| 36 | + Extends: File, |
| 37 | + |
| 38 | + initialize: |
| 39 | + |
| 40 | + // URL is an object created by VideoFile.findAudioURL |
| 41 | + function VideoFile (loader, key, urlConfig, xhrSettings, audioContext) |
| 42 | + { |
| 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 | + |
| 52 | + var fileConfig = { |
| 53 | + type: 'video', |
| 54 | + cache: loader.cacheManager.video, |
| 55 | + extension: urlConfig.type, |
| 56 | + responseType: 'arraybuffer', |
| 57 | + key: key, |
| 58 | + url: urlConfig.url, |
| 59 | + xhrSettings: xhrSettings, |
| 60 | + config: { context: audioContext } |
| 61 | + }; |
| 62 | + |
| 63 | + File.call(this, loader, fileConfig); |
| 64 | + }, |
| 65 | + |
| 66 | + /** |
| 67 | + * Called automatically by Loader.nextFile. |
| 68 | + * This method controls what extra work this File does with its loaded data. |
| 69 | + * |
| 70 | + * @method Phaser.Loader.FileTypes.VideoFile#onProcess |
| 71 | + * @since 3.20.0 |
| 72 | + */ |
| 73 | + onProcess: function () |
| 74 | + { |
| 75 | + this.state = CONST.FILE_PROCESSING; |
| 76 | + |
| 77 | + var _this = this; |
| 78 | + |
| 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; |
| 84 | + |
| 85 | + _this.onProcessComplete(); |
| 86 | + }, |
| 87 | + function (e) |
| 88 | + { |
| 89 | + // eslint-disable-next-line no-console |
| 90 | + console.error('Error decoding audio: ' + this.key + ' - ', e ? e.message : null); |
| 91 | + |
| 92 | + _this.onProcessError(); |
| 93 | + } |
| 94 | + ); |
| 95 | + |
| 96 | + this.config.context = null; |
| 97 | + } |
| 98 | + |
| 99 | +}); |
| 100 | + |
| 101 | +VideoFile.create = function (loader, key, urls, config, xhrSettings) |
| 102 | +{ |
| 103 | + var game = loader.systems.game; |
| 104 | + var audioConfig = game.config.audio; |
| 105 | + var deviceAudio = game.device.audio; |
| 106 | + |
| 107 | + // url may be inside key, which may be an object |
| 108 | + if (IsPlainObject(key)) |
| 109 | + { |
| 110 | + urls = GetFastValue(key, 'url', []); |
| 111 | + config = GetFastValue(key, 'config', {}); |
| 112 | + } |
| 113 | + |
| 114 | + var urlConfig = VideoFile.getAudioURL(game, urls); |
| 115 | + |
| 116 | + if (!urlConfig) |
| 117 | + { |
| 118 | + return null; |
| 119 | + } |
| 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 | + // } |
| 132 | +}; |
| 133 | + |
| 134 | +VideoFile.getAudioURL = function (game, urls) |
| 135 | +{ |
| 136 | + if (!Array.isArray(urls)) |
| 137 | + { |
| 138 | + urls = [ urls ]; |
| 139 | + } |
| 140 | + |
| 141 | + for (var i = 0; i < urls.length; i++) |
| 142 | + { |
| 143 | + var url = GetFastValue(urls[i], 'url', urls[i]); |
| 144 | + |
| 145 | + if (url.indexOf('blob:') === 0 || url.indexOf('data:') === 0) |
| 146 | + { |
| 147 | + return url; |
| 148 | + } |
| 149 | + |
| 150 | + var audioType = url.match(/\.([a-zA-Z0-9]+)($|\?)/); |
| 151 | + |
| 152 | + audioType = GetFastValue(urls[i], 'type', (audioType) ? audioType[1] : '').toLowerCase(); |
| 153 | + |
| 154 | + if (game.device.audio[audioType]) |
| 155 | + { |
| 156 | + return { |
| 157 | + url: url, |
| 158 | + type: audioType |
| 159 | + }; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + return null; |
| 164 | +}; |
| 165 | + |
| 166 | +/** |
| 167 | + * Adds an Audio or HTML5Audio file, or array of audio files, to the current load queue. |
| 168 | + * |
| 169 | + * You can call this method from within your Scene's `preload`, along with any other files you wish to load: |
| 170 | + * |
| 171 | + * ```javascript |
| 172 | + * function preload () |
| 173 | + * { |
| 174 | + * this.load.audio('title', [ 'music/Title.ogg', 'music/Title.mp3', 'music/Title.m4a' ]); |
| 175 | + * } |
| 176 | + * ``` |
| 177 | + * |
| 178 | + * The file is **not** loaded right away. It is added to a queue ready to be loaded either when the loader starts, |
| 179 | + * or if it's already running, when the next free load slot becomes available. This happens automatically if you |
| 180 | + * are calling this from within the Scene's `preload` method, or a related callback. Because the file is queued |
| 181 | + * it means you cannot use the file immediately after calling this method, but must wait for the file to complete. |
| 182 | + * The typical flow for a Phaser Scene is that you load assets in the Scene's `preload` method and then when the |
| 183 | + * Scene's `create` method is called you are guaranteed that all of those assets are ready for use and have been |
| 184 | + * loaded. |
| 185 | + * |
| 186 | + * The key must be a unique String. It is used to add the file to the global Audio Cache upon a successful load. |
| 187 | + * The key should be unique both in terms of files being loaded and files already present in the Audio Cache. |
| 188 | + * Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file |
| 189 | + * then remove it from the Audio Cache first, before loading a new one. |
| 190 | + * |
| 191 | + * Instead of passing arguments you can pass a configuration object, such as: |
| 192 | + * |
| 193 | + * ```javascript |
| 194 | + * this.load.audio({ |
| 195 | + * key: 'title', |
| 196 | + * url: [ 'music/Title.ogg', 'music/Title.mp3', 'music/Title.m4a' ] |
| 197 | + * }); |
| 198 | + * ``` |
| 199 | + * |
| 200 | + * See the documentation for `Phaser.Types.Loader.FileTypes.VideoFileConfig` for more details. |
| 201 | + * |
| 202 | + * The URLs can be relative or absolute. If the URLs are relative the `Loader.baseURL` and `Loader.path` values will be prepended to them. |
| 203 | + * |
| 204 | + * Due to different browsers supporting different audio file types you should usually provide your audio files in a variety of formats. |
| 205 | + * ogg, mp3 and m4a are the most common. If you provide an array of URLs then the Loader will determine which _one_ file to load based on |
| 206 | + * browser support. |
| 207 | + * |
| 208 | + * If audio has been disabled in your game, either via the game config, or lack of support from the device, then no audio will be loaded. |
| 209 | + * |
| 210 | + * Note: The ability to load this type of file will only be available if the Audio File type has been built into Phaser. |
| 211 | + * It is available in the default build but can be excluded from custom builds. |
| 212 | + * |
| 213 | + * @method Phaser.Loader.LoaderPlugin#video |
| 214 | + * @fires Phaser.Loader.LoaderPlugin#addFileEvent |
| 215 | + * @since 3.20.0 |
| 216 | + * |
| 217 | + * @param {(string|Phaser.Types.Loader.FileTypes.VideoFileConfig|Phaser.Types.Loader.FileTypes.VideoFileConfig[])} key - The key to use for this file, or a file configuration object, or array of them. |
| 218 | + * @param {(string|string[])} [urls] - The absolute or relative URL to load the audio files from. |
| 219 | + * @param {any} [config] - An object containing an `instances` property for HTML5Audio. Defaults to 1. |
| 220 | + * @param {Phaser.Types.Loader.XHRSettingsObject} [xhrSettings] - An XHR Settings configuration object. Used in replacement of the Loaders default XHR Settings. |
| 221 | + * |
| 222 | + * @return {Phaser.Loader.LoaderPlugin} The Loader instance. |
| 223 | + */ |
| 224 | +FileTypesManager.register('video', function (key, urls, config, xhrSettings) |
| 225 | +{ |
| 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; |
| 237 | + |
| 238 | + if (Array.isArray(key)) |
| 239 | + { |
| 240 | + for (var i = 0; i < key.length; i++) |
| 241 | + { |
| 242 | + // 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]); |
| 244 | + |
| 245 | + if (audioFile) |
| 246 | + { |
| 247 | + this.addFile(audioFile); |
| 248 | + } |
| 249 | + } |
| 250 | + } |
| 251 | + else |
| 252 | + { |
| 253 | + audioFile = VideoFile.create(this, key, urls, config, xhrSettings); |
| 254 | + |
| 255 | + if (audioFile) |
| 256 | + { |
| 257 | + this.addFile(audioFile); |
| 258 | + } |
| 259 | + } |
| 260 | + |
| 261 | + return this; |
| 262 | +}); |
| 263 | + |
| 264 | +module.exports = VideoFile; |
0 commit comments