在 tvOS 的内置应用程序中,当您观看视频时,它会在您向下滑动时显示有关该视频的信息。我找不到任何关于开发人员如何做同样事情的信息。我确信它的设计是可能的,因为它说“向下滑动以获取信息”有人想通了吗?我正在使用 AVPlayerViewController。谢谢。
5 回答
要使“信息”部分显示在“向下滑动以获取信息”窗格中,请使用键空间和以下任何键AVPlayerViewController
创建AVMutableMetadataItem
s :AVMetadataKeySpaceCommon
AVMetadataCommonKeyTitle
AVMetadataCommonKeyDescription
AVMetadataiTunesMetadataKeyContentRating
AVMetadataQuickTimeMetadataKeyGenre
并将它们添加到AVPlayerItem
'sexternalMetadata
数组中。为了让每个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)
这没有很好的记录。这个论坛帖子对于弄清楚这一点至关重要。
@JenelEjercitoMyers 的 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
's customInfoViewController
。
这可从 tvOS 11.0 获得
官方苹果文档: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)))
除了上面的答案,我还想在顶层架子上添加艺术品、流派和内容评级。这与所提到的略有不同。它们可以按如下方式添加到 externalMetadata 数组中。
//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];
仅供参考,我无法让它出现在运行 tvOS 12.2 或 13(测试版)的模拟器上。最终让它工作的是添加metadataItem.locale = NSLocale.current
. 注释掉,它不会出现。