itunes 歌曲导出 和上传

本文介绍了一段代码实现从iPhone设备中批量读取本地音乐文件,并使用AVFoundation框架将其导出为M4A格式,然后将导出的音频文件上传至服务器的过程。代码通过设置过滤条件来获取所有音乐文件,使用AVURLAsset进行资源加载,并选择合适的导出预设生成音频文件。最后,代码处理输出路径,确保文件不重复上传。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

app要求把iPhone本地歌曲导出到项目上传到服务器 

   源代码 :

 MPMediaQuery *everything = [[MPMediaQuery alloc] init];

    // 读取条件

    MPMediaPropertyPredicate *albumNamePredicate =

    [MPMediaPropertyPredicate predicateWithValue:[NSNumber numberWithInt:MPMediaTypeMusic ] forProperty: MPMediaItemPropertyMediaType];

    [everything addFilterPredicate:albumNamePredicate];

    

    NSLog(@"Logging items from a generic query...");

    

    NSArray *itemsFromGenericQuery = [everything items];

    

    

    

    for (MPMediaItem *song in itemsFromGenericQuery) {


}


 AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:url options:nil];

            

            

            

            

            

            NSLog (@"compatible presets for songAsset: %@",[AVAssetExportSession exportPresetsCompatibleWithAsset:songAsset]);

            

            NSArray *ar = [AVAssetExportSession exportPresetsCompatibleWithAsset: songAsset];

            

            NSLog(@"ar==%@", ar);

            

            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",name];

            

            

            

            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");

                         

                         

                         

                         

                         

                         

                         

                         

                         [self uploadMusic:exportFile]; //这里上传

                         

                         

                         

                         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;

                     }

                 }

                 

             }];

            

        }else{

            

            

   

        }


    }

        

        

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值