0

I'm exporting audio.caf audio file with video.mp4 video and creating a new output.mp4 composition. It works fine with iOS 5.0, but with an iPhone4 4.2.1, the composition doesn't play the audio.

NSURL *outputURL = [NSURL fileURLWithPath:outputPath];
AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetMediumQuality];
_assetExport.outputFileType = AVFileTypeQuickTimeMovie;
_assetExport.outputURL = outputURL;
_assetExport.timeRange = CMTimeRangeMake(CMTimeMakeWithSeconds(0.25, 600), videoAsset.duration);
_assetExport.shouldOptimizeForNetworkUse = YES;

[_assetExport exportAsynchronouslyWithCompletionHandler:^{

I've checked the original tracks in this iphone and both are ok, only the composition doesn't add the audio.

This is how I'm adding the audio track to the composition:

NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audioURL options:nil];
CMTimeRange audio_timeRange = CMTimeRangeMake(CMTimeMake(startRecording_,1), 
                                              audioAsset.duration);

AVMutableCompositionTrack *b_compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

[b_compositionAudioTrack insertTimeRange:audio_timeRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:&error];

It worked in many iOS5 devices :S

Thanks in advance!

nano
  • 2,511
  • 4
  • 25
  • 42
  • Not related to the question, but may I ask why the `timeRange`was set to start at `0.25` and not `kCMTimeZero`? Is there a special reason for this? – John Estropia May 30 '12 at 05:08
  • 1
    Yes, I wanted to upload that video to Facebook and the preview there was a black screen. For avoiding that and see "something" as a preview I forced to start the video 1/4 seconds later so the preview in Facebook is not black, but a real frame of the video. – nano Jun 04 '12 at 15:13

2 Answers2

0

I changed audio to .m4a audio and it's working now!

nano
  • 2,511
  • 4
  • 25
  • 42
0

I found that I needed to change the file extension to 'mov' then do my export (im using PassThrough preset), then change it back to mp3 (in my case). Seems like a bug....

Dermot
  • 1,713
  • 2
  • 18
  • 29