-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(autoplay): manage autoplay promise and allowMutedAutoPlay flag (#…
…144)
- Loading branch information
Showing
14 changed files
with
300 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
## Managing Autoplay on the Player | ||
If you would to start playing your content automatically, simply set the value of the player configuration `playback.autoplay` accordingly. | ||
|
||
>### playback.autoplay | ||
>##### Type: `boolean` | ||
>##### Default: `false` | ||
>##### Description: Start playback automatically. | ||
>If set to `true`, playback will start automatically when the player receives the content. If set to `false`, a user action will be required to start playback. | ||
#### Example - Basic usage: | ||
```js | ||
var config = { | ||
playback: { | ||
autoplay: true | ||
}, | ||
sources: {...} | ||
}; | ||
// No additional code required - playback will begin automatically | ||
var player = Playkit.loadPlayer(config); | ||
``` | ||
|
||
>**_Note_**: | ||
> Some developers simulate the autoplay behavior by calling the play() method right after creating the player without manipulating the player configuration. However, we strongly recommend not doing this! | ||
>```js | ||
>var config = { | ||
> sources: {...} | ||
>}; | ||
>var player = Playkit.loadPlayer(config); | ||
>// Bad practice! | ||
>player.play(); | ||
>``` | ||
> #### Why it's bad? | ||
> When you use the method described above, the player has no way of knowing whether the call to `play()` has been implemented via user action or via an API call. This can cause unexpected behavior in browsers that do not support autoplay. | ||
### What if the browser does not allow autoplay? | ||
On some platforms, web browsers do not allow to playback to begin automatically. The platforms that block attempts at playback are mostly targeted at web browsers on mobile devices; however, some desktop browsers have also started to align these restrictions to create a unified behavior between platforms (currently Safari11 and beginning with Chrome64). | ||
#### The Good News | ||
These restrictions apply only to automatic playback with sound, but not to automatic playback without sound. | ||
The PlayKitJS core can manage this logic on its own, and identify whether the current environment does not support autoplay with sound. In this case, the player will apply the autoplay with sound. | ||
To use this feature, you'll need to set the `playback.allowMutedAutoPlay` accordingly to the desired behavior. | ||
>### playback.allowMutedAutoPlay | ||
>##### Type: `boolean` | ||
>##### Default: `true` | ||
>##### Description: Start playback automatically without sound if autoplay with sound is not allowed. | ||
>If set to `true` and the run-time browser blocks autoplay, autoplay will begin muted until any user action is executed on the player, which will then unmute playback. If set to `false` and the run-time browser blocks autoplay, playback will not start automatically. | ||
The following matrix summarizes the results for configuring `playback.allowMutedAutoPlay` when `playback.autoplay=true`: | ||
| Browser Policy | `allowMutedAutoPlay=true` |` allowMutedAutoPlay=false` | ||
| ----------------- | ----------------- | ------ | | ||
| Blocks autoplay | Muted autoplay | No autoplay (user action is required) | | ||
| Allows autoplay | Autoplay with/without sound according to the player configuration | Autoplay with/without sound according to the player configuration | ||
#### Example - Basic usage: | ||
```js | ||
var config = { | ||
playback: { | ||
autoplay: true, | ||
allowMutedAutoPlay: true | ||
}, | ||
sources: {...} | ||
}; | ||
// If browser blocks autoplay, playback will start muted | ||
var player = Playkit.loadPlayer(config); | ||
``` | ||
#### Example - Basic usage 2: | ||
```js | ||
var config = { | ||
playback: { | ||
autoplay: true, | ||
allowMutedAutoPlay: false | ||
}, | ||
sources: {...} | ||
}; | ||
// If browser blocks autoplay, playback will not start automatically | ||
var player = Playkit.loadPlayer(config); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
//@flow | ||
declare type CapabilityResult = { [capabilityName: string]: any }; | ||
|
||
declare interface ICapability { | ||
static runCapability(): void; | ||
static getCapability(): Promise<CapabilityResult>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"Base64Mp4Source": "data:video/mp4;base64,AAAAIGZ0eXBpc29tAAACAGlzb21pc28yYXZjMW1wNDEAAAAIZnJlZQAABPptZGF0AAACrQYF//+p3EXpvebZSLeWLNgg2SPu73gyNjQgLSBjb3JlIDE0OCByMjcyMSA3MmQ1M2FiIC0gSC4yNjQvTVBFRy00IEFWQyBjb2RlYyAtIENvcHlsZWZ0IDIwMDMtMjAxNiAtIGh0dHA6Ly93d3cudmlkZW9sYW4ub3JnL3gyNjQuaHRtbCAtIG9wdGlvbnM6IGNhYmFjPTEgcmVmPTMgZGVibG9jaz0xOjA6MCBhbmFseXNlPTB4MzoweDExMyBtZT1oZXggc3VibWU9NyBwc3k9MSBwc3lfcmQ9MS4wMDowLjAwIG1peGVkX3JlZj0xIG1lX3JhbmdlPTE2IGNocm9tYV9tZT0xIHRyZWxsaXM9MSA4eDhkY3Q9MSBjcW09MCBkZWFkem9uZT0yMSwxMSBmYXN0X3Bza2lwPTEgY2hyb21hX3FwX29mZnNldD00IHRocmVhZHM9MSBsb29rYWhlYWRfdGhyZWFkcz0xIHNsaWNlZF90aHJlYWRzPTAgbnI9MCBkZWNpbWF0ZT0xIGludGVybGFjZWQ9MCBibHVyYXlfY29tcGF0PTAgY29uc3RyYWluZWRfaW50cmE9MCBiZnJhbWVzPTMgYl9weXJhbWlkPTIgYl9hZGFwdD0xIGJfYmlhcz0wIGRpcmVjdD0xIHdlaWdodGI9MSBvcGVuX2dvcD0wIHdlaWdodHA9MiBrZXlpbnQ9MjUwIGtleWludF9taW49MjUgc2NlbmVjdXQ9NDAgaW50cmFfcmVmcmVzaD0wIHJjX2xvb2thaGVhZD00MCByYz1jcmYgbWJ0cmVlPTEgY3JmPTIzLjAgcWNvbXA9MC42MCBxcG1pbj0wIHFwbWF4PTY5IHFwc3RlcD00IGlwX3JhdGlvPTEuNDAgYXE9M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AYAAAAGAAAABgAAAAYAAAAGAAAABgAAAAYAAAAGAAAABgAAAAYAAAAGAAAABgAAAAYAAAAGAAAABgAAAAYAAAAGAAAABgAAAAYAAAAGAAAAaHN0Y28AAAAAAAAAFgAAAvUAAAMkAAADNgAAA0gAAANuAAADgAAAA5IAAAO4AAADygAAA+4AAAQCAAAEFAAABDgAAARMAAAEagAABIIAAASWAAAEtAAABMwAAATsAAAE/gAABRYAAABidWR0YQAAAFptZXRhAAAAAAAAACFoZGxyAAAAAAAAAABtZGlyYXBwbAAAAAAAAAAAAAAAAC1pbHN0AAAAJal0b28AAAAdZGF0YQAAAAEAAAAATGF2ZjU3LjU2LjEwMA==" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// @flow | ||
import * as Utils from '../../../utils/util' | ||
import * as EncodingSources from '../../../assets/encoding-sources.json' | ||
|
||
export default class Html5AutoPlayCapability implements ICapability { | ||
static _vid: HTMLVideoElement = Utils.Dom.createElement('video'); | ||
static _promise: Promise<*>; | ||
|
||
/*** | ||
* Runs the test for autoplay capability. | ||
* @public | ||
* @static | ||
* @returns {void} | ||
*/ | ||
static runCapability(): void { | ||
try { | ||
Html5AutoPlayCapability._vid.src = EncodingSources.Base64Mp4Source; | ||
Html5AutoPlayCapability._promise = Html5AutoPlayCapability._vid.play() || Promise.resolve(); | ||
} catch (e) { | ||
Html5AutoPlayCapability._promise = Promise.reject(); | ||
} | ||
} | ||
|
||
/** | ||
* Gets the test result for autoplay capability. | ||
* @returns {Promise<CapabilityResult>} - The result object for autoplay capability. | ||
* @static | ||
* @public | ||
*/ | ||
static getCapability(): Promise<CapabilityResult> { | ||
return Html5AutoPlayCapability._promise | ||
.then(() => ({autoplay: true})) | ||
.catch(() => ({autoplay: false})); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// @flow | ||
import * as Utils from '../../../utils/util' | ||
|
||
export default class Html5IsSupportedCapability implements ICapability { | ||
static _vid: HTMLVideoElement = Utils.Dom.createElement('video'); | ||
static _result: boolean; | ||
|
||
/*** | ||
* Runs the test for isSupported capability. | ||
* @public | ||
* @static | ||
* @returns {void} | ||
*/ | ||
static runCapability(): void { | ||
try { | ||
Html5IsSupportedCapability._vid.volume = 0.5; | ||
Html5IsSupportedCapability._result = !!Html5IsSupportedCapability._vid.canPlayType; | ||
} catch (e) { | ||
Html5IsSupportedCapability._result = false; | ||
} | ||
} | ||
|
||
/** | ||
* Gets the test result for isSupported capability. | ||
* @returns {Promise<CapabilityResult>} - The result object for isSupported capability. | ||
* @static | ||
* @public | ||
*/ | ||
static getCapability(): Promise<CapabilityResult> { | ||
return Promise.resolve({isSupported: Html5IsSupportedCapability._result}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.