如何更改用于YouTube播放的MPNowPlayingInfoCenter信息?

3
我正在开发一个使用JavaScript控制的html5 webview应用程序,播放来自播放列表的YouTube视频。现在我想使用以下代码更改iOS InfoCenter中显示的标题、艺术家和其他信息:
Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter");
if (playingInfoCenter) {
    NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];
    [songInfo setObject:@"Audio Title" forKey:MPMediaItemPropertyTitle];
    [songInfo setObject:@"Audio Author" forKey:MPMediaItemPropertyArtist];
    [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];
}

我的问题是,我在 iOS 上播放 YouTube 视频时设置了标题和艺术家信息,但只能短暂地生效,之后 iOS 又会将这些信息替换为原始的 YouTube 标题和视频 URL。有没有一种方法可以永久地用自己的信息覆盖原始信息呢?

谢谢!

1个回答

5

有点不规范,但您需要为AVPlayerItemNewAccessLogEntry设置一个通知监听器。

像这样:

-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(playerItemWasSet:)
                                             name:@"AVPlayerItemNewAccessLogEntry"
                                           object:nil];
}

然后

-(void)playerItemWasSet:(NSNotification *)note {
    Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter");
    if (playingInfoCenter) {
        NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];
        [songInfo setObject:@"Audio Title" forKey:MPMediaItemPropertyTitle];
        [songInfo setObject:@"Audio Author" forKey:MPMediaItemPropertyArtist];
        [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];
    }
}

我不知道苹果会不会对此有问题,因为我没有提交过带有这个功能的应用程序。只是出于好玩试了一下。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接