我想选择从iPod库的歌曲和使用播放avplayer我想要的音乐继续播放应用程序切换到后台我是新来的iOS编程即使有人能帮助我..
谢谢
我想选择从iPod库的歌曲和使用播放avplayer我想要的音乐继续播放应用程序切换到后台我是新来的iOS编程即使有人能帮助我..
谢谢
为了让用户从他们的音乐库中选择一首歌曲(或歌曲),使用MPMediaPickerController
类。
-(void) pickSong {
// Create picker view
MPMediaPickerController* picker = [[MPMediaPickerController alloc] init];
picker.delegate = self;
// Check how to display
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
// Show in popover
[popover dismissPopoverAnimated:YES];
popover = [[UIPopoverController alloc] initWithContentViewController:picker];
[popover presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
} else {
// Present modally
[self presentViewController:picker animated:YES completion:nil];
}
}
更改self.navigationItem.rightBarButtonItem
如果你不显示它从一个按钮上的标题栏的右侧。
然后,你要听通过实现委托的结果:
当调用用户取消了选择:
-(void) mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker {
// Dismiss selection view
[self dismissViewControllerAnimated:YES completion:nil];
[popover dismissPopoverAnimated:YES];
popover = nil;
}
当调用用户选择的内容:
-(void) mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {
// Dismiss selection view
[self dismissViewControllerAnimated:YES completion:nil];
[popover dismissPopoverAnimated:YES];
popover = nil;
// Get AVAsset
NSURL* assetUrl = [mediaItemCollection.representativeItem valueForProperty:MPMediaItemPropertyAssetURL];
AVURLAsset* asset = [AVURLAsset URLAssetWithURL:assetUrl options:nil];
// Create player item
AVPlayerItem* playerItem = [AVPlayerItem playerItemWithAsset:asset];
// Play it
AVPlayer* myPlayer = [AVPlayer playerWithPlayerItem:playerItem];
[myPlayer play];
}
你需要一个UIPopoverController* popover;
在类.h文件中。 你也应该保留myPlayer
某处...
为了让音乐继续在后台,一个添加audio
下在你的Info.plist字符串数组UIBackgroundModes
关键。