OGeek|极客世界-中国程序员成长平台

标题: iOS 8 : Get title of song being played by the system music player [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 09:05
标题: iOS 8 : Get title of song being played by the system music player

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

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

我在网上阅读的所有内容都表明这应该是正确的,但是 _title 每次都被分配 nil 。有什么想法吗?

顺便说一句,我有@imported MediaPlayer



Best Answer-推荐答案


我认为您只是使用了错误的 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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4