iOS の Spotify には、非常に興味深いコントロール センターの統合があります。下のハンバーガーボタンに注目してください。
ロック画面も同じ!
彼らはどうやってそれらをしますか?MPMediaCenter などに API はありますか?
iOS の Spotify には、非常に興味深いコントロール センターの統合があります。下のハンバーガーボタンに注目してください。
ロック画面も同じ!
彼らはどうやってそれらをしますか?MPMediaCenter などに API はありますか?
はい、そのための API があります
リモート コントロール イベントに関する Apple ドキュメントの説明を見ると、2 つのクラスMPRemoteCommand
がMPRemoteCommandCenter
強調表示されています。MPRemoteCommandCenterを調べると、次のようなコマンドが多数あることがわかります。likeCommand
またはdislikeCommand
、ハンドラを追加できます。これらのコマンドにハンドラーを追加すると、コントロール センターに表示されます。
以下は、スクリーンショットに示されているのとほぼ同じ結果を達成するオールインワン コードです。
- (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;
}
必要なコードを書くことに加えて、
AVFoundation
プロジェクトに追加#import <AVFoundation/AVFoundation.h>
と#import <MediaPlayer/MediaPlayer.h>
"Audio and AirPlay"
アプリの設定でバックグラウンド モードを有効にします。