Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Spotify customize the media playback controls on iOS?

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

Hamburger

The same thing is on the lock screen!

lock screen

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

like image 841
Moshe Avatar asked May 15 '15 17:05

Moshe


People also ask

How do you put playback on Spotify?

Go to Home > Settings > Playback > Equalizer and switch it on. Then, tap one of the presets available to you or customize it by dragging the dots yourself. If you're an Android user, the option to use the Spotify equalizer may depend on your device manufacturer.

How do I restrict playback on Spotify?

Check the toggle button next to the 'Explicit content' option on the 'Settings' screen. If it is green, click to turn it grey. A grey toggle suggests that playback of explicit-rated content is OFF. Green represents enabling explicit content playback on Spotify.

How to make music sound better on Spotify?

How to Make Spotify Sound Better: 6 Settings to Tweak. 1 1. Normalize Volume Level. We’ve all experienced that moment when a new song plays a lot louder than the previous one. With Normalize, Spotify adjusts ... 2 2. Audio Quality Control. 3 3. Equalizer. 4 4. Crossfade. 5 5. Gapless. More items

What are the top Spotify settings to make a difference?

Here are the top Spotify settings that you can change to make the most difference. 1. Normalize Volume Level. We’ve all experienced that moment when a new song plays a lot louder than the previous one. With Normalize, Spotify adjusts the volume levels for you to prevent this from happening. To change this, go to Home > Settings > Normalize.

How to equalize Spotify audio on Android?

If you’re an Android user, the option to equalize on Spotify may depend on your device manufacturer. To know if your device can handle it, you can check the option at Home > Settings > Audio Quality.

Why is my Spotify volume higher than the previous one?

We’ve all experienced that moment when a new song plays a lot louder than the previous one. With Normalize, Spotify adjusts the volume levels for you to prevent this from happening. To change this, go to Home > Settings > Normalize. Then, you can select the Volume Level that automatically adjusts all Spotify tracks to your environment.


1 Answers

Yes, there is an API for that

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;
}

In addition to writing the code you need to

  • 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.

enter image description hereenter image description here

like image 51
luk2302 Avatar answered Oct 02 '22 14:10

luk2302