检测原生iOS Music App中当前正在播放的歌曲信息

Posted

技术标签:

【中文标题】检测原生iOS Music App中当前正在播放的歌曲信息【英文标题】:Detect currently playing song information in native iOS Music App 【发布时间】:2014-06-16 22:37:23 【问题描述】:

我在本地 ios 音乐应用程序中获取当前正在播放的歌曲信息(如(标题、艺术家...)时遇到了一些问题。

这是确切的问题,我的应用程序使用 MPMusicPlayerControlleriPodMusicPlayer 此属性调用 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]];

但是,当用户将应用程序留在后台或仅使用 控制中心 更改歌曲时,问题就出现了。我的应用程序仍然显示以前的歌曲信息,如果用户继续使用控制中心或音乐应用程序本身。

我试着用这种方式解决问题……

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

【问题讨论】:

【参考方案1】:

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

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

【讨论】:

我应该在 getTrackDescription 或 viewDidLoad 的什么地方实现它? 您可能应该在viewWillAppear: 中将自己添加为观察者,并在viewDidDisappear: 中删除观察者。您需要在通知的回调中调用 getTrackDescription 好的,我已尝试实施您的建议,但无法正常工作,您能否在帖子中看到我的更新部分并让我知道问题所在.. 或任何建议..@0x7fffffff【参考方案2】:

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

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


【讨论】:

以上是关于检测原生iOS Music App中当前正在播放的歌曲信息的主要内容,如果未能解决你的问题,请参考以下文章

iOS App:用原生播放器替换 webview 中的 Flash

使用 C# 和 Xamarin 按下 F8(播放)按钮后阻止 iTunes / Music.app 启动

vue-music 关于playlist (底部播放列表组件)

vue-music 关于playlist (底部播放列表组件)

pygame:获取音乐的当前时间

在 iOS 应用程序之间交换数据