ios - Spotify 如何自定义 iOS 上的媒体播放控件?

标签 ios mpmediaplayercontroller control-center

iOS 上的 Spotify 有一个非常有趣的控制中心集成。注意下面的汉堡包按钮。

Hamburger

锁屏也是一样!

lock screen

他们是怎么做到的? MPMediaCenter 中有 API 吗?

最佳答案

是的,有一个 API

查看苹果文档中关于 remote control events 的说明你有两个类(class)MPRemoteCommandMPRemoteCommandCenter突出显示。抬头看MPRemoteCommandCenter会告诉你有很多命令,比如 likeCommanddislikeCommand您可以添加处理程序。向这些命令添加处理程序会使它们显示在控制中心中。

下面是一些一体化代码,可实现与屏幕截图中显示的结果几乎完全相同的结果:

- (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"在应用程序设置中。

enter image description here enter image description here

关于ios - Spotify 如何自定义 iOS 上的媒体播放控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30265813/

相关文章:

ios - 构建(ipa)大小的差异

ios - 403 未根据 Alamofire 的请求提供身份验证凭据

ios - 如何更改锁定屏幕/控制中心上的轨道位置?

ios - 更新 MPRemoteCommandCenter 播放/暂停按钮

ios - 从 UITableView 播放视频

ios - 获取在控制中心 iOS 上播放的当前轨道

ios - `URLByAppendingPathComponent` 不接受参数

iphone - 如何从底部对齐 UILabel 文本?

ios - MPMediaPickerController didPickMediaItems 返回 MPModelObjectMediaItem

ios - 如何使用systemMusicPlayer获取当前播放项目的播放列表?