Set up notifications

Last update: 2023-10-02

The player can listen for a range of events that indicate the state of the player.

Assuming that PTMediaPlayer is a property of the client player, self.player in the following example represents the PTMediaPlayer instance. The following example implements the addObservers method that are shown in the PTMediaPlayer set up instructions and includes most of the notifications:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMediaPlayerStatusChange:)
      name:PTMediaPlayerStatusNotification object:self.player];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMediaPlayerNotification:)
      name:PTMediaPlayerNewNotificationEntryAddedNotification object:self.player];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMediaPlayerTimeChange:)
      name:PTMediaPlayerTimeChangeNotification object:self.player];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMediaPlayerItemPlayStarted:)
      name:PTMediaPlayerPlayStartedNotification object:self.player];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMediaPlayerItemPlayCompleted:)
      name:PTMediaPlayerPlayCompletedNotification object:self.player];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMediaPlayerItemTimelineChanged:)
      name:PTMediaPlayerTimelineChangedNotification object:self.player];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMediaPlayerItemMediaSelectionOptionsAvailable:)
      name:PTMediaPlayerMediaSelectionOptionsAvailableNotification object:self.player];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMediaPlayerAdBreakStarted:)
      name:PTMediaPlayerAdBreakStartedNotification object:self.player];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMediaPlayerAdBreakCompleted:)
      name:PTMediaPlayerAdBreakCompletedNotification object:self.player];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMediaPlayerAdPlayStarted:)
      name:PTMediaPlayerAdStartedNotification object:self.player];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMediaPlayerAdPlayProgress:)
      name:PTMediaPlayerAdProgressNotification object:self.player];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMediaPlayerAdPlayCompleted:)
      name:PTMediaPlayerAdCompletedNotification object:self.player];

iOS notifications

ThePTMediaPlayerNotifications class lists the notifications that the TVSDK dispatches to your player.

Notification Meaning
PTMediaPlayerAdBreakCompletedNotification An ad break ended.
PTMediaPlayerAdBreakStartedNotification An ad break started.
PTMediaPlayerAdClickNotification A user clicked a banner ad.
PTMediaPlayerAdCompletedNotification An individual ad ended.
PTMediaPlayerAdProgressNotification An ad progressed; dispatched constantly while an ad plays.
PTMediaPlayerAdStartedNotification An individual ad started.
PTBackgroundManifestErrorNotification Downloading the background manifest failed.
PTMediaPlayerBufferingCompletedNotification Buffering has completed.
PTMediaPlayerBufferingStartedNotification The media player enters a buffering state.
PTAudioTrackChangeCompleted A change on the audio track of the currently playing media has completed.
PTAudioTrackChangeStarted A change on the audio track of the currently playing media is initiated.
PTMediaPlayerItemChangedNotification A different PTMediaPlayerItem of the PTMediaPlayer has been set.
PTMediaPlayerItemDRMMetadataChanged DRM metadata changed.
PTMediaPlayerMediaSelectionOptionsAvailableNotification There are new subtitles and alternate audio tracks ( PTMediaSelectionOption ).
PTMediaPlayerNewNotificationEntryAddedNotification A new PTNotification has been added to the PTNotificationHistoryItem of the current PTMediaPlayerItem , that is, when a notification event is added to the notification history.
PTMediaPlayerPlayCompletedNotification Media playback ended.
PTMediaPlayerSeekCompletedNotification Seeking has completed.
PTMediaPlayerSeekErrorNotification The current seek operation has failed.
PTMediaPlayerSeekStartedNotification Seeking is starting.
PTMediaPlayerPlayStartedNotification Playback started.
PTMediaPlayerStatusNotification The player status changed. Possible status values are:
  • PTMediaPlayerStatusCreated

  • PTMediaPlayerStatusInitializing

  • PTMediaPlayerStatusInitialized

  • PTMediaPlayerStatusReady

  • PTMediaPlayerStatusPlaying

  • PTMediaPlayerStatusPaused

  • PTMediaPlayerStatusStopped

  • PTMediaPlayerStatusCompleted

  • PTMediaPlayerStatusError

PTMediaPlayerTimeChangeNotification The playback current time changed.
PTMediaPlayerTimelineChangedNotification The current player timeline changed.
PTTimedMetadataChangedNotification The TVSDK encountered the first occurrence of a subscribed tag.
PTTimedMetadataChangedInBackgroundNotification

A subscribed tag is identified on the background manifest and a new PTTimedMetadata instance is prepared from it.

Sample handlers for notifications

The following code snippets illustrate some of the ways you can use notifications.

Fetch the PTAdBreak instance using PTMediaPlayerAdBreakKey:

 - (void) onMediaPlayerAdBreakStarted:(NSNotification *) notification {
   // Fetch the PTAdBreak instance using PTMediaPlayerAdBreakKey
   PTAdBreak *adBreak = [notification.userInfo objectForKey:PTMediaPlayerAdBreakKey];
   ...
   ...
}

Set subtitlesOptions and audioOptions:

 - (void) onMediaPlayerItemMediaSelectionOptionsAvailable:(NSNotification \*) notification {
   //SubtitlesOptions and audioOptions are set and accessible now.
   NSArray* subtitlesOptions = self.player.currentItem.subtitlesOptions;
   NSArray* audioOp tions = self.player.currentItem.audioOptions;
   ...
   ...
}

Fetch the PTAd instance using PTMediaPlayerAdKey:

 - (void) onMediaPlayerAdPlayStarted:(NSNotification \*)  notification {
   // Fetch the PTAdinstance using PTMediaPlayerAdKey
   PTAd *ad = [notification.userInfo objectForKey:PTMediaPlayerAdKey];
   ...
   ...
}

On this page