tvOS AVPlayerViewController 视频信息

tvOS AVPlayerViewController Video Info

在 tvOS 的内置应用中,当您观看视频时,它会在您向下滑动时显示有关该视频的信息。我找不到任何关于开发人员如何做同样事情的信息。我确定它的设计是为了实现它所说的 "Swipe down for info" 有人想通了吗?我正在使用 AVPlayerViewController。谢谢。

要使 "Info" 部分显示在 AVPlayerViewController you create AVMutableMetadataItem 的 "Swipe down for info" 窗格中,使用 AVMetadataKeySpaceCommon 键空间和以下任意键:

AVMetadataCommonKeyTitle
AVMetadataCommonKeyDescription
AVMetadataiTunesMetadataKeyContentRating
AVMetadataQuickTimeMetadataKeyGenre

并将它们添加到 AVPlayerItemexternalMetadata 数组中。为了显示每个 AVMutableMetadataItem,您必须至少设置 identifierextendedLanguageTagvalue 属性。这是一个例子:

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)

这没有很好的记录。 This forum post 对解决这个问题至关重要。


Objective-C @JenelEjercitoMyers 的示例:

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;

除了 Jeff 的回答,这是我用来避免重复的功能:

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];

接受的答案是正确的。我们可以使用 AVMutableMetadataItem 来提供视频相关信息。

但是如果您需要在播放器菜单中有更多选项,最好创建一个 UIViewController 带有自定义信息和设置选项 [根据您的要求] 并将其设置为 AVPlayerViewController' s customInfoViewController.

This is available from tvOS 11.0

苹果官方文档:Apple Docs Link

仅供参考,我无法在模拟器 运行 tvOS 12.2 或 13(测试版)上显示它。最终让它起作用的是添加 metadataItem.locale = NSLocale.current。注释掉,它没有出现。