无法使用 AVAssetExportSession 导出 AVMutableComposition
Posted
技术标签:
【中文标题】无法使用 AVAssetExportSession 导出 AVMutableComposition【英文标题】:Can not export AVMutableComposition with AVAssetExportSession 【发布时间】:2018-07-23 13:46:35 【问题描述】:这是作曲创作的一部分:
AVMutableComposition* mixComposition = [[AVMutableComposition alloc] init];
for (NSDictionary *track in tracks)
NSURL *url = [[NSURL alloc] initWithString:track[@"url"]];
AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:url options:nil];
AVMutableCompositionTrack *firstTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio
preferredTrackID:kCMPersistentTrackID_Invalid];
[firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, urlAsset.duration)
ofTrack:[[urlAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
atTime:kCMTimeZero
error:nil];
[self persist:mixComposition for:songId];
然后我希望将集合保存在目录中,这样我就不必每次都下载它
合成的输出如下所示:
"AVMutableCompositionTrack: 0x1c4a276a0 trackID = 1, mediaType = soun, editCount = 1", "AVMutableCompositionTrack: 0x1c4a28560 trackID = 2, mediaType = soun, editCount = 1"...,
- (void)persist:(AVMutableComposition *) composition
for:(NSString *) songId
NSLog(@"%@", composition);
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]
initWithAsset:composition
presetName:AVAssetExportPresetAppleM4A];
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent: songId];
NSURL *url = [NSURL fileURLWithPath: path];
exportSession.outputURL = url;
exportSession.shouldOptimizeForNetworkUse = YES;
exportSession.outputFileType = AVFileTypeAppleM4A;
// perform the export
[exportSession exportAsynchronouslyWithCompletionHandler:^
if (AVAssetExportSessionStatusCompleted == exportSession.status)
NSLog(@"AVAssetExportSessionStatusCompleted");
NSLog(@"Path : %@", url);
else if (AVAssetExportSessionStatusFailed == exportSession.status)
// a failure may happen because of an event out of your control
// for example, an interruption like a phone call comming in
// make sure and handle this case appropriately
NSLog(@"%@", exportSession.error);
else
NSLog(@"Export Session Status: %ld", (long)exportSession.status);
];
我得到的错误:
Error Domain=AVFoundationErrorDomain Code=-11800 "该操作可能 未完成” UserInfo=NSLocalizedFailureReason=未知错误 发生 (-12780), NSLocalizedDescription=操作不能 完成,NSUnderlyingError=0x1c0a409f0 错误 Domain=NSOSStatusErrorDomain Code=-12780 "(null)"
【问题讨论】:
【参考方案1】:您使用的路径必须是您需要导出的文件的内容类型,如下所示:
let directory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
let audioTypeOutput = ".m4a"//type of your file, mp3, m4a for audio
let exportPath = directory?.appendingPathComponent(name + audioTypeOutput)
希望对您有所帮助!
【讨论】:
以上是关于无法使用 AVAssetExportSession 导出 AVMutableComposition的主要内容,如果未能解决你的问题,请参考以下文章
无法使用 AVAssetExportSession 修剪音频文件
AVAssetExportSession 无法创建文件错误 -12115
使用 AVAssetExportSession 全屏导出视频
AVAssetExportSession AVFoundationErrorDomain Code -11800 操作无法完成,NSOSStatusErrorDomain Code=-12780 &q