tvOS AVPlayerViewController视频信息

时间:2015-10-22 15:36:01

标签: tvos avplayerviewcontroller

在观看视频时内置的tvOS应用中,当您向下滑动时,它会显示有关该视频的信息。我无法找到有关开发人员如何做同样事情的任何信息。我确信它的设计是可行的,因为它说"向下滑动以获取信息"有人想出来了吗?我正在使用AVPlayerViewController。感谢。

5 个答案:

答案 0 :(得分:17)

要让“信息”部分显示在AVPlayerViewController的“向下滑动信息”窗格中,您可以使用AVMetadataKeySpaceCommon键空间和以下任意键创建AVMutableMetadataItem

AVMetadataCommonKeyTitle
AVMetadataCommonKeyDescription
AVMetadataiTunesMetadataKeyContentRating
AVMetadataQuickTimeMetadataKeyGenre

并将它们添加到AVPlayerItem的{​​{3}}数组中。为了显示每个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)

这没有详细记录。 externalMetadata对于解决这个问题至关重要。

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

答案 1 :(得分:1)

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

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)))

答案 2 :(得分:1)

除了上述答案,我还想在顶层架子上添加艺术品,流派和内容评级。这与上面提到的略有不同。可以按如下方式将它们添加到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];

答案 3 :(得分:1)

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

但是,如果您需要在播放器菜单中有更多选项,则最好根据自己的要求创建一个具有自定义信息和设置选项的UIViewController并将其设置为AVPlayerViewController的{ {1}}。

  

可从tvOS 11.0中获得

enter image description here

有关此内容的官方苹果文档:Apple Docs Link

答案 4 :(得分:0)

仅供参考,我无法在运行tvOS 12.2或13(测试版)的模拟器上显示此内容。最终起作用的是添加了metadataItem.locale = NSLocale.current。评论出来,它不会出现。