Skip to content

Commit eedf951

Browse files
committed
rewrite onPlayerError function
1 parent 80847d5 commit eedf951

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

src/components/App.js

Lines changed: 30 additions & 14 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

@@ -186,12 +187,8 @@ export default class App extends React.Component {
186187
{
187188
audioConfig
188189
},
189-
function() {
190-
this.updateVolume();
191-
}
190+
this.updateVolume
192191
);
193-
194-
return this;
195192
}
196193

197194
fadeUp() {
@@ -344,18 +341,37 @@ export default class App extends React.Component {
344341
);
345342

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

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-
);
353+
const { mounts, remotes, erroredStreams, url } = this.state;
354+
const sortedStreams = this.sortStreams([...mounts, ...remotes]);
355+
356+
// Pause if all streams are in the errored list
357+
if (erroredStreams.length === sortedStreams.length) {
358+
this.pause();
359+
return;
360+
}
361+
362+
const availableStreams = sortedStreams.filter(stream => stream.url !== url);
354363
const currentStream = sortedStreams.find(stream => stream.url === url);
355-
sortedStreams.unshift(currentStream);
356364

357-
// Then play the next item in the array
358-
this.setUrl(sortedStreams[1].url);
365+
// If the url is already in the errored list, use another url
366+
if (erroredStreams.some(stream => stream.url === url)) {
367+
this.setUrl(availableStreams[0].url);
368+
} else {
369+
// Otherwise, add the url to the errored list, then use another url
370+
this.setState(
371+
{ erroredStreams: [...erroredStreams, currentStream] },
372+
() => this.setUrl(availableStreams[0].url)
373+
);
374+
}
359375
};
360376

361377
render() {

0 commit comments

Comments
 (0)