Playing iPod Library songs using CocosDenshion
-
11-04-2021 - |
Question
I am trying to let the user select a song from the iPod Library. After getting the song URL from that, I am trying to play it using the CocosDenshion
library but I am not getting any sound.
Here's the code sample of MPMediaPickerControllerDelegate
:
#pragma mark MPMediaPickerControllerDelegate
- (void)mediaPicker: (MPMediaPickerController *)mediaPicker
didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {
[self dismissModalViewControllerAnimated:YES];
if ([mediaItemCollection count] < 1) {
return;
}
[song release];
song = [[[mediaItemCollection items] objectAtIndex:0] retain];
NSURL *itemURL = [song valueForProperty:MPMediaItemPropertyAssetURL];
fileURL = [itemURL absoluteString];
[[SimpleAudioEngine sharedEngine] preloadEffect:fileURL];
}
and here's the handler for the button that plays the sound.
- (IBAction)playSong {
[[SimpleAudioEngine sharedEngine] playEffect:fileURL pitch:1.0f pan:0.0f gain:1.0f];
}
Can anyone tell me what is going wrong. I need to be able to change the pitch of the selected song. That is why I am using CocosDenshion library.
Solution
Have a look at the URL returned by [song valueForProperty:MPMediaItemPropertyAssetURL]
in the debugger. It is not a standard file system URL but a custom URL that points into the asset library so it is no wonder CocosDenshion can't open it.
You will need to use AV Foundation (probably AVAudioPlayer
) to play the song. Or use AV Foundation to first convert the song into a "real" file and then play it back with Cocos Denshion but that is probably much more complicated than the first option.
The documentation for MPMediaItemPropertyAssetURL
clearly says so, too:
Usage of the URL outside of the AV Foundation framework is not supported.