I am developing a music application that should play music in the background.
I am using MPMoviePlayerController to play music. My code to run MPMoviePlayerController :
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];
When I execute [self.player play]; , the music begins. But I also want to show the song title, album name and album art in LockScreen and ControlCenter. I am using the following 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]; }
But nothing is displayed in LockScreen. It also does not appear in ControlCenter.
How can I solve my problem? I did not find anything on the Internet.
Thanks Fabian.
source share