2

While recording a video using AVFoundation's - (void)startRecordingToOutputFileURL:(NSURL*)outputFileURL recordingDelegate:(id<AVCaptureFileOutputRecordingDelegate>)delegate; method, if the video duration is more than 12 seconds, there is no audio track in the output file. Everything works fine, if the video duration is less than 12 seconds...

Delegate in which the output file URL is received is:

- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error
{   
    NSLog(@"AUDIO %@", [[AVAsset assetWithURL:outputFileURL] tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]); //App crashes here...

    NSLog(@"VIDEO %@", [[AVAsset assetWithURL:outputFileURL] tracksWithMediaType:AVMediaTypeVideo]);
}

My app crashes for a video that is longer than 12 seconds with this error: *** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'

nr5
  • 4,228
  • 8
  • 42
  • 82
  • And would you like to show what you're _doing_ in that delegate method? That could be important... – matt Nov 28 '14 at 04:22
  • Well _that's_ new information. Your original question never mentioned a crash, now did it? Please show what line the crash occurs on, and the crash log. – matt Nov 28 '14 at 04:46
  • Yes I have added the crash info. and a comment on the NSLog line where the app crashes. – nr5 Nov 28 '14 at 05:08
  • What happens if you check the `error` first? – matt Nov 28 '14 at 05:17
  • The whole code is in between the if (! error) condition. I didn't mention it here to avoid the lengthy code... – nr5 Nov 28 '14 at 05:19
  • I'm going to guess that the problem is that the recording takes place on a background thread, so you're encountering a threading problem. All the sample code (e.g. AVCam) has you calling `writeVideoAtPathToSavedPhotosAlbum:` and not doing anything else until the completion block of that call. So maybe you're just checking the output file too soon. – matt Nov 28 '14 at 05:28
  • From Apple, about the didFinishRecording... delegate: This method is called when the file output has finished writing all data to a file whose recording was stopped, either because startRecordingToOutputFileURL:recordingDelegate: or stopRecording were called, or because an error (described by the error parameter), occurred (if no error occurred, the error parameter is nil). This method is always called for each recording request, even if no data is successfully written to the file. – nr5 Nov 28 '14 at 05:31
  • But what I'm suggesting is that it isn't actually finished. Does it help if you call `stopRunning` on the session? Oh, also, make sure your AVAudioSession is okay. – matt Nov 28 '14 at 05:56
  • Well, I am calling the stopRecording method of AVCaptureMovieFileOutput which in turns calls the didFinishRecording... delegate and in the delegate I call the stopRunning method of AVCaptureSession. – nr5 Nov 28 '14 at 06:08
  • I have a new idea. I found some code that captures the video in 10-minute segments. Now, why would someone want to do that? It makes me wonder whether you've simply hit some sort of built-in limit...? – matt Nov 28 '14 at 18:34

3 Answers3

5

My guess is that AVCaptureMovieFileOutput has better support for QuickTime containers (.qt, .mov) than for mp4 although it is the industry standard. For instance when writing a movie file in fragments to an .mp4, something probably happens to the fragment table (sample table).

So you could either change the file format to .mov or turn of writing the file in fragments. See this question: ios-8-ipad-avcapturemoviefileoutput-drops-loses-never-gets-audio-track-after

Community
  • 1
  • 1
Henrik Lineholm
  • 356
  • 1
  • 6
  • 9
3

Spent almost 1 day to solve this & This is the perfect solution for this...

After a lot got help from iOS 8 iPad AVCaptureMovieFileOutput drops / loses / never gets audio track after 13 - 14 seconds of recording ...

Just add this line

avCaptureMovieFileOutput.movieFragmentInterval = kCMTimeInvalid
Ahtazaz
  • 903
  • 8
  • 21
0

Just changed the extension of the path to which the video is being recorded to mov from mp4 and it worked...

nr5
  • 4,228
  • 8
  • 42
  • 82