AVMutableCompositionTrack - insertTimeRange - insertEmptyTimeRange issue

I have a strange problem: I want to create a new sound file from two sound files and silence.

sound1: 2 seconds + silence: silence in 2 seconds + sound2: 2 seconds

When I try the code below, I get a sound file lasting 6 seconds with all parts, but in a different order! Order: sound1, sound2, silence

I can’t put this silence in the middle of this composition (also not at the beginning). Is this typical behavior or am I doing something wrong?

Here is the code for merging segments:

[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [audio1 duration])  ofTrack:clipAudioTrack1 atTime:kCMTimeZero error:nil];
[compositionAudioTrack insertEmptyTimeRange:CMTimeRangeMake(kCMTimeZero, CMTimeMake(2, 1))];
[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [audio2 duration])  ofTrack:clipAudioTrack2 atTime:CMTimeMake(4, 1) error:nil];

Maybe someone has an idea? Thank you in advance!

By the way: the following code without insertEmptyTimeRange-line also does not work, it just generates 4 seconds of sound and sound2 slides to the end of sound1:

    [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [audio1 duration])  ofTrack:clipAudioTrack1 atTime:kCMTimeZero error:nil];
[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [audio2 duration])  ofTrack:clipAudioTrack2 atTime:CMTimeMake(4, 1) error:nil];

It seems that it is not allowed that between the segments there is “nothing” ??

+5
3

- , "insertEmptyTimeRange". "" - , . , . ;) ( - "insertEmptyTimeRange", ...)

+1

:

[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, CMTimeMake(2,1))
                               ofTrack:audioTrack
                                atTime:kCMTimeZero
                                error:nil];
[compositionAudioTrack insertEmptyTimeRange:CMTimeRangeMake(CMTimeMake(2, 1), CMTimeMake(4, 1))];
[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, CMTimeMake(2,1))  
                               ofTrack:audioTrack 
                                atTime:CMTimeMake(4, 1)
                                 error:nil];

" " (2,4), (0,2) (4,6).

+1

, ,

insertEmptyTimeRange: if you insert an empty track into a track, any media that was presented during this interval before insertion will be presented immediately afterwards.

So, all you have to do is first add both clips, and then add a time range of 2 times. This should work

insertTimeRange (0 to 2)
insertTimeRange (2 to 4)
insertEmptyTimeRange (2 to 4)

0
source

All Articles