1.调用系统音乐库
MPMediaPickerController *mpc = [[MPMediaPickerController alloc]initWithMediaTypes:MPMediaTypeAnyAudio];
mpc.delegate = self;//委托
mpc.prompt =@"请选择音频文件";//提示文字
mpc.allowsPickingMultipleItems=NO;//是否允许一次选择多个
[self presentModalViewController:mpc animated:YES];
#pragma mark 音频代理方法
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection{
/*insert your code*/
for (MPMediaItem* item in [mediaItemCollection items]) {
//item里有你想要的音频信息
[self convertToMp3:item];//缓存到沙河路径
}
[self dismissModalViewControllerAnimated:YES];
}
3.缓存沙河路径并上传服务器
#pragma mark 存储文件到沙河
- (void) convertToMp3: (MPMediaItem *)song
{
_hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
_hud.mode = MBProgressHUDModeText;
_hud.labelText = @"上传中";
NSURL *url = [song valueForProperty:MPMediaItemPropertyAssetURL];
AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:url options:nil];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *dirs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [dirs objectAtIndex:0];
NSLog (@"compatible presets for songAsset: %@",[AVAssetExportSession exportPresetsCompatibleWithAsset:songAsset]);
NSString * localpath = [documentsDirectoryPath stringByAppendingPathComponent:@"voice.mp3"];
AVAssetExportSession *exporter = [[AVAssetExportSession alloc]
initWithAsset: songAsset
presetName: AVAssetExportPresetAppleM4A];
NSLog (@"created exporter. supportedFileTypes: %@", exporter.supportedFileTypes);
exporter.outputFileType = @"com.apple.m4a-audio";
NSString *exportFile = [documentsDirectoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.m4a",@"voice"]];
NSError *error1;
if([fileManager fileExistsAtPath:exportFile])
{
[fileManager removeItemAtPath:exportFile error:&error1];
}
NSURL* exportURL = [NSURL fileURLWithPath:exportFile] ;
exporter.outputURL = exportURL;
// do the export
[exporter exportAsynchronouslyWithCompletionHandler:^
{
int exportStatus = exporter.status;
switch (exportStatus) {
case AVAssetExportSessionStatusFailed: {
// log error to text view
NSError *exportError = exporter.error;
NSLog (@"AVAssetExportSessionStatusFailed: %@", exportError);
break;
}
case AVAssetExportSessionStatusCompleted: {
NSLog (@"AVAssetExportSessionStatusCompleted");
//自己封装的网络请求
[[NetWorking shareNetWorking] UploadVoiceWithUrl:exportURL progress:^(NSProgress *progress) {
} success:^(id responseObject) {
NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
NSArray *urlArray = dict[@"urls"];
[_voiceUrlArray addObjectsFromArray:urlArray];
_hud.labelText = @"上传成功";
[_hud hide:YES afterDelay:1];
// [_animationView hide];
} failure:^(NSError *error) {
_hud.labelText = @"上传失败";
[_hud hide:YES afterDelay:1];
}];
break;
}
case AVAssetExportSessionStatusUnknown: {
NSLog (@"AVAssetExportSessionStatusUnknown");
break;
}
case AVAssetExportSessionStatusExporting: {
NSLog (@"AVAssetExportSessionStatusExporting");
break;
}
case AVAssetExportSessionStatusCancelled: {
NSLog (@"AVAssetExportSessionStatusCancelled");
break;
}
case AVAssetExportSessionStatusWaiting: {
NSLog (@"AVAssetExportSessionStatusWaiting");
break;
}
default:
{ NSLog (@"didn't get export status");
break;
}
}
}];
}