检测当前在iOS音乐应用中播放的歌曲信息

时间:2014-06-16 22:37:23

标签: ios objective-c ios7

我有一些问题需要在原生iOS音乐应用中获取当前播放的歌曲信息,如(标题,艺术家......)。

这是确切的问题,我的应用使用MPMusicPlayerController iPodMusicPlayer此属性调用myPlayer。当用户在我的应用程序中控制音乐时,我能够以这种方式显示当前歌曲播放信息......

- (void)getTrackDescription {

    // getting whats currently playing
    self.nowPlayingItem = [myPlayer nowPlayingItem];

    // song title currently playing
    self.title = [self.nowPlayingItem valueForProperty:MPMediaItemPropertyTitle];

    // if title is not fund Unknown will be displayed
    if (title == (id)[NSNull null] || title.length == 0) {
        title = @"Unknown";
    }

    // artist currently playing
    self.artist = [self.nowPlayingItem valueForProperty:MPMediaItemPropertyArtist];

    // if artist is not fund Unknown will be displayed
    if (artist == (id)[NSNull null] || artist.length == 0) {
        artist = @"Unknown";
    }

    //[self.currentlyPlaying setLineBreakMode:NSLineBreakByWordWrapping];

    // displaying current artist and title song playing
    [self.currentlyPlaying setText:[NSString stringWithFormat:@"%@ - %@", artist, title]];
}

但是,问题出现在用户将应用程序留在后台或只是使用控制中心更改歌曲时..我的应用程序仍会显示上一首歌曲信息,如果用户继续使用Control Center或音乐应用程序本身。

我试图以这种方式解决问题......

- (void)viewDidLoad {
    [super viewDidLoad];

    if (self.myPlayer.playbackState == MPMusicPlaybackStatePlaying || self.myPlayer.playbackState == MPMusicPlaybackStateSeekingForward || self.myPlayer.playbackState == MPMusicPlaybackStateSeekingBackward){
        // updating track name regardless the user uses app controllers or not
        [self getTrackDescription];
    } 
}

我甚至尝试评论if条件,看看我是否以这种方式获得歌曲信息,但这是同样的问题。

好吧,我希望有人可以帮助我,解释我做错了什么..

提前致谢......!

更新

这就是我现在所拥有的...我的getTrackDescription逻辑是相同的,但签名已更改为此- (void)getTrackDescription:(id)notification

- (void)viewWillAppear:(BOOL)animated{

    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];

    [notificationCenter addObserver:self
                           selector:@selector(getTrackDescription:)
                               name:MPMusicPlayerControllerPlaybackStateDidChangeNotification
                             object:self.myPlayer];
    [self.myPlayer beginGeneratingPlaybackNotifications];

}

-(void)viewWillDisappear:(BOOL)animated{

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                               name:MPMusicPlayerControllerPlaybackStateDidChangeNotification
                                               object:self.myPlayer];
    [self.myPlayer endGeneratingPlaybackNotifications];
}

2 个答案:

答案 0 :(得分:3)

您要做的是将视图控制器作为观察者添加到MPMusicPlayerControllerNowPlayingItemDidChangeNotification通知中,听起来,每当音乐播放器中的曲目发生变化时,都会调用它。不要忘记使用以下方法指定播放器何时开始/结束生成这些通知。

[[MPMusicPlayerController iPodMusicPlayer] beginGeneratingPlaybackNotifications];
[[MPMusicPlayerController iPodMusicPlayer] endGeneratingPlaybackNotifications];

答案 1 :(得分:0)

这是我对这个问题的完整答案。

1)我设置了这两个方法..并设置Observer MPMusicPlayerControllerNowPlayingItemDidChangeNotification,它将负责更新我的歌曲信息,无论是getTrackDescription还是与我之前的帖子相同......

- (void)viewWillAppear:(BOOL)animated{

    // creating simple audio player
    self.myPlayer = [MPMusicPlayerController iPodMusicPlayer];

    // assing a playback queue containing all media items on the device
    [self.myPlayer setQueueWithQuery:[MPMediaQuery songsQuery]];

    self.notificationCenter = [NSNotificationCenter defaultCenter];

    [self.notificationCenter addObserver:self
                                selector:@selector(getTrackDescription:)
                                    name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification
                                    object:self.myPlayer];

    [self.notificationCenter addObserver:self
                                selector:@selector(handle_PlayBackNotification:)
                                    name:MPMusicPlayerControllerPlaybackStateDidChangeNotification
                                    object:self.myPlayer];


    [self.myPlayer beginGeneratingPlaybackNotifications];

}

-(void)viewWillDisappear:(BOOL)animated {

    [self.notificationCenter removeObserver:self
                                name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification
                                object:self.myPlayer];

    [self.notificationCenter removeObserver:self
                                    name:MPMusicPlayerControllerPlaybackStateDidChangeNotification
                                    object:self.myPlayer];

    [self.myPlayer endGeneratingPlaybackNotifications];

}

然后魔术本身会发生..它基本上是@ 0x7fffffff建议我做的事情,但我不得不做一些挖掘,因为我根本不熟悉通知。

所以,如果你想看看如何为Observer MPMusicPlayerControllerPlaybackStateDidChangeNotification切换我的播放/暂停按钮,这就是我这样做的方法..

- (void)handle_PlayBackNotification:(id)notification{

    if(myPlayer.playbackState == 1){

        // sets the pause image in play button
        [self.pauseBtn setBackgroundImage:[UIImage imageNamed:@"pauseBtn2.png"] forState:(UIControlStateNormal)];

    } else {

        // resets the image to normal play image
        [self.pauseBtn setBackgroundImage: nil forState:(UIControlStateNormal)];


    }

}

以防这是getTrackDescription ...

- (void)getTrackDescription:(id)notification {

    // getting whats currently playing
    self.nowPlayingItem = self.myPlayer.nowPlayingItem;

    // song title currently playing
    self.title = [self.nowPlayingItem valueForProperty:MPMediaItemPropertyTitle];

    // if title is not fund Unknown will be displayed
    if (title == (id)[NSNull null] || title.length == 0) {
        title = @"Unknown";
    }

    // artist currently playing
    self.artist = [self.nowPlayingItem valueForProperty:MPMediaItemPropertyArtist];

    // if artist is not fund Unknown will be displayed
    if (artist == (id)[NSNull null] || artist.length == 0) {
        artist = @"Unknown";
    }

    // displaying current artist and title song playing
    [self.currentlyPlaying setText:[NSString stringWithFormat:@"%@ - %@", artist, title]];

}