2

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)

Benr783
  • 2,860
  • 4
  • 20
  • 28
  • Apple Music is a separate app that appears to use the exclusive playback category, so I think it is incompatible with anything that requires an exclusive audio session, like `MPRemoteCommandCenter` commands. – Rhythmic Fistman Dec 08 '17 at 06:41

3 Answers3

1

Trying with Apple's sample code, I observe the same behaviour as long as applicationQueuePlayer is used. Using systemMusicPlayer makes the code working in the way you expect, updating the Control Center.

Bahri Okuroglu
  • 178
  • 1
  • 7
  • Sure, but my whole app is centered around using applicationQueuePlayer, and it requires that. So changing it is simply not the answer. – Benr783 Jul 02 '17 at 22:49
  • Hi @Benr783 I am having a similar issue and am using applicationQueuePlayer Did you ever figure this out? – RubberDucky4444 Jan 29 '18 at 05:41
  • For whomever reaches this place, this actually worked for me; though I'm still trying to get the feedback on the Control Center actions – Andres C Oct 29 '18 at 14:15
1

This is a bug IMO.

rdar://33923587

EDIT for curious:

I was able to override Control Center/Lock Screen track description by creating my own auddio session without mix options (so it paused the audio from Apple Music/Music Library) but this isn't a solution.

Md. Rezwanul Haque
  • 2,882
  • 7
  • 28
  • 45
Adam Różyński
  • 451
  • 3
  • 10
1

The MPMusicPlayerApplicationController communicates with MPRemoteCommandCenter on your behalf. All you need to do in order to see controls is set up a queue that contains more than one song.

BTW, instantiating MPMusicPlayerApplicationController in didSelectRowAtIndexPath: is not good practice because you instantiate a new MPMusicPlayerApplicationController everytime the user taps on a cell. You only want one instance that should be instantiated in viewDidLoad:, stored to an iVar and then configured as needed.

Mojo66
  • 1,109
  • 12
  • 21