将MPMediaItems与AVAudioPlayer一起使用 [英] Using MPMediaItems with AVAudioPlayer
问题描述
是否可以使用MPMediaPickerController选择媒体项目,然后将它们加载到AVAudioPlayer对象中?
Is it possible to pick media items using MPMediaPickerController and then load them into an AVAudioPlayer object?
推荐答案
如果 MPMusicPlayerController
无法满足您的需求,您可以将音频复制到本地软件包,这样您就可以使用 AVAudioPlayer
。
If MPMusicPlayerController
doesn't meet your needs, you can copy the audio to your local bundle so you can use AVAudioPlayer
.
编辑
您基本上有三个选项可以播放来自用户iPod的音频library: MPMediaPlayer
, AVPlayer
和 AVAudioPlayer
。
You basically have three options for playing audio from the user's iPod library: MPMediaPlayer
, AVPlayer
and AVAudioPlayer
.
以下是 MPMediaPlayer
和 AVPlayer
的示例:
- (void) mediaPicker: (MPMediaPickerController *) mediaPicker
didPickMediaItems: (MPMediaItemCollection *) collection {
MPMediaItem *item = [[collection items] objectAtIndex:0];
NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL];
[self dismissModalViewControllerAnimated: YES];
// Play the item using MPMusicPlayer
MPMusicPlayerController* appMusicPlayer = [MPMusicPlayerController applicationMusicPlayer];
[appMusicPlayer setQueueWithItemCollection:collection];
[appMusicPlayer play];
// Play the item using AVPlayer
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:url];
AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
[player play];
}
如果你需要使用 AVAudioPlayer
由于某种原因,或者您需要访问音频文件的实际音频数据,您必须先将音频文件复制到应用程序的目录中,然后在那里使用它。 AVAsset
+ AVPlayer
东西最接近 ALAsset
如果您习惯使用照片和视频。
If you need to use AVAudioPlayer
for some reason, or you need access to the audio file's actual audio data, you have to first copy the audio file to your app's directory and then work with it there. The AVAsset
+ AVPlayer
stuff is the closest analogy to ALAsset
if you're used to working with photos and videos.
这篇关于将MPMediaItems与AVAudioPlayer一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!