Creating an instance of AMP

To create an instance of amp, use the function akamai.amp.AMP.create. You must pass the function an HTML DOM element or a valid CSS selector to a DOM element:

akamai.amp.AMP.create("#amp");

Optionally you can pass a configuration object to as the second argument to modify the player's default settings:

var config = {
  media: {
    src: "http://projects.mediadev.edgesuite.net/customers/akamai/video/VfE.mp4",
    type: "video/mp4",
    autoplay: true
  }
};
akamai.amp.AMP.create("amp", config);

Sometimes it is necessary to run additional code to set up the player. To ensure this code is run after the player is fully initialized, wait for the create function's promise to resolve:

var config = {
  media: {
    src: "http://projects.mediadev.edgesuite.net/customers/akamai/video/VfE.mp4",
    type: "video/mp4",
    autoplay: true
  }
};
akamai.amp.AMP.create("amp", config)
  .then(function (player) {
    console.log(player);
    player.addEventListener("mediachange", function (event) { console.log(event); });
  });

Or, using the new async/await functionality:

async function onload() {
  var config = {
    media: {
      src: "http://projects.mediadev.edgesuite.net/customers/akamai/video/VfE.mp4",
      type: "video/mp4",
      autoplay: true
    }
  };
  var player = await akamai.amp.AMP.create("amp", config);
  console.log(player);
  player.addEventListener("mediachange", function (event) { console.log(event); });
}

Alternatively, an event listener can be passed as the third argument to the create function. This listener will be executed when the ready event is fired:

var config = {
  media: {
    src: "http://projects.mediadev.edgesuite.net/customers/akamai/video/VfE.mp4",
    type: "video/mp4",
    autoplay: true
  }
};
akamai.amp.AMP.create("amp", config, function (event) {
  var player = event.player;
  console.log(player);
  player.addEventListener("mediachange", function (event) { console.log(event); });
});