Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 将音频与视频合并到Objective-C中_Iphone_Ios_Objective C_Avassetwriter - Fatal编程技术网

Iphone 将音频与视频合并到Objective-C中

Iphone 将音频与视频合并到Objective-C中,iphone,ios,objective-c,avassetwriter,Iphone,Ios,Objective C,Avassetwriter,我正在尝试使用以下命令将音频文件与视频文件合并: + (void)CompileFilesToMakeMovie:(NSString *) audioPath { NSLog(@"a"); AVMutableComposition* mixComposition = [AVMutableComposition composition]; NSURL *audio_inputFileUrl = [[NSURL alloc] initFileURLWithPath:audioPath]; NS

我正在尝试使用以下命令将音频文件与视频文件合并:

+ (void)CompileFilesToMakeMovie:(NSString *) audioPath {
NSLog(@"a");
AVMutableComposition* mixComposition = [AVMutableComposition composition];
NSURL *audio_inputFileUrl = [[NSURL alloc] initFileURLWithPath:audioPath];



NSString *videoPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"input" ofType:@"mov"];
NSURL*    video_inputFileUrl = [NSURL fileURLWithPath:videoPath];

NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
NSString *outputFilePath = [docsDir stringByAppendingPathComponent:@"OutputFile.mov"];
NSURL*    outputFileUrl = [NSURL fileURLWithPath:outputFilePath];

if ([[NSFileManager defaultManager] fileExistsAtPath:outputFilePath])
    [[NSFileManager defaultManager] removeItemAtPath:outputFilePath error:nil];
NSLog(@"b");


CMTime nextClipStartTime = kCMTimeZero;

AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:video_inputFileUrl options:nil];
CMTimeRange video_timeRange = CMTimeRangeMake(kCMTimeZero,videoAsset.duration);
AVMutableCompositionTrack *a_compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[a_compositionVideoTrack insertTimeRange:video_timeRange ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:nextClipStartTime error:nil];

 NSLog(@"c");
AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audio_inputFileUrl options:nil];
CMTimeRange audio_timeRange = CMTimeRangeMake(kCMTimeZero, audioAsset.duration);
AVMutableCompositionTrack *b_compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[b_compositionAudioTrack insertTimeRange:audio_timeRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:nextClipStartTime error:nil];
 NSLog(@"d");


AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
_assetExport.outputFileType = @"com.apple.quicktime-movie";
_assetExport.outputURL = outputFileUrl;

[_assetExport exportAsynchronouslyWithCompletionHandler:
 ^(void ) {
     UISaveVideoAtPathToSavedPhotosAlbum(outputFilePath, nil, nil, nil);
 }       
 ];


}
但在为“NSRangeException”记录“c”后,我收到一个错误,原因:

*-[\uu NSArrayM objectAtIndex:]:索引0超出空的界限 数组'

音频文件位于

  NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *docsDir = [dirPaths objectAtIndex:0];
  NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"%1.iaff"];
有什么建议吗?
谢谢。

首先我要说的是,您提供的音频格式不正确

NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"%1.iaff"];
使其aiff
NSString*soundFilePath=[docsDir stringByAppendingPathComponent:@“1.aiff”]

如果您不确定所使用的代码, 这个代码对我有用

    -(void) mergeAudio:(NSURL *) audioURL withVideo:(NSURL *) videoURL andSaveToPathUrl:(NSString *) savePath{

 AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audioURL options:nil];
 AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:videoURL options:nil];

 AVMutableComposition* mixComposition = [AVMutableComposition composition];

 AVMutableCompositionTrack *compositionCommentaryTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio
 preferredTrackID:kCMPersistentTrackID_Invalid];
 [compositionCommentaryTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration)
 ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
 atTime:kCMTimeZero error:nil];

 AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
 preferredTrackID:kCMPersistentTrackID_Invalid];
 [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration)
 ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
 atTime:kCMTimeZero error:nil];

 AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition
 presetName:AVAssetExportPresetPassthrough];

 NSURL    *savetUrl = [NSURL fileURLWithPath:savePath];

 if ([[NSFileManager defaultManager] fileExistsAtPath:savePath])
 {
 [[NSFileManager defaultManager] removeItemAtPath:savePath error:nil];
 }

 _assetExport.outputFileType = @"com.apple.quicktime-movie";


 _assetExport.outputURL = savetUrl;
 _assetExport.shouldOptimizeForNetworkUse = YES;

 [_assetExport exportAsynchronouslyWithCompletionHandler:
 ^(void ) {
 NSLog(@"fileSaved !");
 }
 ];


 }

传递最终文件的视频URL音频URL保存路径,这是因为音频或视频文件尚未完全写入,您正在尝试合并它们。确保使用完成处理程序来确保特定路径上的音频和视频文件已完全写入。

您的问题解决了吗。。。。我认为这段代码只适用于.mp3文件?而不是m4a?