iOS 将系统音乐库的文件缓存到沙河路径并上传服务器

1.调用系统音乐库

 MPMediaPickerController *mpc = [[MPMediaPickerController alloc]initWithMediaTypes:MPMediaTypeAnyAudio];

            mpc.delegate = self;//委托

            mpc.prompt =@"请选择音频文件";//提示文字

            mpc.allowsPickingMultipleItems=NO;//是否允许一次选择多个

            [self presentModalViewController:mpc animated:YES];


2.系统音乐库协议方法

#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;

             }

         }

         

     }];

}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值