iOS 8 : Get title of song being played by the system music player

标签 ios objective-c

我正在尝试获取默认音乐应用当前正在播放的歌曲的标题。方法如下:

- (NSString*)getSongTitle {
  MPMediaItem *currentSong = [[MPMusicPlayerController systemMusicPlayer] nowPlayingItem];
  _title = [currentSong valueForProperty:@"MPMediaGroupingTitle"];
  NSLog(_title);
  return _title;
}

我在网上读到的所有内容都说这应该是正确的,但是 _title 每次都被赋值为 nil。有什么想法吗?

顺便说一句,我有@imported MediaPlayer

最佳答案

我认为您只是使用了错误的 key 。试试 MPMediaItemPropertyTitle。它应该看起来像这样:

- (NSString*)getSongTitle {
  MPMediaItem *currentSong = [[MPMusicPlayerController systemMusicPlayer] nowPlayingItem];
  _title = [currentSong valueForProperty:MPMediaItemPropertyTitle];
  NSLog(_title);
  return _title;
}

关于iOS 8 : Get title of song being played by the system music player,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31481255/

相关文章:

iphone - 如何对 iOS 应用程序使用的服务器进行负载测试?

ios - 是否有任何用 C/C++ 编写的开源 EGL 测试套件?

iOS 即席分发更新

ios - 保存后 Core Data objectID 为 nil

ios - 如何自定义 UICollectionView Cell 以显示如附件所示的图像网格?

objective-c - 当 nsnotification 发送到非事件 View Controller 时会发生什么?

ios - Facebook 权限 Facebook SDK for iOS 5

ios - UISwipeGesture 'unrecognized selector sent to instance'

objective-c - 从第三个 View Controller 导航项弹出到第一个 View Controller ( Root View Controller )未更改

objective-c - 如何存储类变量,然后在 Objective-C 中调用该类的静态方法