如何在ios中获取UIImagePickerController中的音频文件与kuTTypeMovie相同

时间:2014-08-14 05:34:26

标签: ios iphone audio uiimagepickercontroller

我是iOS新手,我选择音频文件,如mp3,mp4等。

UIImagePickerControlle r相同,下面给出的代码工作正常,但它只获得视频文件而不是音频文件我只选择音频文件:

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
imagePickerController.delegate = self;
imagePickerController.sourceType =  UIImagePickerControllerSourceTypeSavedPhotosAlbum;
imagePickerController.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
[self presentViewController:imagePickerController animated:YES completion:nil];

当我替换

imagePickerController.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeAudio, nil];
带有kUTTypeMovie应用程序的

kUTTypeAudio将崩溃...

提前致谢..

2 个答案:

答案 0 :(得分:1)

您无法使用Audio r获取UIImagePickerControlle个文件,而应使用MPMediaPickerController

MPMediaPickerController *pickerController = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];
pickerController.prompt = @"Select Song";
pickerController.delegate = self;
[self presentViewController:pickerController animated:YES completion:nil];

选择后,您将在MpMediaPicker委托方法中收到回电:

- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
    MPMediaItem *theChosenSong = [[mediaItemCollection items]objectAtIndex:0];
    NSString *songTitle = [theChosenSong valueForProperty:MPMediaItemPropertyTitle];
    NSURL *assetURL = [theChosenSong valueForProperty:MPMediaItemPropertyAssetURL];
    AVURLAsset  *songAsset  = [AVURLAsset URLAssetWithURL:assetURL options:nil];
}

答案 1 :(得分:-1)

无法从库中添加或获取mp3文件。您只能从照片库中获取图像和视频。 kUTTypeMovie是包含视频和音频的媒体格式的抽象类型标识符。