Skip to content

Commit 883e253

Browse files
committed
rewrite onPlayerError function
1 parent 80847d5 commit 883e253

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

src/components/App.js

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export default class App extends React.Component {
6060
mounts: [],
6161
remotes: [],
6262
playing: null,
63+
erroredStreams: [],
6364

6465
// Note: the crossOrigin is needed to fix a CORS JavaScript requirement
6566

@@ -182,16 +183,7 @@ export default class App extends React.Component {
182183
let audioConfig = { ...this.state.audioConfig };
183184
audioConfig.targetVolume =
184185
direction.toLowerCase() === "up" ? this.state.audioConfig.maxVolume : 0;
185-
this.setState(
186-
{
187-
audioConfig
188-
},
189-
function() {
190-
this.updateVolume();
191-
}
192-
);
193-
194-
return this;
186+
this.setState({ audioConfig }, this.updateVolume);
195187
}
196188

197189
fadeUp() {
@@ -344,18 +336,37 @@ export default class App extends React.Component {
344336
);
345337

346338
onPlayerError = () => {
347-
const { mounts, remotes, url } = this.state;
339+
/*
340+
* This error handler works as follows:
341+
* - When the player cannot play the url:
342+
* - If the url is already in the `erroredStreams` list: try another url
343+
* - If the url is not in `erroredStreams`: add the url to the list and try another url
344+
* - If `erroredStreams` has as many items as the list of available streams:
345+
* - Pause the player because this means all of our urls are having issues
346+
*/
348347

349-
// Get the stream list, sorted by bitrate, from high to low,
350-
// find and move the current url to the beginning of the array.
351-
const sortedStreams = this.sortStreams([...mounts, ...remotes]).filter(
352-
stream => stream.url !== url
353-
);
348+
const { mounts, remotes, erroredStreams, url } = this.state;
349+
const sortedStreams = this.sortStreams([...mounts, ...remotes]);
350+
351+
// Pause if all streams are in the errored list
352+
if (erroredStreams.length === sortedStreams.length) {
353+
this.pause();
354+
return;
355+
}
356+
357+
const availableStreams = sortedStreams.filter(stream => stream.url !== url);
354358
const currentStream = sortedStreams.find(stream => stream.url === url);
355-
sortedStreams.unshift(currentStream);
356359

357-
// Then play the next item in the array
358-
this.setUrl(sortedStreams[1].url);
360+
// If the url is already in the errored list, use another url
361+
if (erroredStreams.some(stream => stream.url === url)) {
362+
this.setUrl(availableStreams[0].url);
363+
} else {
364+
// Otherwise, add the url to the errored list, then use another url
365+
this.setState(
366+
{ erroredStreams: [...erroredStreams, currentStream] },
367+
() => this.setUrl(availableStreams[0].url)
368+
);
369+
}
359370
};
360371

361372
render() {

0 commit comments

Comments
 (0)