Tak, istnieje API dla tego
Patrząc instrukcji znalezionych w docs jabłoni dotyczących remote control events masz dwie klasy MPRemoteCommand
i MPRemoteCommandCenter
podświetlona. Wyszukiwanie MPRemoteCommandCenter pokaże Ci, że istnieje wiele poleceń, takich jak likeCommand
lub dislikeCommand
, do których możesz dodać moduły obsługi. Dodanie procedur obsługi do tych poleceń powoduje, że są one wyświetlane w centrum sterowania.
Poniżej jest jakiś kod all-in-one osiągnięcie dość dużo dokładnie te same wyniki przedstawiono na swoich screenach:
- (void)showCustomizedControlCenter {
/* basic audio initialization */
NSString *soundFilePath = [NSString stringWithFormat:@"%@/test.mp3", [[NSBundle mainBundle] resourcePath]];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
self.player.numberOfLoops = -1;
[self.player play];
/* registering as global audio playback */
[[AVAudioSession sharedInstance] setActive:YES error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
/* the cool control center registration */
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
[commandCenter.playCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent *event) {
return MPRemoteCommandHandlerStatusSuccess;
}];
[commandCenter.dislikeCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent *event) {
return MPRemoteCommandHandlerStatusSuccess;
}];
[commandCenter.likeCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent *event) {
return MPRemoteCommandHandlerStatusSuccess;
}];
[commandCenter.nextTrackCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent *event) {
return MPRemoteCommandHandlerStatusSuccess;
}];
/* setting the track title, album title and button texts to match the screenshot */
commandCenter.likeCommand.localizedTitle = @"Thumb Up";
commandCenter.dislikeCommand.localizedTitle = @"Thumb down";
MPNowPlayingInfoCenter* info = [MPNowPlayingInfoCenter defaultCenter];
NSMutableDictionary* newInfo = [NSMutableDictionary dictionary];
[newInfo setObject:@"Mixtape" forKey:MPMediaItemPropertyTitle];
[newInfo setObject:@"Jamie Cullum" forKey:MPMediaItemPropertyArtist];
info.nowPlayingInfo = newInfo;
}
Oprócz pisania kodu trzeba
- dodać
AVFoundation
do swój projekt
#import <AVFoundation/AVFoundation.h>
i #import <MediaPlayer/MediaPlayer.h>
- włączyć tryby tła
"Audio and AirPlay"
w ustawieniach aplikacji.