iOS,当音乐改变时阅读标题音乐ipod

时间:2014-01-29 19:25:45

标签: nsnotificationcenter ipod nsnotifications mpmusicplayercontroller mpmediaitem

我想从iPod音乐目前的音乐播放中获取标题和艺术家名称 但是我希望当音乐改变时我可以检测到并且UI标签正在改变

这是代码:

 - (IBAction)getTitlemusic
{
   // MPMediaItem * currentItem = self.musicPlayer.nowPlayingItem;


    MPMediaItem * song = [[MPMusicPlayerController iPodMusicPlayer] nowPlayingItem];
    NSString * album = [song valueForProperty:MPMediaItemPropertyAlbumTitle];
    NSString * title = [song valueForProperty:MPMediaItemPropertyTitle];
    NSString * artist = [song valueForProperty:MPMediaItemPropertyArtist];


    NSLog(@"%@,%@,%@", album,title,artist);

    songlabel.text = [NSString stringWithFormat:@"%@",album ];

    arttistlabel.text =  [NSString stringWithFormat:@"%@\r%@",title,artist];


      [self performSelector:@selector(getTitlemusic) withObject:nil afterDelay:10.0f];

}

我知道计时器和延迟不太好主意

1 个答案:

答案 0 :(得分:0)

MPMusicPlayerController *player = [MPMusicPlayerController iPodMusicPlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(getTitlemusic)
                                             name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification
                                           object:player];
[player beginGeneratingPlaybackNotifications];

使用此代码