Reputation: 533
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?
Upvotes: 1
Views: 723
Reputation: 8483
If you need to download image asynchronously -like in my case-, here is what I'm using for it:
Objective-C
#import <AFNetworking/UIKit+AFNetworking.h>
@property (strong, nonatomic) NSMutableArray<AVMetadataItem*>* metaData;
// Build title item
AVMutableMetadataItem* titleMetadataItem = [[AVMutableMetadataItem alloc] init];
titleMetadataItem.locale = NSLocale.currentLocale;
titleMetadataItem.key = AVMetadataCommonKeyTitle;
titleMetadataItem.keySpace = AVMetadataKeySpaceCommon;
titleMetadataItem.value = self.currentVideoData.Title;
[_metaData addObject:titleMetadataItem];
// Build description item
AVMutableMetadataItem* descriptionMetadataItem = [[AVMutableMetadataItem alloc] init];
descriptionMetadataItem.locale = NSLocale.currentLocale;
descriptionMetadataItem.key = AVMetadataCommonKeyDescription;
descriptionMetadataItem.keySpace = AVMetadataKeySpaceCommon;
descriptionMetadataItem.value = description;
[_metaData addObject:descriptionMetadataItem];
self.player.currentItem.externalMetadata = _metaData;
NSString* artwork = @"https://Your_image_path"
__weak typeof(self) weakSelf = self;
if (![artwork isEqualToString:@""]) {
// Build artwork item
UIImageView* imgArtwork = [UIImageView new];
imgArtwork.contentMode = UIViewContentModeScaleAspectFit;
[imgArtwork
setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:artwork]]
placeholderImage:nil
success:^(NSURLRequest* _Nonnull request,
NSHTTPURLResponse* _Nullable response,
UIImage* _Nonnull image) {
[weakSelf.metaData
addObject:[weakSelf metadataArtworkItemWithImage:image]];
weakSelf.player.currentItem.externalMetadata = _metaData; // re-setting meta data here...
}
failure:nil];
}
//...
// same method of @beyowulf
- (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;
}
it works like a charm.
Upvotes: 0
Reputation: 15331
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;
Upvotes: 5