- (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...
- 之前有個要選取本地音樂混音的功能,要讀取本地庫的音樂,發現取到的路徑是系統處理過的路徑,不能直接用, 在網上找了一...