iphone - AVAssetExportSession-AVMutableCompositionTrack-导出的输出文件为空

标签 iphone audio avfoundation

我尝试将.mp3-文件添加到AVMutableCompositionTrack,然后再导出新文件。问题是:导出后,生成的文件存在,但为空,无法播放。有人在我的代码中看到错误了吗?

AVMutableComposition *saveComposition = [AVMutableComposition composition];
 NSArray *docpaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *tempPath = [docpaths objectAtIndex:0];

 NSLog(@"Temporary Path: %@", tempPath);

 NSString *audioPath = [[NSBundle mainBundle] pathForResource: @"1" ofType: @"mp3"];
 NSURL *audioUrl = [[NSURL alloc] initFileURLWithPath:audioPath];
 AVURLAsset *audio = [AVURLAsset URLAssetWithURL:audioUrl options:nil];
 NSLog(@"%@", audio);
 [audioUrl release];
 AVMutableCompositionTrack *compositionAudioTrack = [saveComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
 AVAssetTrack *clipAudioTrack = [[audio tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];

 [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [audio duration])  ofTrack:clipAudioTrack atTime:kCMTimeZero error:nil];

 NSString *path = [tempPath stringByAppendingPathComponent:@"mergedaudio.m4a"];
 if([[NSFileManager defaultManager] fileExistsAtPath:path])
 {
  [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
 }
 NSURL *url = [[NSURL alloc] initFileURLWithPath: path];

 AVAssetExportSession *exporter = [[[AVAssetExportSession alloc] initWithAsset:saveComposition presetName:AVAssetExportPresetAppleM4A] autorelease];
 exporter.outputURL=url;
 [exporter setOutputFileType:@"com.apple.m4a-audio"];

 NSLog(@"%@", [exporter supportedFileTypes]);
 exporter.outputFileType=[[exporter supportedFileTypes] objectAtIndex:0];

 [exporter exportAsynchronouslyWithCompletionHandler:^{

 }];

先感谢您 !

最佳答案

就像我在评论中写道,它与不同的文件格式有关。我将文件更改为.m4a以及代码-因此,所有内容(此操作的源和目标)都与.m4a相关,并且可以正常工作。

顺便说一句:我也尝试使用.wav文件,但是使用wav进行操作时会发生奇怪的事情。我不推荐。

关于iphone - AVAssetExportSession-AVMutableCompositionTrack-导出的输出文件为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4004887/

相关文章:

ios - 将音频与视频完美融合

iphone - 使用 AVPlayer 循环播放一小段视频,然后向后播放

iphone - 如何让同一个 Facebook 对象可用于我的 iOS Facebook SDK 项目中的所有代码?

ios - 从cordova html页面(cordova)加载保存在iOS App Documents目录中的图像?

iphone - HTML 到 PDF 页面的底部边距随着每一页的增加而变大

python - Python利用文件路径

c++ - Qt 无法在其他设备上播放声音

ios - 使用 UILocalNotifications、带有完成处理程序的远程通知和后台获取

java - 使用 TargetDataLine 从 Wine 中捕获声音

ios - 使用 AVAssetExportSession 导出时间流逝会导致黑色视频