【问题标题】:How does Spotify customize the media playback controls on iOS?Spotify 如何自定义 iOS 上的媒体播放控件?
【发布时间】:2015-07-27 18:00:54
【问题描述】:

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

锁屏上也是一样!

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

【问题讨论】:

    标签: ios mpmediaplayercontroller control-center


    【解决方案1】:

    是的,有一个 API

    查看苹果文档中关于remote control events 的说明,您会看到两个类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"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-03
      • 2020-05-10
      • 2012-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-29
      相关资源
      最近更新 更多