Listen for events (iOS)

JWP's event listeners enable you to listen for player events to occur. Once the event occurs, you can define an appropriate response.

You can listen for events either through the JWPlayerViewController or the JWPlayerView.



JWPlayerViewController

When using the JWPlayerViewController to display your content, this class subscribes to all events and is assigned as each delegate.

Use the following steps to listen for events:

  1. Override the delegate methods and read the needed data.
  2. Call super() to inform the JWPlayerViewController of the event.

// Subclass of JWPlayerViewController
class PlayerViewController: JWPlayerViewController {
    
    override func jwplayer(_ player: JWPlayer, beganChapter chapter: JWCue) {
        // Always be sure to inform the superclass of the event so
        // the user interface updates.
        super.jwplayer(player, beganChapter: chapter)
        
        // Handle the event here.
    }
}


JWPlayerView

🚧

The JWPlayerViewController does not support this approach.

Attempting to follow these steps using the JWPlayerViewController will produce the following results:

  • The assignment will be unsuccessful.
  • A warning will be printed to the console announcing that JWPlayerViewController cannot be overridden as a delegate for these events.

If are using JWPlayerView, use the following steps:

  1. Create your own class which conforms to a specific delegate.
  2. Assign it as the delegate through the JWPlayer object.

// A custom chapters delegate
class ChapterDelegate: JWChaptersDelegate {
    
    func jwplayer(_ player: JWPlayer, beganChapter chapter: JWCue) {
        // Handle the event here.
    }
    
}

// Assume this delegate is declared in a class you designed.
let chaptersDelegate = ChaptersDelegate()

// Your custom method for setting up your chapter observer.
func yourSetupMethod(view: JWPlayerView) {
    let player = view.player
    let player.chaptersDelegate = chaptersDelegate
}