4

我在我的 iOS 应用程序中使用新的 SDK for Spotify。在此,我使用以下代码从 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];


                  }

}]; 

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

4

1 回答 1

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...
于 2016-02-24T10:41:57.307 回答