Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVAssetExportSession audio missing in iOS9

I have a code to export a video file. The code works fine on iOS8, but on devices with iOS9 there is no sound on the video file. I think the problem is on the AVAssetExportSession, but I'm unable to finde the issue and fix it. What is also weird, is the asset works fine when I export it for playback.

(void)export {
self.videoComposition.renderScale   = 1.0;
self.videoComposition.animationTool = [self coreAnimationTool];


FakeAsset * assetFaker = [FakeAsset new];
[assetFaker fakeAssetWithDuration:self.currentTime completitionBlock:^(AVAsset *asset) {
    [self addEmptyBackgroundTrackFromAsset:asset];
    AVAssetExportSession * exportSession    =
    [AVAssetExportSession exportSessionWithAsset:[self.composition copy]
                                      presetName:AVAssetExportPresetHighestQuality];
    [MovieExport setCurrentExporter:exportSession];


    NSUInteger i = 0;
    NSString * path;
    do {
        path  =
        [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"movieExport%i.mov",i]]; i++;
    } while ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:NULL]);

    NSURL * url = [NSURL fileURLWithPath:path];
    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
        [[NSFileManager defaultManager] removeItemAtURL:url error:nil];
    }
    exportSession.outputURL                 = url;
    exportSession.outputFileType            = AVFileTypeMPEG4;
    exportSession.audioMix                  = self.audioMix;
    exportSession.videoComposition          = self.videoComposition;
    exportSession.timeRange                 = CMTimeRangeMake(kCMTimeZero, self.currentTime);
    [exportSession exportAsynchronouslyWithCompletionHandler:^{
        switch (exportSession.status) {
            case AVAssetExportSessionStatusCompleted:
                self.callback(@{MovieCreatorVideoURL : exportSession.outputURL}, nil);
                break;
            case AVAssetExportSessionStatusFailed:
                self.callback(nil,exportSession.error);
                break;
            case AVAssetExportSessionStatusCancelled:
                self.callback(nil, exportSession.error);
                break;
            default:
                break;
        }
    }];
}];
}

- (void)exportForPlayback {
self.videoComposition.renderScale = 2.0;
FakeAsset * assetFaker = [FakeAsset new];
[assetFaker fakeAssetWithDuration:self.currentTime completitionBlock:^(AVAsset *asset){
    [self addEmptyBackgroundTrackFromAsset:asset];
    AVPlayerItem  * playerItem  = [AVPlayerItem  playerItemWithAsset:self.composition];
    playerItem.videoComposition = self.videoComposition;
    playerItem.audioMix         = self.audioMix;

    MovieMetadata * metaData   = [MovieMetadata new];
    metaData.orderedElements   = self.orderedElements;
    metaData.orderedMarkers    = self.movieMarkers;
    metaData.elementBeginTimes = self.elementBeginTimes;
    metaData.filmSize          = self.filmSize;
    metaData.filmFormat        = self.filmFormat;
    metaData.filmQuality       = self.filmQuality;

    self.userInfos =
    @{MovieCreatorAnimations     : self.animationLayer,
      MovieCreatorAVPlayerItem   : playerItem,
      MovieCreatorMetadata       : metaData};
    self.callback(self.userInfos, nil);
}];

}

like image 538
jfredsilva Avatar asked Oct 20 '22 01:10

jfredsilva


1 Answers

I found the issue. My AVMutableComposition have multiple AVMutableCompositionTrack. If one AVMutableCompositionTrack don't have a track, the video exported will be mute on iOS9 but will work correctly on iOS8

To fix this issue, when I initialize the AVMutableCompositionTrack, I add audio with no sound.

-(void)initEmptyAudio:(AVMutableCompositionTrack *) obj{
NSString* path = [[NSBundle mainBundle] pathForResource:@"emptyAudio" ofType:@"mp3"];
NSURL*    audio_inputFileUrl = [NSURL fileURLWithPath:path];
AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audio_inputFileUrl options:nil];
CMTimeRange audio_timeRange = CMTimeRangeMake(kCMTimeZero, audioAsset.duration);

[obj insertTimeRange:audio_timeRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:CMTimeMake(0,1) error:nil];
}
like image 138
jfredsilva Avatar answered Oct 22 '22 21:10

jfredsilva