- (void)convertToMp3:(MPMediaItem *)mediaItem {
//get the name of the file.
NSString *songTitle = [mediaItem valueForProperty: MPMediaItemPropertyTitle];
//convert MPMediaItem to AVURLAsset.
AVURLAsset *sset = [AVURLAsset assetWithURL:[mediaItem valueForProperty:MPMediaItemPropertyAssetURL]];
//get the extension of the file.
NSString *fileType = [[[[sset.URL absoluteString] componentsSeparatedByString:@"?"] objectAtIndex:0] pathExtension];
//init export, here you must set "presentName" argument to "AVAssetExportPresetPassthrough". If not, you will can't export mp3 correct.
AVAssetExportSession *export = [[AVAssetExportSession alloc] initWithAsset:sset presetName:AVAssetExportPresetPassthrough];
NSLog(@"export.supportedFileTypes : %@",export.supportedFileTypes);
//export to mov format.
export.outputFileType = @"com.apple.quicktime-movie";
export.shouldOptimizeForNetworkUse = YES;
NSString *extension = [export.outputFileType pathExtension];
NSString *path = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/%@.%@",songTitle, extension];
NSURL *outputURL = [NSURL fileURLWithPath:path];
export.outputURL = outputURL;
[export exportAsynchronouslyWithCompletionHandler:^{
if (export.status == AVAssetExportSessionStatusCompleted){
//then rename mov format to the original format.
NSFileManager *manage = [NSFileManager defaultManager];
NSString *mp3Path = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/%@.%@",songTitle,fileType];
NSError *error = nil;
[manage moveItemAtPath:path toPath:mp3Path error:&error];
NSLog(@"error %@",error);
}else{
NSLog(@"%@",export.error);
}
}];
}
本地音乐库导出MP3
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 今天在做一款音乐播放器的时候需要支持文件共享本地导入音乐,但是导入进去的mp3是只有歌曲名字加格式的,有些甚至歌曲...
- 原文1:http://www.cnblogs.com/wangmingshun/p/5424767.html 原文...
- git代码库回滚: 指的是将代码库某分支退回到以前的某个commit id 【本地代码库回滚】: git rese...
- 之前有个要选取本地音乐混音的功能,要读取本地库的音乐,发现取到的路径是系统处理过的路径,不能直接用, 在网上找了一...