tvosavplayerviewcontroller

tvOS AVPlayerViewController Video Info


In the built-in apps for tvOS when you watch a video it shows information about that video when you swipe down. I can't find any information on how a developer can do this same thing. I'm sure it is designed to be possible as it says "Swipe down for info" Has anybody figured this out? I'm using AVPlayerViewController.


Solution

  • To get the "Info" section to show up in the "Swipe down for info" pane in AVPlayerViewController you create AVMutableMetadataItems with the AVMetadataKeySpaceCommon keyspace and any of the following keys:

    AVMetadataCommonKeyTitle
    AVMetadataCommonKeyDescription
    AVMetadataiTunesMetadataKeyContentRating
    AVMetadataQuickTimeMetadataKeyGenre
    

    and add them to the AVPlayerItem's externalMetadata array. In order for each AVMutableMetadataItem to show up you must at least set the identifier, extendedLanguageTag, and value properties. Here's an example:

    let mediaItem = AVPlayerItem(URL: mediaURL)
    
    let titleMetadataItem = AVMutableMetadataItem()
    titleMetadataItem.locale = NSLocale.currentLocale()
    titleMetadataItem.key = AVMetadataCommonKeyTitle
    titleMetadataItem.keySpace = AVMetadataKeySpaceCommon
    titleMetadataItem.value = "The Title"
                            
    let descriptionMetadataItem = AVMutableMetadataItem()
    descriptionMetadataItem.locale = NSLocale.currentLocale()
    descriptionMetadataItem.key = AVMetadataCommonKeyDescription
    descriptionMetadataItem.keySpace = AVMetadataKeySpaceCommon
    descriptionMetadataItem.value = "This is the description"
    
    mediaItem.externalMetadata.append(titleMetadataItem)
    mediaItem.externalMetadata.append(descriptionMetadataItem)
    

    This isn't well-documented. This forum post was critical to figuring this out.


    Objective-C example:

    AVPlayerItem *mediaItem = [[AVPlayerItem alloc] initWithURL:mediaURL];
    
    AVMutableMetadataItem *titleMetadataItem = [[AVMutableMetadataItem alloc] init];
    titleMetadataItem.locale = NSLocale.currentLocale;
    titleMetadataItem.key = AVMetadataCommonKeyTitle;  
    titleMetadataItem.keySpace = AVMetadataKeySpaceCommon;  
    titleMetadataItem.value = @"The Title";
    
    NSArray *externalMetadata = [[NSArray alloc] initWithObjects:titleMetadataItem, nil];
    
    mediaItem.externalMetadata = externalMetadata;