I try to add some basic information in videos under Info Box, but I don't know how to add or update it?
Anyone knows how to do it?
You can set metadata properties directly on the MediaItem
object that you instantiate.
var mediaItem = new MediaItem("video", videoURL);
mediaItem.title = "My Title";
mediaItem.subtitle = "My Subtitle";
mediaItem.artworkImageURL = someURL;
mediaItem.description = "My description";
For more information see here and here.
You should add externalMetadata
to your AVPlayerItem
. To do this you can add these two helper functions that Apple has provided in their WWDC session:
func metadataItem(identifier : String, value : protocol<NSCopying,
NSObjectProtocol>?) -> AVMetadataItem? {
if let actualValue = value {
let item = AVMutableMetadataItem()
item.value = actualValue
item.identifier = identifier
item.extendedLanguageTag = "und" // undefined (wildcard) language
return item.copy() as? AVMetadataItem
}
return nil
}
func metadataArtworkItem(image: UIImage) -> AVMetadataItem? {
let item = AVMutableMetadataItem()
// Choose PNG or JPEG
item.value = UIImagePNGRepresentation(image)
item.dataType = kCMMetadataBaseDataType_PNG as String
item.identifier = AVMetadataCommonIdentifierArtwork
item.extendedLanguageTag = "und"
return item.copy() as? AVMetadataItem
}
The first one will take an identifier, the type of metadata you would like to add, and the value for that type and return an optional AVMetadataItem
The other takes a UIImage
and returns the same thing.
Now you can say something like the following when you update your AVPlayerItem
:
let videoURL = NSURL(string: "http://devstreaming.apple.com/videos/wwdc/2016/506ms2tv71tcduwp3dm/506/hls_vod_mvp.m3u8")
self.player = AVPlayer(URL: videoURL!)
let playerItem = self.player?.currentItem
var allItems: [AVMetadataItem] = [AVMetadataItem]()
allItems.append(self.metadataItem(AVMetadataCommonIdentifierTitle, value: "AVKit on tvOS")!)
allItems.append(self.metadataItem(AVMetadataCommonIdentifierDescription, value: "2016 WWDC session AVKit on tvOS where adding metadata to AVPlayerItem is covered")!)
if let image = UIImage(named: "Poster"), let artworkItem = metadataArtworkItem(image)
{
allItems.append(artworkItem)
}
playerItem?.externalMetadata = allItems
Which gives you something like:
You can find more information here.
- (AVMetadataItem *)metadataItemWithIdentifier:(NSString *)identifier value:(id<NSObject, NSCopying>) value
{
AVMutableMetadataItem *item = [[AVMutableMetadataItem alloc]init];
item.value = value;
item.identifier = identifier;
item.extendedLanguageTag = @"und";
return [item copy];
}
- (AVMetadataItem *)metadataArtworkItemWithImage:(UIImage *)image
{
AVMutableMetadataItem *item = [[AVMutableMetadataItem alloc]init];
item.value = UIImagePNGRepresentation(image);
item.dataType = (__bridge NSString * _Nullable)(kCMMetadataBaseDataType_PNG);
item.identifier = AVMetadataCommonIdentifierArtwork;
item.extendedLanguageTag = @"und";
return item.copy;
}
// Add this code where you update the AVPlayerItem
NSURL *videoURL = [NSURL URLWithString:self.detailItem.urlString];
playerViewController.player = [AVPlayer playerWithURL:videoURL];
self.player = playerViewController.player;
AVPlayerItem *playerItem = self.player.currentItem;
NSMutableArray <AVMetadataItem *> *allItems = [NSMutableArray new];
[allItems addObject:[self metadataItemWithIdentifier:AVMetadataCommonIdentifierTitle value:@"AVKit on tvOS"]];
[allItems addObject:[self metadataItemWithIdentifier:AVMetadataCommonIdentifierDescription value:@"2016 WWDC session AVKit on tvOS where adding metadata to AVPlayerItem is covered"]];
[allItems addObject:[self metadataArtworkItemWithImage:[UIImage imageNamed:@"Poster"]]];
playerItem.externalMetadata = allItems;