如何在 AVPlayer 中播放 iPod 库文件

发布于 2024-08-15 15:41:39 字数 41 浏览 7 评论 0原文

如何将 iPod 库音乐文件导入到 AVAudioPlayer 中?

How can I get an ipod library music file into AVAudioPlayer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

苏大泽ㄣ 2024-08-22 15:41:39

正如 David 提到的,还有更多的工作要做,例如,您必须管理播放媒体项目集合中的下一个曲目,但这里有一种方法,可以使用用户从 iPod Picker 中选择的一组 MPMediaItems 来实现此目的。 AssetURL 是您使用的,它为您提供 MP3 文件的路径(例如 ipod-library://item/item.mp3?id=-6889145242935454020)

NSURL *anUrl = [[mediaItems objectAtIndex: 0] valueForProperty:MPMediaItemPropertyAssetURL];
self.audioPlayerMusic = [[[AVPlayer alloc] initWithURL:anUrl] retain];                      
[self.audioPlayerMusic play];

As David mentions there is more work to do than this, for example you have to manage playing the next track in a collection of media items, but here is one way to do it with a set of MPMediaItems that a user selected from the iPod Picker. The AssetURL is what you use, it gives you a path to the MP3 file (e.g. ipod-library://item/item.mp3?id=-6889145242935454020)

NSURL *anUrl = [[mediaItems objectAtIndex: 0] valueForProperty:MPMediaItemPropertyAssetURL];
self.audioPlayerMusic = [[[AVPlayer alloc] initWithURL:anUrl] retain];                      
[self.audioPlayerMusic play];
笛声青案梦长安 2024-08-22 15:41:39

是的,您可以使用 SDK 播放 iPod 库中的歌曲,而无需借助 MPMusicPlayerController 类。

更基本的 AVPlayer 类可以通过使用歌曲的 MPMediaItemPropertyAssetURL 属性中的 NSUrl 值来处理 iPod 库中的音频文件。您必须做更多的工作才能正确设置所有内容,但这是可以完成的。

Yes, you can play songs from the iPod library using the SDK without resorting to the MPMusicPlayerController class.

The more basic AVPlayer class can handle audio files from the iPod library by using the NSUrl value from the song's MPMediaItemPropertyAssetURL property. You have to do a lot more work to get everything setup properly, but it can be done.

茶花眉 2024-08-22 15:41:39

SDK 没有提供从 iPod 库读取文件的功能(正如您需要使用 AVAudioPlayer 那样),可能是出于反盗版的原因。要播放 iPod 库项目,请使用 MPMusicPlayerController 类。

编辑:这不再准确。请参阅下面描述 AVPlayer 类的使用的答案。

The SDK has no provision for reading files from the iPod library (as you'd need to do to use AVAudioPlayer with it), probably for anti-piracy reasons. To play iPod library items, use the MPMusicPlayerController class.

Edit: This is no longer accurate. See the below answers which describe the use of the AVPlayer class.

北城挽邺 2024-08-22 15:41:39

是否有可能在 MPMusicPlayerController 中获取有关 dB 计量的信息?也许启动一个AVAudioSession来并行录音就可以完成这项工作?我需要 dB 值来构建某种体积摄谱仪。

Is there any possibility to get information about dB-metering in MPMusicPlayerController? Perhaps initiating an AVAudioSession for Recording in parallel would do the job?? I need dB-values to build some kind of volume-spectrograph.

诺曦 2024-08-22 15:41:39
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {
    NSURL *url = [[mediaItemCollection.items objectAtIndex:0] valueForProperty:MPMediaItemPropertyAssetURL];

    NSError *error;
    self.player = [[AVAudioPlayer alloc] url error:&error];

    if (!error) {
        [self.player prepareToPlay];
        [self.player play];
    }

    [mediaPicker dismissModalViewControllerAnimated:YES];
}
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {
    NSURL *url = [[mediaItemCollection.items objectAtIndex:0] valueForProperty:MPMediaItemPropertyAssetURL];

    NSError *error;
    self.player = [[AVAudioPlayer alloc] url error:&error];

    if (!error) {
        [self.player prepareToPlay];
        [self.player play];
    }

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