setPlaylistItemCallback(callback)

Registers a playlist item callback that can modify or block the progression of a playlist

The playlist item callback accepts two arguments, the playlist item, and its index in the playlist. There are four outcomes the callback can produce:

  1. Return from the callback synchronously to allow the item to load and play without changes
  2. Return a promise that resolves with a new or modified playlist item
  3. Return a promise that resolves without changes
  4. Return a promise that rejects to skip loading and playback of that item

jwplayer().setPlaylistItemCallback(function(item, index) {
  console.log(item); // The playlist item that will be loaded
  console.log(index); // The playlist item’s index in the playlist

  // Return from the callback synchronously to allow the item to load and play without changes
  if (item.verified) {
    return;
  }

  // Return a promise to block playlist progression until resolved
  return new Promise(function(resolve, reject) {
    return doSomethingAsync(item).then(function(result) {
      if (result.ok) {
        if (result.modifiedItem) {
          // Resolve the promise to resume loading and playback with a new item
          resolve(result.modifiedItem);
          
        } else {
          // Resolve the promise to resume loading and playback without changes
          resolve();
        }
      } else {
        // Reject the promise to skip playback of the playlist item
        reject();
      }
    });
  }); 
});
jwplayer().setPlaylistItemCallback(null);
ArgumentDescription
callbackA null argument or a function that is passed with the arguments index and item

When the callback is a function, it can be invoked asynchronously and pass item (a playlist item) and index (the index of the item in the playlist). When the callback returns a Promise, playlist progression is blocked until the Promise is resolved. If the Promise resolves with a valid playlist object, that object will replace the item in the playlist. Since Promises only resolve once, the function is not repeated when the same item is played more than once.

There can only be one active callback. By calling this method again, any existing callback and playlist item Promises are removed.

When the callback is null, the callback and all playlist item promises are removed. See also: removePlaylistItemCallback()
© 2007-2024 Longtail Ad Solutions, Inc.