2017-05-05 67 views
0

我有音乐播放器应用程序,当应用程序转到背景时,它会在锁定的屏幕上显示音乐控制,在我的情况下,正在播放广播艺术家和歌曲。我使用下列内容:刷新应用程序状态在背景上运行

- (void)applicationWillResignActive:(UIApplication *)application { 
    [[PlayerManager sharedInstance] setupInfoForLockerScreen]; 
} 

-(void)setupInfoForLockerScreen{ 

    MPNowPlayingInfoCenter *infoCenter = [MPNowPlayingInfoCenter defaultCenter]; 
    NSString *songName = self.currentPlaylist.lastItem.track.song.length > 0 ? self.currentPlaylist.lastItem.track.song : @""; 
    NSString *artistName = self.currentPlaylist.lastItem.track.artist.length > 0 ? self.currentPlaylist.lastItem.track.artist : @""; 
    infoCenter.nowPlayingInfo = @{ 
            MPMediaItemPropertyTitle:  self.currentPlaylist.title, 
            MPMediaItemPropertyArtist: songName.length > 0 && artistName.length > 0 ? [NSString stringWithFormat:@"%@ - %@", songName, artistName] : @"", 
            MPMediaItemPropertyPlaybackDuration: @(0) 
            }; 
} 

问题是,当数据发生变化,下一首歌曲将在电台,我怎么告诉我的应用程序来刷新自己? applicationWillResignActive我猜应用程序最初进入后台时只会调用一次。

回答

1

MPMusicPlayerController类有一些方法和事件来帮助解决这个问题。

首先,你需要告诉你的应用程序来监听MPMusicPlayerControllerNowPlayingItemDidChangeNotification事件:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNowPlayingItemChangedEvent:) name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification object:self.myMusicPlayer]; 

这将注册的事件处理函数被调用时正在播放的歌曲的变化。

然后调用您的MPMusicPlayerController上的beginGeneratingPlaybackNotifications方法,告诉它开始向您发送播放通知。

[self.myMusicPlayer beginGeneratingPlaybackNotifications]; 

当你想要得到通知,并可以控制,当你根据需要调用beginGeneratingPlaybackNotificationsendGeneratingPlaybackNotifications没有。

然后创建事件处理程序。这是将被调用每次MPMusicPlayerControllerNowPlayingItemDidChangeNotification火灾的方法:

现在,只要当前播放歌曲的变化,你的事件处理程序将被调用,您可以更新你现在玩的信息。

+0

Loughiln如果我没有使用MPMusicPlayerController播放音乐,该怎么办? –

相关问题