我使用了这个代码这很好,但是转换成数据后,当我试图将转换后的数据发送到MFMailComposeViewController或MFMessageComposeViewController时,它有时会以文档格式出现,但未能获得mp3格式。当我播放我们发送给MFmailComposeViewcontroller或MFMessageComposeViewController的歌曲时,正确转换的歌曲正在正确播放,但有些正在转换为文档格式。如何克服这一点。请推荐我。
SelectedAlbumUrl = [[arr_tablVArray] objectAtIndex:indexPath。行]objectForKey: @"SongUrl");
这个方法像这样调用。
[self mediaItemToData:[arr_tablVArray objectAtIndex:indexPath.row]];
-(void)mediaItemToData : (MPMediaItem * ) curItem
{
NSURL *url = SelectedAlbumUrl;
AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL: url options:nil];
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset: songAsset
presetName:AVAssetExportPresetAppleM4A];
exporter.outputFileType = @"com.apple.m4a-audio";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * myDocumentsDirectory = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
[[NSDate date] timeIntervalSince1970];
NSTimeInterval seconds = [[NSDate date] timeIntervalSince1970];
NSString *intervalSeconds = [NSString stringWithFormat:@"%0.0f",seconds];
NSString * fileName = [NSString stringWithFormat:@"%@.caf",intervalSeconds];
NSLog(@"the file name is --->%@",fileName);
NSString *exportFile = [myDocumentsDirectory stringByAppendingPathComponent:fileName];
exportURL = [NSURL fileURLWithPath:exportFile];
exporter.outputURL = exportURL;
// (completion handler block omitted)
[exporter exportAsynchronouslyWithCompletionHandler:
^{
int exportStatus = exporter.status;
switch (exportStatus)
{
case AVAssetExportSessionStatusFailed:
{
NSError *exportError = exporter.error;
NSLog (@"AVAssetExportSessionStatusFailed: %@", exportError);
break;
}
case AVAssetExportSessionStatusCompleted:
{
NSLog (@"AVAssetExportSessionStatusCompleted");
NSData *data = [NSData dataWithContentsOfFile: [myDocumentsDirectory
stringByAppendingPathComponent:fileName]];
data = nil;
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;
}
}
}];
}
After that url converting to NSData sending through email,message and airdrop like this
NSData *Data = [NSData dataWithContentsOfURL:exportURL];
[composer addAttachmentData:Data mimeType:@".caf" fileName:str_songtitle];
试试这个
- (void)addAttachmentData:(NSData*)attachment mimeType:(NSString*)mimeType fileName:(NSString*)filename
with mimeType = audio/mpeg