3

いくつかの小さなサウンドファイルを連結して1つの長いサウンドファイルを作成しようとしています。すべてm4a形式であり、結果もm4a形式である必要があります。

コードは次のとおりです(audFiles配列は、結合するサウンドファイルの名前を保持します)。安心のためにファイルサイズのみを印刷していることに注意してください...

CMTime nextClipStartTime = kCMTimeZero;
AVMutableComposition *combinedSounds = [AVMutableComposition composition];
NSString *tempDir = NSTemporaryDirectory();
NSArray *audFiles;

for (int i = 0; i < [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 > 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");
        }
    }
}];

「exportSessionSUCCESS」が報告され、エクスポートされたファイルが存在して再生できますが、構成ファイルの最初のもののみが含まれています。

私が間違っていることについて何か考えはありますか?

ありがとう。

4

1 に答える 1