显示Spotify现在在控制中心中播放项目信息

时间:2016-02-22 09:33:44

标签: ios objective-c spotify control-center

我在iOS应用中使用了新的Spotify SDK。在这里,我使用以下代码播放Spotify中的歌曲。我能够播放这首歌。但我无法看到正在播放的项目歌曲信息,无法控制iOS控制中心的歌曲。我需要将歌曲信息更新到控制中心。

[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];


                  }

}]; 

感谢您阅读我的问题。提前致谢

1 个答案:

答案 0 :(得分:2)

我昨天在找到你的帖子时正在查找相同的信息。从那以后我就弄明白了。但这很快。我刚开始学习xcode和swift。所以不知道如何在objc中翻译它。希望这会有所帮助;)

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