我正在尝试获取默认音乐应用当前正在播放的歌曲的标题。方法如下:
- (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/