Developer Guide Javascript API Reference Developer Tools Release Notes

Async API: Cancel a Playlist Item

A demo showing how to use the setPlaylistItemCallback to run asynchronous code to determine whether to cancel a subsequent playlist item

github

This is a demo for using the setPlaylistItemCallback API to cancel playback of the next item. The setPlaylistItemCallback API accepts a callback which returns a promise.

This callback is invoked between each playlist item, and blocks playback of the next playlist item until the promise is resolved, or rejected.

Demonstration

In this example, the player is configured with three playlist items, the second of which will be skipped (simulated), representing an unauthorized user. After failing the authorization check, the third playlist item will be loaded.

function isUserAuthorized(item, index) {
	// Handle any asynchronous custom logic
  return new Promise((resolve, reject) => {
	// Simulated to skip second playlist item
	const isAuthorized = index !== 1;
	resolve(isAuthorized);
  });
}

jwplayer('player').setup({
  playlist: [
		{
			file: "//playertest.longtailvideo.com/adaptive/bunny/manifest-no-captions.m3u8",
		},
		{
			file: "//playertest.longtailvideo.com/adaptive/bunny/manifest-no-captions.m3u8",
		},
		{
			file: "//playertest.longtailvideo.com/adaptive/bunny/manifest-no-captions.m3u8",
		},
  ],
  width: `50%`,
  autostart: true,
  mute: true,
});

jwplayer('player').setPlaylistItemCallback((item, index) => {
	// Resolve accepts a playlist item, this can be modified
	// The playlist item that is scheduled to load is
	// passed in as 'item'
	// Reject can be used to cancel a scheduled item from being loaded
	return new Promise((resolve, reject) => {
	  // Handle external bidding and, in this example, return a boolean indicating
	  // whether user is authorized
	  return isUserAuthorized(item, index).then(isAuthorized => {
	    if (!isAuthorized) {
		    // Reject cancels playback and skips to next item.
		    reject();
		  }
  		// If authorized, load and play item.
  		resolve();
	  });
	});
});