Is there a way to add custom information for AVPlayerViewController

I notice that in AVPlayerViewController there is a menu selection when I swipe down from the remote that shows Info/Chapter/Audio/etc options.


Is there a way to add more info in the Info section that's more custom like you do on the movie app from apple that has custom description of what the movie is about?

Answered by Technology Evangelist in 61400022

You can provide title, description, content rating, and an artwork image by setting the externalMetadata property of the AVPlayerItem with an array of AVMetadataItems. The data will then show up in the info panel.


You can also manage chapter markers in a similar way by setting the navigationMarkerGroups property.

Accepted Answer

You can provide title, description, content rating, and an artwork image by setting the externalMetadata property of the AVPlayerItem with an array of AVMetadataItems. The data will then show up in the info panel.


You can also manage chapter markers in a similar way by setting the navigationMarkerGroups property.

So if a full-screen AVMediaPlayerViewController is playing, for example, an.mp3 fille with appropriate externalMetadata array of AVMetadataItems, it will still appear as a black screen with a time-slider at the bottom unless the user opens the info panel? Can we open the info panel programatically?

It is not possible to open the info panel programatically. You might want to look at displaying some content using the contentOverlayView property of AVPlayerViewController. It's a non-interactive layer, but would avoid having a completely black screen.

I tried this and I get a list of chapters created programmatically, but I can't make it show thumbnails.


This is the code I'm using:


CMTime time1 = CMTimeMakeWithSeconds(0, 1);
CMTime duration1 = CMTimeMakeWithSeconds(90, 1);

AVMutableMetadataItem *title1 = [[AVMutableMetadataItem alloc] init];
title1.key = AVMetadataCommonKeyTitle;
title1.keySpace = AVMetadataKeySpaceCommon;
title1.value = @"Opening";
title1.locale = NSLocale.currentLocale;
title1.time = time1;
title1.duration = duration1;

AVMutableMetadataItem *artwork1 = [[AVMutableMetadataItem alloc] init];
artwork1.key = AVMetadataCommonKeyArtwork;
artwork1.keySpace = AVMetadataKeySpaceCommon;
artwork1.dataType = (__bridge NSString * _Nullable)(kCMMetadataBaseDataType_JPEG);
artwork1.value = UIImageJPEGRepresentation([UIImage imageNamed:@"cu-pic-1"], 1);
artwork1.locale = [NSLocale currentLocale];
artwork1.time = time1;
artwork1.duration = duration1;

AVTimedMetadataGroup *chapter1 = [[AVTimedMetadataGroup alloc]
                                  initWithItems:@[title1, artwork1]
                                  timeRange:CMTimeRangeMake(time1, duration1)];

AVNavigationMarkersGroup *chapterGroup = [[AVNavigationMarkersGroup alloc]
                                          initWithTitle:@"Chapters"
                                          timedNavigationMarkers:@[chapter1]];

playerItem.navigationMarkerGroups = @[chapterGroup];


And I just get a label with the text "Opening", but no UIImageView around. I can confirm the image I use is loading and the NSData generated is correct.


http://cl.ly/image/1G0t3C1D320S


(The image you see is the same image, but provided as Artwork of the player item through the extraMetadata method)


Why aren't the thumbnails showing up? Is this a bug in the tvOS AVPlayerViewController?


Thanks in advance! 😀

Did you try to remove the following line?

artwork1.dataType = (__bridge NSString * _Nullable)(kCMMetadataBaseDataType_JPEG);


I also had trouble to display an image (PNG instead of JPG). But without specifying the dataType property everthing's working fine.


Here is my code (Swift):


if let image = UIImage(named: "myPNG") {
            let artwork = AVMutableMetadataItem()
            artwork.key = AVMetadataCommonKeyArtwork
            artwork.keySpace = AVMetadataKeySpaceCommon
            artwork.value = UIImagePNGRepresentation(image)
            artwork.locale = NSLocale.currentLocale()
            video.externalMetadata.append(artwork)
        }

I think I tried that too and it didn't work, but I'm gonna try again. Thank you!

The properties that must be set for each metadata item are: identifier, extendedLanguageTag, and value

As long as you set those three, you should be good to go.

Definitely not working 😟


I saw identifier and extendedLanguageTag are automatically set from key, keySpace and locale, but I set them manually with no result. I only see the title of the chapters but not the images.


You can use this code to try it yourself (I'm using Xcode 7.1 beta 2 with the tvOS simulator): https://gist.github.com/sergiou87/b3c438d49ae7bb695ba7


You may need to replace the URLs if they don't work for you, but right now they work. Then, you only need to present the playerVC somewhere and see that the thumbnails are white, or they just don't appear, or it takes them from the video, but it never uses the image I provide (which is used as item artwork too and works perfectly).

Thanks sergiou87.


Can you please file a bug at bugreporter.apple.com and include the content of the gist in the report?


Thanks!

Working now in tvOS beta 3! Thank you very much 😀

Is there any reason why after doing so my info/metadata that I put in there is faded and hard to read?

I see the same thing, SerenadeX. It seems to be a bug with the Simulator. Works fine on the actual device.

I'm getting this compilation error: "Property 'externalMetadata' not found on object of type 'AVPlayerItem *'"


Using XCode 7.2 tvOS 9.1


This is the code:


NSMutableArray* items = [[NSMutableArray alloc] init];

AVMutableMetadataItem* item = [AVMutableMetadataItem metadataItem];
item.locale = NSLocale.currentLocale;
item.key = AVMetadataCommonKeyTitle;
item.keySpace = AVMetadataKeySpaceCommon;
item.value = @"Video 1";
[items addObject:item];

item = [AVMutableMetadataItem metadataItem];
item.locale = NSLocale.currentLocale;
item.key = AVMetadataQuickTimeMetadataKeyGenre;
item.keySpace = AVMetadataKeySpaceCommon;
item.value = @"Summary";
[items addObject:item];

playerItem.externalMetadata = items; //<-- Compilation error


Compilation error in the last line of code. Can't figure out what's wrong. Autocompletion in the XCode editor doesn't show externalMetadata, navigationMarkerGroups or interstitialTimeRanges. Any help would be appreciated.

That's very odd - I can't reproduce that particular issue on my end.


Are you including AVKit in that file? Note that these extensions on AVPlayerItem are part of the AVKit framework, not AVFoundation.


This sample code works fine for me. I'm using identifiers here rather than key and keySpace, which would be our recommendation:


- (void)setMetadata {
    NSMutableArray* items = [[NSMutableArray alloc] init];

    AVMutableMetadataItem* item = [AVMutableMetadataItem metadataItem];
    item.locale = NSLocale.currentLocale;
    item.identifier = AVMetadataCommonIdentifierTitle;
    item.value = @"Video 1";
    [items addObject:item];

    item = [AVMutableMetadataItem metadataItem];
    item.locale = NSLocale.currentLocale;
    item.identifier = AVMetadataIdentifierQuickTimeMetadataGenre;
    item.value = @"Summary";
    [items addObject:item];

    playerItem.externalMetadata = items;
}
Is there a way to add custom information for AVPlayerViewController
 
 
Q