首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用MPMediaPickerController拾取音频

用MPMediaPickerController拾取音频
EN

Stack Overflow用户
提问于 2014-04-07 08:21:15
回答 2查看 3.3K关注 0票数 3

嗨,我正在使用MPMediaPickerController从电话库中选择音频文件。但它给了我空白的屏幕。我不明白为什么会这样。我在模拟器上运行应用程序。这是我的代码

代码语言:javascript
复制
- (IBAction)selectFile:(UIButton *)sender{

MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeMusic];
mediaPicker.delegate = self;
mediaPicker.allowsPickingMultipleItems = YES;
mediaPicker.prompt = @"Select Your Favourite Song!";
[mediaPicker loadView];
[self.navigationController presentViewController:mediaPicker animated:YES completion:nil]; }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-04-07 09:12:34

MPMediaPickerController在模拟器中不工作。苹果在“”下的"iPod图书馆访问编程指南“中注意到了这一点。便条上写着:

注意:要遵循这些步骤,您将需要一个配置设备,因为模拟器无法访问设备的iPod库。

为了防止断言,您可以始终检查是否可以在代码中访问此操作(下面的代码使用了ARCASDK5.0)。

代码语言:javascript
复制
MPMediaPickerController *picker = [[MPMediaPickerController alloc]     initWithMediaTypes:MPMediaTypeAnyAudio];

[picker setDelegate:self];
[picker setAllowsPickingMultipleItems:YES];
[picker setPrompt:NSLocalizedString(@"Add songs to play","Prompt in media item picker")];

@try {
[picker loadView]; // Will throw an exception in iOS simulator
[self presentViewController:picker animated:YES completion:nil];
}
@catch (NSException *exception) {
    [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Oops!",@"Error title")
                            message:NSLocalizedString(@"The music library is not available.",@"Error message when MPMediaPickerController fails to load") 
                           delegate:nil 
                  cancelButtonTitle:@"OK" 
                  otherButtonTitles:nil] show];
}
票数 1
EN

Stack Overflow用户

发布于 2016-03-28 20:26:17

对于那些想在Swift中找到答案的人来说:

代码语言:javascript
复制
@IBAction func openMediaPickerController(sender: AnyObject) {
        let picker = MPMediaPickerController()
        picker.delegate = self
        picker.allowsPickingMultipleItems = true
        picker.prompt = NSLocalizedString("Chose audio file", comment: "Please chose an audio file")
        self.presentViewController(picker, animated: true, completion: nil)
}
func mediaPickerDidCancel(mediaPicker: MPMediaPickerController) {
    self.dismissViewControllerAnimated(true, completion: nil)
}
func mediaPicker(mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
    //run any code you want once the user has picked their chosen audio
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22907074

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档