diff --git a/CHANGELOG.md b/CHANGELOG.md index 8850263a8..a2e1cdec5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ CHANGELOG * @hartman added default print styling ([view](https://github.com/videojs/video.js/pull/3304)) * @ldayananda updated videojs to not do anything if no src is set ([view](https://github.com/videojs/video.js/pull/3378)) * @nickygerritsen removed unused tracks when changing sources. Fixes #3000 ([view](https://github.com/videojs/video.js/pull/3002)) +* @vit-koumar updated Flash tech to return Infinity from duration instead of -1 ([view](https://github.com/videojs/video.js/pull/3128)) -------------------- diff --git a/src/js/tech/flash.js b/src/js/tech/flash.js index 11bb2ceff..8bb2d31e3 100644 --- a/src/js/tech/flash.js +++ b/src/js/tech/flash.js @@ -229,6 +229,20 @@ class Flash extends Tech { } } + /** + * Get media duration + * + * @returns {Number} Media duration + */ + duration() { + if (this.readyState() === 0) { + return NaN; + } else { + let duration = this.el_.vjs_getProperty('duration'); + return duration >= 0 ? duration : Infinity; + } + } + /** * Load media into player * @@ -312,7 +326,7 @@ class Flash extends Tech { // Create setters and getters for attributes const _api = Flash.prototype; const _readWrite = 'rtmpConnection,rtmpStream,preload,defaultPlaybackRate,playbackRate,autoplay,loop,mediaGroup,controller,controls,volume,muted,defaultMuted'.split(','); -const _readOnly = 'networkState,readyState,initialTime,duration,startOffsetTime,paused,ended,videoWidth,videoHeight'.split(','); +const _readOnly = 'networkState,readyState,initialTime,startOffsetTime,paused,ended,videoWidth,videoHeight'.split(','); function _createSetter(attr){ var attrUpper = attr.charAt(0).toUpperCase() + attr.slice(1); diff --git a/test/unit/tech/flash.test.js b/test/unit/tech/flash.test.js index eadab9f93..229d78911 100644 --- a/test/unit/tech/flash.test.js +++ b/test/unit/tech/flash.test.js @@ -197,6 +197,33 @@ test('play after ended seeks to the beginning', function() { equal(seeks[0], 0, 'seeked to the beginning'); }); +test('duration returns NaN, Infinity or duration according to the HTML standard', function() { + let duration = Flash.prototype.duration; + let mockedDuration = -1; + let mockedReadyState = 0; + let result; + let mockFlash = { + el_: { + vjs_getProperty() { + return mockedDuration; + } + }, + readyState: function() { + return mockedReadyState; + } + }; + result = duration.call(mockFlash); + ok(Number.isNaN(result), 'duration returns NaN when readyState equals 0'); + + mockedReadyState = 1; + result = duration.call(mockFlash); + ok(!Number.isFinite(result), 'duration returns Infinity when duration property is less then 0'); + + mockedDuration = 1; + result = duration.call(mockFlash); + equal(result, 1, 'duration returns duration property when readeyState and duration property are both higher than 0'); +}); + // fake out the interaction but leave all the other logic intact class MockFlash extends Flash { constructor() {