Spotify如何在iOS上自定义媒体播放控件? [英] How does Spotify customize the media playback controls on iOS?

查看:214
本文介绍了Spotify如何在iOS上自定义媒体播放控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Spotify on iOS has a very interesting Control Center integration. Notice the hamburger button below.

同样的事情在锁定屏幕上!

The same thing is on the lock screen!

他们是如何做到的? MPMediaCenter中是否有API?

How do they do those? Is there an API in MPMediaCenter or something?

推荐答案

是的,有一个API

查看有关远程控制事件你得到两个类 MPRemoteCommand MPRemoteCommandCenter 突出显示。查找 MPRemoteCommandCenter 将向您显示有许多命令,例如 likeCommand dislikeCommand 您可以添加处理程序。向这些命令添加处理程序会导致它们显示在控制中心。

Looking at the instructions found in the apple docs regarding remote control events you get two classes MPRemoteCommand and MPRemoteCommandCenter highlighted. Looking up MPRemoteCommandCenter will show you there are a multitude of commands like likeCommand or dislikeCommand you can add handlers for. Adding handlers to those commands causes them beeing displayed in the control center.

下面是一些一体化代码,它实现了截图中显示的完全相同的结果:

Below is some all-in-one code achieving pretty much exactly the same results shown on your screenshots:

- (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>

  • 在应用设置中激活后台模式音频和播放

  • add AVFoundation to your project
  • #import <AVFoundation/AVFoundation.h> and #import <MediaPlayer/MediaPlayer.h>
  • activate the Background Modes "Audio and AirPlay" in the app settings.


这篇关于Spotify如何在iOS上自定义媒体播放控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆