Ich entwickle eine Musik-Anwendung, die Musik im Hintergrund abspielen sollte.xcode - MPNowPlayingInfoCenter Info wird nicht auf iOS 8 angezeigt
Ich verwende die MPMoviePlayerController
, um die Musik zu spielen.
NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
resourcePath = [resourcePath stringByAppendingString:@"/music.m4a"];
NSError* err;
self.player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:resourcePath]];
if (err) {
NSLog(@"ERROR: %@", err.localizedDescription);
}
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
[session setActive:YES error:nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self.player setShouldAutoplay:NO];
[self.player setControlStyle: MPMovieControlStyleEmbedded];
self.player.view.hidden = YES;
[self.player prepareToPlay];
Wenn ich ausführen [self.player play];
die Musik beginnt: Mein Code der MPMoviePlayerController
einzuleiten. Aber ich möchte auch den Namen des Songs, den Namen des Albums und das Cover des Albums im LockScreen und im ControlCenter anzeigen. Ich verwende den folgenden Code:
Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter");
if (playingInfoCenter) {
NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];
MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage: [UIImage imageNamed:@"artwork.png"]];
[songInfo setObject:@"SongName" forKey:MPMediaItemPropertyTitle];
[songInfo setObject:@"ArtistName" forKey:MPMediaItemPropertyArtist];
[songInfo setObject:@"AlbumTitle" forKey:MPMediaItemPropertyAlbumTitle];
[songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];
}
Aber nichts wird auf dem LockScreen angezeigt. Es wird auch nicht im ControlCenter angezeigt.
Wie kann ich mein Problem lösen? Ich habe im Internet nichts gefunden.
Vielen Dank im Voraus, Fabian.
Wird Ihr Code tatsächlich ausgeführt? – matt
Ja, [[MPNowPlayingInfoCenter defaultCenter] .nowPlayingInfo objectForKey: MPMediaItemPropertyTitle] gibt den richtigen SongName zurück. – FTFT1234
Aha! Das sind wirklich gute Neuigkeiten. Es läuft also nicht nur der Code, sondern auch die 'nowPlayingInfo'. Also lassen Sie mich Sie fragen - sind Sie Ersthelfer, damit Sie die Fernsteuerungs-Ereignisse erfolgreich vom Sperrbildschirm und vom Kontrollzentrum empfangen? – matt