Skip to content

Commit 64e55f5

Browse files
feat: Add audioPosterMode option (#7629)
1 parent f588317 commit 64e55f5

File tree

4 files changed

+80
-10
lines changed

4 files changed

+80
-10
lines changed

docs/guides/options.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* [width](#width)
2222
* [Video.js-specific Options](#videojs-specific-options)
2323
* [aspectRatio](#aspectratio)
24+
* [audioPosterMode](#audiopostermode)
2425
* [autoSetup](#autosetup)
2526
* [breakpoints](#breakpoints)
2627
* [children](#children)
@@ -181,6 +182,13 @@ Puts the player in [fluid](#fluid) mode and the value is used when calculating t
181182

182183
Alternatively, the classes `vjs-16-9`, `vjs-9-16`, `vjs-4-3` or `vjs-1-1` can be added to the player.
183184

185+
### `audioPosterMode`
186+
187+
> Type: `boolean`
188+
> Default: `false`
189+
190+
If set to true, it enables the poster viewer experience by hiding the video element and displaying the poster image persistently. This option can be set to `true` or `false` by calling `audioPosterMode([true|false])` at runtime.
191+
184192
### `autoSetup`
185193

186194
> Type: `boolean`

src/css/components/_poster.scss

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,14 @@
1616
height: 100%;
1717
}
1818

19-
// Hide the poster after the video has started playing
20-
.vjs-has-started .vjs-poster {
19+
// Hide the poster after the video has started playing and when native controls are used
20+
.vjs-has-started .vjs-poster,
21+
.vjs-using-native-controls .vjs-poster {
2122
display: none;
2223
}
2324

24-
// Don't hide the poster if we're playing audio
25-
.vjs-audio.vjs-has-started .vjs-poster {
25+
// Don't hide the poster if we're playing audio or when audio-poster-mode is true
26+
.vjs-audio.vjs-has-started .vjs-poster,
27+
.vjs-has-started.vjs-audio-poster-mode .vjs-poster {
2628
display: block;
2729
}
28-
29-
// Hide the poster when native controls are used otherwise it covers them
30-
.vjs-using-native-controls .vjs-poster {
31-
display: none;
32-
}

src/js/player.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4293,6 +4293,37 @@ class Player extends Component {
42934293
return !!this.isAudio_;
42944294
}
42954295

4296+
/**
4297+
* Get the current audioPosterMode state or set audioPosterMode to true or false
4298+
*
4299+
* @param {boolean} [value]
4300+
* The value to set audioPosterMode to.
4301+
*
4302+
* @return {boolean}
4303+
* True if audioPosterMode is on, false otherwise.
4304+
*/
4305+
audioPosterMode(value) {
4306+
4307+
if (this.audioPosterMode_ === undefined) {
4308+
this.audioPosterMode_ = this.options_.audioPosterMode;
4309+
}
4310+
4311+
if (typeof value !== 'boolean' || value === this.audioPosterMode_) {
4312+
return this.audioPosterMode_;
4313+
}
4314+
4315+
this.audioPosterMode_ = value;
4316+
4317+
if (this.audioPosterMode_) {
4318+
this.tech_.hide();
4319+
this.addClass('vjs-audio-poster-mode');
4320+
return;
4321+
}
4322+
// Show the video element and hide the poster image if audioPosterMode is set to false
4323+
this.tech_.show();
4324+
this.removeClass('vjs-audio-poster-mode');
4325+
}
4326+
42964327
/**
42974328
* A helper method for adding a {@link TextTrack} to our
42984329
* {@link TextTrackList}.
@@ -5099,7 +5130,8 @@ Player.prototype.options_ = {
50995130
},
51005131

51015132
breakpoints: {},
5102-
responsive: false
5133+
responsive: false,
5134+
audioPosterMode: false
51035135
};
51045136

51055137
[

test/unit/player.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,6 +1570,39 @@ QUnit.test('should add an audio player region if an audio el is used', function(
15701570
player.dispose();
15711571
});
15721572

1573+
QUnit.test('default audioPosterMode value at player creation', function(assert) {
1574+
const player = TestHelpers.makePlayer({});
1575+
1576+
assert.ok(player.audioPosterMode() === false, 'audioPosterMode is false by default');
1577+
1578+
const player2 = TestHelpers.makePlayer({
1579+
audioPosterMode: true
1580+
});
1581+
1582+
assert.ok(player2.audioPosterMode() === true, 'audioPosterMode can be set to true when the player is created');
1583+
player.dispose();
1584+
player2.dispose();
1585+
});
1586+
1587+
QUnit.test('get and set audioPosterMode value', function(assert) {
1588+
const player = TestHelpers.makePlayer({});
1589+
1590+
player.audioPosterMode(true);
1591+
assert.ok(player.audioPosterMode() === true, 'audioPosterMode is set to true');
1592+
});
1593+
1594+
QUnit.test('vjs-audio-poster-mode class and video element visibility when audioPosterMode is true', function(assert) {
1595+
const player = TestHelpers.makePlayer({});
1596+
1597+
player.audioPosterMode(true);
1598+
assert.ok(player.el().className.indexOf('vjs-audio-poster-mode') !== -1, 'vjs-audio-poster-mode class is present');
1599+
assert.ok(player.tech_.el().className.indexOf('vjs-hidden') !== -1, 'video element is hidden');
1600+
1601+
player.audioPosterMode(false);
1602+
assert.ok(player.el().className.indexOf('vjs-audio-poster-mode') === -1, 'vjs-audio-poster-mode class is removed');
1603+
assert.ok(player.tech_.el().className.indexOf('vjs-hidden') === -1, 'video element is visible');
1604+
});
1605+
15731606
QUnit.test('should setScrubbing when seeking or not seeking', function(assert) {
15741607
const player = TestHelpers.makePlayer();
15751608
let isScrubbing;

0 commit comments

Comments
 (0)