使用 CocosDenshion 播放 iPod 库歌曲
我试图让用户从 iPod 库中选择一首歌曲。从中获取歌曲 URL 后,我尝试使用 CocosDenshion 库播放它,但没有收到任何声音。
这是 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];
}
这是播放声音的按钮的处理程序。
- (IBAction)playSong {
[[SimpleAudioEngine sharedEngine] playEffect:fileURL pitch:1.0f pan:0.0f gain:1.0f];
}
谁能告诉我出了什么问题。我需要能够改变所选歌曲的音高。这就是我使用 CocosDenshion 库的原因。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看调试器中
[song valueForProperty:MPMediaItemPropertyAssetURL]
返回的 URL。它不是标准文件系统 URL,而是指向资源库的自定义 URL,因此难怪 CocosDenshion 无法打开它。您需要使用 AV Foundation(可能是
AVAudioPlayer
)来播放该歌曲。或者使用 AV Foundation 首先将歌曲转换为“真实”文件,然后使用 Cocos Denshion 播放,但这可能比第一个选项复杂得多。MPMediaItemPropertyAssetURL
的文档也清楚地说明了这一点: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: