Note that there are some explanatory texts on larger screens.

plurals
  1. POConcatenating sound files on the iPhone (m4a) fails
    text
    copied!<p>I am trying to make one long sound file by concatenating several smaller sound files - all are in the m4a format, and the result should also be in the m4a format.</p> <p>Here is the code (the audFiles array holds the names of the sound files to join). Note I only print the file sizes for peace of mind...</p> <pre><code>CMTime nextClipStartTime = kCMTimeZero; AVMutableComposition *combinedSounds = [AVMutableComposition composition]; NSString *tempDir = NSTemporaryDirectory(); NSArray *audFiles; for (int i = 0; i &lt; [audFiles count]; i++) { NSString *addSound = [tempDir stringByAppendingString:audFiles[i]]; if ([[NSFileManager defaultManager] fileExistsAtPath:addSound] == YES) { NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:addSound error:nil]; if (fileAttributes != nil) { NSString *fileSize = [fileAttributes objectForKey:NSFileSize]; NSLog(@"file %@ %@", addSound, fileSize); NSURL *assetURL = [[NSURL alloc] initFileURLWithPath:addSound]; AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:assetURL options:nil]; if (asset != nil) { CMTimeRange timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration); NSLog(@"asset %d length %lld", i, asset.duration.value); if (asset.duration.value &gt; 0) { AVAssetTrack *audTrack = [asset tracksWithMediaType:AVMediaTypeAudio][0]; AVMutableCompositionTrack *audioTrack = [combinedSounds addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; if ([audioTrack insertTimeRange:timeRange ofTrack:audTrack atTime:nextClipStartTime error:nil] == NO) { NSLog(@"insertTimeRange %d FAILED", i); } nextClipStartTime = CMTimeAdd(nextClipStartTime, asset.duration); nextClipStartTime = CMTimeAdd(nextClipStartTime, CMTimeMake(0.1, 1)); NSLog(@"nextClipStartTime %lld", nextClipStartTime.value); } } } } } NSString *finalSound = [tempDir stringByAppendingString:@"result.m4a"]; AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:combinedSounds presetName:AVAssetExportPresetPassthrough]; NSString *exported = [tempDir stringByAppendingString:finalSound]; [[NSFileManager defaultManager] removeItemAtPath:exported error:nil]; NSURL *exportedURL = [[NSURL alloc] initFileURLWithPath:exported]; exportSession.outputURL = exportedURL; exportSession.shouldOptimizeForNetworkUse = YES; exportSession.outputFileType = AVFileTypeAppleM4A; [exportSession exportAsynchronouslyWithCompletionHandler:^{ switch (exportSession.status) { case AVAssetExportSessionStatusFailed: { NSLog(@"exportSession FAIL"); break; } case AVAssetExportSessionStatusCompleted: { NSLog(@"exportSession SUCCESS"); } } }]; </code></pre> <p>"exportSession SUCCESS" is reported, and the exported file exists and can be played, but it only contains the first of the constituent files.</p> <p>Any ideas what I am doing wrong ?</p> <p>Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload