Access BPM field on a song (MPMediaItemPropertyBea

2020-02-26 03:42发布

问题:

I'm building an application that has a media player in it (I'm using iOS SDK 4.2). While a given song is playing, I can access pretty much every one of the properties, but I can not access MPMediaItemPropertyBeatsPerMinute.

It returns null every time.

The Apple doc states:

The number of musical beats per minute for the media item, corresponding to the “BPM” field in the Info tab in the Get Info dialog in iTunes. Value is an NSNumber object representing an NSUInteger data type.

Available in iOS 4.0 and later.

Declared in MPMediaItem.h.

Please note that the songs I am using DO have the BPM data in the BPM field. I know it is not set by default.

The code I have been trying is:

MPMusicPlayerController *musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
// ...
MPMediaItem *currentItem = [musicPlayer nowPlayingItem];
if (currentItem != NULL) {
  NSLog([NSString stringWithFormat:@"%@", [currentItem valueForProperty:MPMediaItemPropertyBeatsPerMinute]]);
}

The code above spits out "(null)" for every song.

Since I was able to access every other property of the current song this way, I believe this could be a bug in the SDK.

So, my question is slightly two-fold.

  1. Am I doing this wrong, or is this a known bug in the SDK?
  2. How else could I go about retrieving the BPM data on the song?

回答1:

I've tested this issue with iOS SDK 4.3 and can confirm that it works. Your code looks okay however.

It might be a bug in the 4.2 SDK that has been fixed in 4.3. I can also imagine that older versions of iTunes do not sync the BPM property correctly.

You could try to use the string @"beatsPerMinute" instead of MPMediaItemPropertyBeatsPerMinute (works in iOS 4.3), but I guess that won't resolve your problem.