Skip to content

Commit

Permalink
Added check for media element error state on initialisation
Browse files Browse the repository at this point in the history
See #523
  • Loading branch information
chrisn committed Feb 20, 2024
1 parent 5d006c1 commit 08bf2da
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/mediaelement-player.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ MediaElementPlayer.prototype.init = function(eventEmitter) {
if (!mediaElementHasSource(self._mediaElement)) {
return Promise.resolve();
}
else if (self._mediaElement.error &&
self._mediaElement.error.code === MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED) {
// The media element has a source, but the format is not supported.
return Promise.reject(self._mediaElement.error);
}

return new Promise(function(resolve, reject) {
function eventHandler(event) {
Expand Down
34 changes: 34 additions & 0 deletions test/api-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,40 @@ describe('Peaks', function() {
});
});
});

context('with audio element in error state', function() {
let mediaElement;

beforeEach(function() {
mediaElement = document.createElement('audio');
mediaElement.id = 'adpcm';
mediaElement.src = '/base/test_data/adpcm.wav';
mediaElement.muted = true;
document.body.appendChild(mediaElement);
});

afterEach(function() {
document.body.removeChild(mediaElement);
});

it('should invoke callback with an error', function(done) {
Peaks.init({
overview: {
container: document.getElementById('overview-container')
},
zoomview: {
container: document.getElementById('zoomview-container')
},
mediaElement: document.getElementById('adpcm'),
dataUri: { arraybuffer: '/base/test_data/sample.dat' }
}, function(err, instance) {
expect(err).to.be.an.instanceOf(MediaError);
expect(err.code).to.equal(MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED);
expect(instance).to.equal(undefined);
done();
});
});
});
});

context('with invalid options', function() {
Expand Down
Binary file added test_data/adpcm.wav
Binary file not shown.

0 comments on commit 08bf2da

Please sign in to comment.