I am using Apple's new Apple Music API to make an app. I have a screen with a list of songs, and the user can tap one to play it. My code partially works. The song that is tapped will play, but it does not show up on the control center. The control center stays empty, as if nothing were playing.
- (void)viewDidLoad {
[super viewDidLoad];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
[[[MPRemoteCommandCenter sharedCommandCenter] playCommand] addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
NSLog(@"play");
return MPRemoteCommandHandlerStatusSuccess;
}];
[[[MPRemoteCommandCenter sharedCommandCenter] pauseCommand] addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
NSLog(@"pause");
return MPRemoteCommandHandlerStatusSuccess;
}];
[[[MPRemoteCommandCenter sharedCommandCenter] stopCommand] addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
return MPRemoteCommandHandlerStatusSuccess;
}];
[[[MPRemoteCommandCenter sharedCommandCenter] togglePlayPauseCommand] addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
return MPRemoteCommandHandlerStatusSuccess;
}];
[[MPRemoteCommandCenter sharedCommandCenter] playCommand].enabled = YES;
[[MPRemoteCommandCenter sharedCommandCenter] pauseCommand].enabled = YES;
[[MPRemoteCommandCenter sharedCommandCenter] stopCommand].enabled = YES;
[[MPRemoteCommandCenter sharedCommandCenter] togglePlayPauseCommand].enabled = YES;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioSession setActive:YES error:nil];
}
```
When a cell is tapped:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
MPMusicPlayerApplicationController *controller = [MPMusicPlayerController applicationQueuePlayer];
[controller performQueueTransaction:^(MPMusicPlayerControllerMutableQueue * _Nonnull queue) {
MPMusicPlayerStoreQueueDescriptor *queueDescripter = [[MPMusicPlayerStoreQueueDescriptor alloc] initWithStoreIDs:@[_album.songs[indexPath.row].identifier]];
[queue insertQueueDescriptor:queueDescripter afterItem:nil];
} completionHandler:^(MPMusicPlayerControllerQueue * _Nonnull queue, NSError * _Nullable error) {
[controller setNowPlayingItem:queue.items[0]];
[controller prepareToPlay];
[controller play];
MPMediaItem *item = [controller nowPlayingItem];
NSDictionary *info = @{MPMediaItemPropertyAlbumTitle:item.albumTitle, MPMediaItemPropertyArtist:album.artistName, MPMediaItemPropertyTitle:item.title, MPMediaItemPropertyArtwork:item.artwork, MPMediaItemPropertyPlaybackDuration:@(item.playbackDuration), MPNowPlayingInfoPropertyElapsedPlaybackTime:@0};
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:info];
}];
}
In addition, this message is logged to the console. I am not sure if this relates to this issue.
[RemoteControl] Error setting active player: (null)