在tvOS的内置应用程序中,当您观看视频时,向下滑动会显示有关该视频的信息。我找不到任何关于开发人员如何做到这一点的信息。我相信它是可以实现的,因为它上面写着“向下滑动查看信息”。有人弄清楚了吗?我正在使用AVPlayerViewController。
AVPlayerViewController
创建带有AVMetadataKeySpaceCommon
键空间和以下任意键的AVMutableMetadataItem
。AVMetadataCommonKeyTitle
AVMetadataCommonKeyDescription
AVMetadataiTunesMetadataKeyContentRating
AVMetadataQuickTimeMetadataKeyGenre
AVPlayerItem
的externalMetadata
数组中。为了使每个AVMutableMetadataItem
显示出来,您至少必须设置identifier
、extendedLanguageTag
和value
属性。以下是一个示例: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)
Objective-C示例:
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;
AVMutableMetadataItem
提供有关视频的信息。UIViewController
[基于您的要求] 并将其设置为 AVPlayerViewController
的 customInfoViewController
。
这在 tvOS 11.0 中可用
有关此内容的官方 Apple 文档:Apple Docs Link
private func setupMetadata(data: String, key: (NSCopying & NSObjectProtocol))->AVMutableMetadataItem{
let metadataItem = AVMutableMetadataItem()
metadataItem.locale = NSLocale.current
metadataItem.key = key
metadataItem.keySpace = AVMetadataKeySpaceCommon
metadataItem.value = data as (NSCopying & NSObjectProtocol)?
return metadataItem
}
并且正在使用:
//in AVPlayerViewControler
//Suppose you have an already initialized avPlayerItem
avPlayerItem.externalMetadata.append(self.setupMetadata(data: "title of video", key: AVMetadataCommonKeyTitle as (NSCopying & NSObjectProtocol)))
avPlayerItem.externalMetadata.append(self.setupMetadata(data: "RugDealer", key: AVMetadataCommonKeyAuthor as (NSCopying & NSObjectProtocol)))
avPlayerItem.externalMetadata.append(self.setupMetadata(data: "Description of the video", key: AVMetadataCommonKeyDescription as (NSCopying & NSObjectProtocol)))
//Sets the content rating on the top shelf
AVMutableMetadataItem *ratingInfo = [[AVMutableMetadataItem alloc] init];
ratingInfo.key = AVMetadataiTunesMetadataKeyContentRating;
ratingInfo.keySpace = AVMetadataKeySpaceiTunes;
ratingInfo.locale = [NSLocale currentLocale];
ratingInfo.value = @"PG-13"; //Rating of the video
ratingInfo.extendedLanguageTag = @"und";
[externalMetadata addObject:ratingInfo];
//Sets the thumbnail on the shelf
AVMutableMetadataItem *artwork1 = [[AVMutableMetadataItem alloc] init];
artwork1.key = AVMetadataCommonKeyArtwork;
artwork1.keySpace = AVMetadataKeySpaceCommon;
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:artworkAddress]];
artwork1.value = imageData;
artwork1.locale = [NSLocale currentLocale];
[externalMetadata addObject:artwork1];
//Sets the Genre on the shelf
AVMutableMetadataItem *genresInfo = [[AVMutableMetadataItem alloc] init];
genresInfo.key = AVMetadataQuickTimeMetadataKeyGenre;
genresInfo.keySpace = AVMetadataKeySpaceQuickTimeMetadata;
genresInfo.locale = [NSLocale currentLocale];
genresInfo.value = @"Drama, Medical";
[externalMetadata addObject:genresInfo];
这是@kemicofaghost的精彩回答已经更新了
func meta(_ data: String, key: AVMetadataKey) -> AVMutableMetadataItem {
let mdi = AVMutableMetadataItem()
mdi.locale = NSLocale.current
mdi.key = key as (NSCopying & NSObjectProtocol)
mdi.keySpace = AVMetadataKeySpace.common
mdi.value = data as (NSCopying & NSObjectProtocol)?
return mdi
}
然后
someItem.externalMetadata.append(meta("Episode 13", key:.commonKeyTitle) )
https://developer.apple.com/documentation/avkit/customizing_the_tvos_playback_experience
顺便说一下,我无法在运行tvOS 12.2或13(beta)的模拟器上显示它。最终让它工作的是添加metadataItem.locale = NSLocale.current
。如果将其注释掉,则不会出现。