我一直在cocos-2dx中創建一個項目,但需要創建一個.mm來從iOS庫中選擇音樂以在後臺播放。我通過播放.mm文件中的音樂來實現它,但出於各種原因,它需要使用SimpleAudioEngine在cocos-2dx中播放。我已經嘗試將url的absoluteString轉換爲NSString以將id轉換爲const char *以將其移回到cocos-2dx文件,但它仍然沒有運行。使用MPMediaItem和simpleaudioengine
這是它給了我的absoluteString(iPod的庫://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;
}
二.mm
const char* MediaPicker::songUrl(){
id na1 = [[MediaPickerWrapper sharedIntance] returnsongUrl];
const char *cString = [na1 cStringUsingEncoding:NSASCIIStringEncoding];
return cString;
}
科科斯-2DX文件
CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(media->songUrl());
CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic(media->songUrl(),false);