Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change MPNowPlayingInfoCenter information for youtube playback?

Tags:

html

youtube

ios

I am developing a html5 webview app playing youtube videos from a playlist (controlled with JavaScript). Now i want to change the displayed title, artist and other information in iOS InfoCenter using this code:

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

It works for a second but then iOS replaces my set information (title and artist) again with the original youtube title and the video url. Is there a way to permanently overwrite this information with my own?

Thank You!

like image 695
Jonson Avatar asked Dec 05 '25 14:12

Jonson


1 Answers

A bit hacky, but you need set a notification listener for AVPlayerItemNewAccessLogEntry

Like so:

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

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

and then

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

I don't know whether Apple will have a problem with it or not, I've not submitted an app with this in. Just tried it out for fun.

like image 91
Eddie Amphlett Avatar answered Dec 07 '25 03:12

Eddie Amphlett



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!