+ exportSessionWithAsset:presetName: 在 iOS7 上返回 nil
Posted
技术标签:
【中文标题】+ exportSessionWithAsset:presetName: 在 iOS7 上返回 nil【英文标题】:+ exportSessionWithAsset:presetName: returning nil on iOS7 【发布时间】:2015-06-06 04:44:57 【问题描述】:我正在使用以下代码:
- (AVAssetExportSession *)testRecording
AVMutableComposition *composition = [[AVMutableComposition alloc] init];
NSURL *url = [[NSBundle mainBundle] URLForResource:kTestSongName withExtension:@"mp3"];
AVAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
CMTimeRange range = CMTimeRangeMake(CMTimeMake(0, 600), asset.duration);
NSError *editError;
BOOL result = [composition insertTimeRange:range ofAsset:asset atTime:kCMTimeZero error:&editError];
if (!result)
NSLog(@"Error inserting timerange");
AVAssetExportSession *exportSession = [AVAssetExportSession
exportSessionWithAsset:composition
presetName:AVAssetExportPresetAppleM4A];
return exportSession;
这在 ios8 上完成了它应该做的事情,但在 iOS7 上 exportSession 在方法结束时总是为零。我尝试使用不同格式的资产(mp3、caf),但结果是一样的。
如果我播放乐曲而不是导出它,声音很好:
AVPlayerItem * item = [[AVPlayerItem alloc] initWithAsset:composition];
AVPlayer * player = [AVPlayer playerWithPlayerItem:item];
[player play];
你知道什么地方出了问题吗?
【问题讨论】:
【参考方案1】: exportSession = [[AVAssetExportSession alloc]
initWithAsset:songAsset
presetName:AVAssetExportPresetAppleM4A];
exportSession.outputFileType = @"com.apple.m4a-audio";
filePath = [[docDir stringByAppendingPathComponent:fileName] stringByAppendingPathExtension:@"m4a.mov"]
exportSession.outputURL = [NSURL fileURLWithPath:filePath];
[exportSession exportAsynchronouslyWithCompletionHandler:^
if (exportSession.status == AVAssetExportSessionStatusCompleted)
NSString *newPath = [NSString stringWithFormat:@"%@/%@", documentDirectory, [[[filePath lastPathComponent] substringToIndex:[[filePath lastPathComponent] length]-5] stringByAppendingString:@"a"]];
[[NSFileManager defaultManager] removeItemAtPath:newPath error:nil];
NSError *error = nil;
[[NSFileManager defaultManager] moveItemAtPath:filePath toPath:newPath error:&error];
if (exportSession.status == AVAssetExportSessionStatusCancelled)
if (exportSession.status == AVAssetExportSessionStatusExporting)
if (exportSession.status == AVAssetExportSessionStatusFailed)
NSLog(@"erros:%@",exportSession.error);
if (exportSession.status == AVAssetExportSessionStatusUnknown)
if (exportSession.status == AVAssetExportSessionStatusWaiting)
];
如果要保存为 mp3 格式,请使用ffmpeg。
【讨论】:
以上是关于+ exportSessionWithAsset:presetName: 在 iOS7 上返回 nil的主要内容,如果未能解决你的问题,请参考以下文章