如何更改 youtube 播放的 MPNowPlayingInfoCenter 信息?

Jon*_*son 3 html youtube ios

我正在开发一个 html5 webview 应用程序,从播放列表(用 JavaScript 控制)播放 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];
}
Run Code Online (Sandbox Code Playgroud)

它工作了一秒钟,但随后 iOS 再次用原始 youtube 标题和视频 url 替换了我的设置信息(标题和艺术家)。有没有办法用我自己的信息永久覆盖这些信息?

谢谢你!

小智 5

有点hacky,但您需要为 AVPlayerItemNewAccessLogEntry 设置一个通知侦听器

像这样:

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

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(playerItemWasSet:)
                                             name:@"AVPlayerItemNewAccessLogEntry"
                                           object:nil];
}
Run Code Online (Sandbox Code Playgroud)

进而

-(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];
    }
}
Run Code Online (Sandbox Code Playgroud)

我不知道 Apple 是否会对此有问题,我还没有提交带有此功能的应用程序。只是为了好玩而尝试了一下。