首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法用AVMutableComposition导出AVAssetExportSession

无法用AVMutableComposition导出AVAssetExportSession
EN

Stack Overflow用户
提问于 2018-07-23 13:46:35
回答 1查看 228关注 0票数 1

这里是构图创作的一部分:

代码语言:javascript
复制
  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“.

代码语言:javascript
复制
- (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);
    }
  }];

}

我所犯的错误:

错误Domain=AVFoundationErrorDomain代码=-11800“操作无法完成”UserInfo={NSLocalizedFailureReason=An未知错误发生(-12780),NSLocalizedDescription=The操作无法完成,NSUnderlyingError=0x1c0a409f0 {Error Domain=NSOSStatusErrorDomain代码=-12780 "(null)"}}

EN

回答 1

Stack Overflow用户

发布于 2019-05-30 07:27:29

您使用的路径必须包含需要导出的文件的内容类型,如下所示:

代码语言:javascript
复制
    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)

希望能帮上忙!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51480367

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档