I've been making a project in cocos-2dx, but needed to create a .mm to select music from the iOS library to play in the background. I got it working by playing the music in the .mm file, but for various reasons, it needs to play in cocos-2dx using SimpleAudioEngine. I've tried converting the url's absoluteString to an NSString to an id to a const char* to move it back to the cocos-2dx files, but it still didn't run.
This is what it gave me for the absoluteString(ipod-library://item/item.m4a?id=456458322781804615)
.cc-
-(void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)collection{
if(collection){
MPMusicPlayerController* appMusicPlayer = [MPMusicPlayerController applicationMusicPlayer];
[appMusicPlayer setQueueWithItemCollection:collection];
[appMusicPlayer play];
MPMusicPlayerController *iPodMusicPlayerController = [MPMusicPlayerController iPodMusicPlayer];
MPMediaItem *nowPlayingItem = [iPodMusicPlayerController nowPlayingItem];
NSURL *url = [nowPlayingItem valueForProperty:MPMediaItemPropertyAssetURL];
NSString *filePath= [url absoluteString];
NSString *filePath2 = [url path];
_songUrl = filePath;
}
-(id)returnsongUrl{
return _songUrl;
}
Second .mm
const char* MediaPicker::songUrl(){
id na1 = [[MediaPickerWrapper sharedIntance] returnsongUrl];
const char *cString = [na1 cStringUsingEncoding:NSASCIIStringEncoding];
return cString;
}
Cocos-2dx file
CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(media->songUrl());
CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic(media->songUrl(),false);
It filters down to calling AVAudioPlayer's
initWithContentsOfURL:
method and should work with iPod media item urls. However, AVAudioPlayer only gained support for iPod URLs in iOS6. You may need to set a breakpoint to make sure your iPod URL path is getting passed through to theCDAudioManager's load
method correctly.You'll have to write your own wrapper and use AVPlayer or AVQueuePlayer if you want to target pre-iOS6 devices.
https://developer.apple.com/library/prerelease/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS6.html#//apple_ref/doc/uid/TP40011812-SW1