Use timed metadata at the appropriate time

Use TimedMetadata when the current playback time matches the start time.

Use the saved dictionary from the previous topic to actually use these saved PTTimedMetadata objects during playback.

  1. Extract and update the current playback time from this notification and find all the PTTimedMetadata objects whose start time values match the current playback time. You can use these objects to perform various actions. For example:
    - (void) onMediaPlayerTimeChange:(NSNotification *)notification
    {
        CMTimeRange seekableRange = self.player.seekableRange;
        if (CMTIMERANGE_IS_VALID(seekableRange))
        {
            int currentTime = (int) CMTimeGetSeconds(self.player.currentTime);
            NSArray *allKeys = timedMetadataCollection ? [timedMetadataCollection allKeys] : [NSArray array];
            NSMutableArray *timedMetadatasToDelete = [[[NSMutableArray alloc] init] autorelease];
            int count = [allKeys count];
     
            for (int i=count - 1; i > -1; i--)
            {
               NSNumber *currTimedMetadataTime = allKeys[i];
               if ([currTimedMetadataTime integerValue] == currentTime)
               {
                /*
                    Use the timed metadata here and remove it from the collection.
                */
                 NSLog (@"IN PLAYBACK TIME %i TO EXECUTE TIMEDMETADATA %@ scheduled at time %f",currentTime,currTimedMetadata.name,CMTimeGetSeconds(currTimedMetadata.time));
                 
                PTTimedMetadata *currTimedMetadata = [timedMetadataCollection objectForKey:currTimedMetadataTime];
                [timedMetadatasToDelete addObject:currTimedMetadataTime];
               }
            }
            
            for (int i=0; i<[timedMetadatasToDelete count]; i++)
            {
                NSNumber *timedMetadataToDelete = timedMetadatasToDelete[i];
                [timedMetadataCollection removeObjectForKey:timedMetadataToDelete];
            }
        }
    }
  2. Periodically flush stale PTTimedMetadata instances from the list to prevent memory from growing continuously.