Search code examples
iosobjective-cspotifycontrol-center

Display Spotify now playing item info in Control Center


I am using the new SDK for Spotify in my iOS app . In this I am using the below code for playing song from Spotify.I am able to play the song. But I am unable to see the now playing item song info and unable to control the songs from the iOS Control Center. I need to update the song info to the Control center.

[SPTTrack trackWithURI:appDelegate.player.currentTrackURI
               session:auth.session
              callback:^(NSError *error, SPTTrack *track) {

                  self.trackTitle.text = track.name;

                  SPTPartialArtist *artist = [track.artists objectAtIndex:0];
                  self.trackArtist.text = artist.name;

                  appDelegate.currentPlayingSongName = track.name;
                  appDelegate.currentPlayingArtistName = artist.name;

                  //[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = [NSDictionary dictionaryWithObject:track.name forKey: MPMediaItemPropertyTitle];

                  Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter");

                  if (playingInfoCenter) {

                      NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];

                      [songInfo setObject:@"Audio Title" forKey:MPMediaItemPropertyTitle];
                      [songInfo setObject:@"Audio Author" forKey:MPMediaItemPropertyArtist];
                      [songInfo setObject:@"Audio Album" forKey:MPMediaItemPropertyAlbumTitle];
                      [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];


                  }

}]; 

Thank you for reading my question. Thanks in advance


Solution

  • I was yesterday looking for the same info when I found your post. I figured it out since then. But this is in swift. I am just starting to learn xcode and swift. So no idea how to translate this in objc. Hope this helps ;)

    //show now playing info:
    
    var player: SPTAudioStreamingController?
    let songInfo = [
                    MPMediaItemPropertyTitle:  self.player!.currentTrackMetadata[SPTAudioStreamingMetadataTrackName] as! String,
                    MPMediaItemPropertyArtist: self.player!.currentTrackMetadata[SPTAudioStreamingMetadataArtistName] as! String,
                    MPMediaItemPropertyArtwork: albumArt,
                    MPNowPlayingInfoPropertyElapsedPlaybackTime: self.player!.currentPlaybackPosition,
                    MPMediaItemPropertyPlaybackDuration: self.player!.currentTrackDuration,
                    MPNowPlayingInfoPropertyPlaybackRate:  1.0
                ]
                MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = songInfo
    
    
    //setup and receive remote control events:
    
    UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
    
    // then 
    
    override func remoteControlReceivedWithEvent(event: UIEvent?) {
        if event!.type == UIEventType.RemoteControl {
            if event!.subtype == UIEventSubtype.RemoteControlPlay {
                togglePlay()
    //etc...