Skip to content

Commit a7ad26c

Browse files
committed
Change xhrHeaders to xhr and include method and withCredentials
This introduces a small breaking change, but I think it is worth it to bundle all XHR-related properties under a single property.
1 parent 894b1e9 commit a7ad26c

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

README.md

+16-6
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,24 @@ The rate of playback. 0.5 to 4.0, with 1.0 being normal speed.
213213
The size of the inactive sounds pool. Once sounds are stopped or finish playing, they are marked as ended and ready for cleanup. We keep a pool of these to recycle for improved performance. Generally this doesn't need to be changed. It is important to keep in mind that when a sound is paused, it won't be removed from the pool and will still be considered active so that it can be resumed later.
214214
#### format `Array` `[]`
215215
howler.js automatically detects your file format from the extension, but you may also specify a format in situations where extraction won't work (such as with a SoundCloud stream).
216-
#### xhrWithCredentials `Boolean` `false`
217-
Whether or not to enable the `withCredentials` flag on XHR requests used to fetch audio files when using Web Audio API ([see reference](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials)).
218-
#### xhrHeaders `Object` `null`
219-
When using Web Audio, howler.js uses an XHR request to load the audio files. If you need to send custom headers with this request, include them with this parameter. For example:
216+
#### xhr `Object` `null`
217+
When using Web Audio, howler.js uses an XHR request to load the audio files. If you need to send custom headers, set the HTTP method or enable `withCredentials` ([see reference](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials)), include them with this parameter. Each is optional (method defaults to `GET`, headers default to `null` and withCredentials defaults to `false`). For example:
220218
```javascript
219+
// Using each of the properties.
221220
new Howl({
222-
xhrHeaders: {
223-
Authorization: 'Bearer:' + token,
221+
xhr: {
222+
method: 'POST',
223+
headers: {
224+
Authorization: 'Bearer:' + token,
225+
},
226+
withCredentials: true,
227+
}
228+
});
229+
230+
// Only changing the method.
231+
new Howl({
232+
xhr: {
233+
method: 'POST',
224234
}
225235
});
226236
```

src/howler.core.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@
483483
self._suspendTimer = null;
484484
self.state = 'suspending';
485485

486+
// Handle updating the state of the audio context after suspending.
486487
var handleSuspension = function() {
487488
self.state = 'suspended';
488489

@@ -492,8 +493,8 @@
492493
}
493494
};
494495

495-
// Either suspension is resolved or rejected (i.e. in case of interrupted state of audio context)
496-
// the Howler's 'suspending' state needs to be updated.
496+
// Either the state gets suspended or it is interrupted.
497+
// Either way, we need to update the state to suspended.
497498
self.ctx.suspend().then(handleSuspension, handleSuspension);
498499
}, 30000);
499500

@@ -583,8 +584,11 @@
583584
self._sprite = o.sprite || {};
584585
self._src = (typeof o.src !== 'string') ? o.src : [o.src];
585586
self._volume = o.volume !== undefined ? o.volume : 1;
586-
self._xhrWithCredentials = o.xhrWithCredentials || false;
587-
self._xhrHeaders = o.xhrHeaders || null;
587+
self._xhr = {
588+
method: o.xhr && o.xhr.method ? o.xhr.method : 'GET',
589+
headers: o.xhr && o.xhr.headers ? o.xhr.headers : null,
590+
withCredentials: o.xhr && o.xhr.withCredentials ? o.xhr.withCredentials : false,
591+
};
588592

589593
// Setup all other default properties.
590594
self._duration = 0;
@@ -2349,14 +2353,14 @@
23492353
} else {
23502354
// Load the buffer from the URL.
23512355
var xhr = new XMLHttpRequest();
2352-
xhr.open('GET', url, true);
2353-
xhr.withCredentials = self._xhrWithCredentials;
2356+
xhr.open(self._xhr.method, url, true);
2357+
xhr.withCredentials = self._xhr.withCredentials;
23542358
xhr.responseType = 'arraybuffer';
23552359

23562360
// Apply any custom headers to the request.
2357-
if (self._xhrHeaders) {
2358-
Object.keys(self._xhrHeaders).forEach(function(key) {
2359-
xhr.setRequestHeader(key, self._xhrHeaders[key]);
2361+
if (self._xhr.headers) {
2362+
Object.keys(self._xhr.headers).forEach(function(key) {
2363+
xhr.setRequestHeader(key, self._xhr.headers[key]);
23602364
});
23612365
}
23622366

0 commit comments

Comments
 (0)