Techdocs

AMP Web SDK

AMP player

Autoplay

Basic autoplay functionality can be enabled via the player configuration object:

var config = {
    autoplay: true
};
var amp = akamai.amp.AMP.create("amp", config);

In this configuration the player will autoplay the media only on platforms that support unmuted playback. If the player encounters a platform that cannot autoplay, but an autoplay value of true is present in the config, an autoplayblocked event is dispatched:

amp.addEventListener("autoplayblocked", function (event) {
    var mode = event.detail.mode;
    var threshold = event.detail.threshold;
    console.log("Attemted autoplay '" + mode + "', but the browser's threshold is '" + threshold + "'")
})

The player can be configured to attempt muted, or muted inline, playback by providing an autoplayPolicy:

var config = {
    autoplay: true,
    autoplayPolicy: "muted"
};
var amp = akamai.amp.AMP.create("amp", config);

The values for autoplayPolicy are defined in the akamai.amp.AutoplayPolicy class and their values represent the following functionality:

  • "default": The default autoplayPolicy. The player will use the browser's default behavior for autoplay, i.e. the player will only autoplay on platforms that support unrestricted playback.
  • "muted": Player will mute on platforms that only allow muted playback.
  • "mutedinline": Player will mute and inline on platforms that only allow muted inline playback.
  • "allowed": Player will attempt to autoplay without detecting the autoplay threshold. This mode can cause issues in players that have ads enabled, but can be useful in scenarios where the platform is known to support autoplay (i.e. TV OSes)
  • "blocked": Player will not attempt to autoplay regardless of config.autoplay. The player will not attempt to detect the autoplay threshold.
  • "none": The player will not attempt to detect autoplay.
  • "unknown": Autoplay detection has not been attempted. This is the initial value of autoplayThreshold and can be used to determine whether or not to manually run AutoplayThreshold.init.

By default the player will run its autoplay detection routine the first time a player is created if the autoplayPolicy value is not none. In certain scenarios it may be beneficial to run the detection before creating the player. This can be achieved by explicitly calling akamai.amp.AutoplayThreshold:

akamai.amp.AutoplayThreshold.init()
    .then(function (threshold) {
        console.log("The browser's autoplay threshold is", threshold);
    });