使用 CocosDenshion 播放 iPod 库歌曲

发布于 2024-12-23 10:15:17 字数 952 浏览 1 评论 0原文

我试图让用户从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心凉 2024-12-30 10:15:17

查看调试器中 [song valueForProperty:MPMediaItemPropertyAssetURL] 返回的 URL。它不是标准文件系统 URL,而是指向资源库的自定义 URL,因此难怪 CocosDenshion 无法打开它。

您需要使用 AV Foundation(可能是 AVAudioPlayer)来播放该歌曲。或者使用 AV Foundation 首先将歌曲转换为“真实”文件,然后使用 Cocos Denshion 播放,但这可能比第一个选项复杂得多。

MPMediaItemPropertyAssetURL 的文档也清楚地说明了这一点:

不支持在 AV Foundation 框架之外使用 URL。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文