13

iOS の Spotify には、非常に興味深いコントロール センターの統合があります。下のハンバーガーボタンに注目してください。

ハンバーガー

ロック画面も同じ!

ロック画面

彼らはどうやってそれらをしますか?MPMediaCenter などに API はありますか?

4

1 に答える 1

18

はい、そのための API があります

リモート コントロール イベントに関する Apple ドキュメントの説明を見ると、2 つのクラスMPRemoteCommandMPRemoteCommandCenter強調表示されています。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"アプリの設定でバックグラウンド モードを有効にします。

ここに画像の説明を入力 ここに画像の説明を入力

于 2015-05-15T18:07:12.820 に答える