Weird bug with AVCaptureSession

不羁岁月 提交于 2019-12-06 07:25:49

I think I've figured it out:

The AVCaptureMovieFileOutput class has a movieFragmentInterval variable which defaults to 10. So every 10 seconds, a "sample table" is written to the quicktime file. Without a sample table the file is unreadable.

When I tested this all of my recordings were pretty short, so it seems like the bug happened whenever a sample table wasnt written out to the file. That's weird because I assumed when i call [movieOutput stopRecording] that this would write out the sample table, but I guess it didnt. When I lower the fragmentInterval to 5 seconds with:

CMTime fragmentInterval = CMTimeMake(5,1);

[movieOutput setMovieFragmentInterval:fragmentInterval];
[movieOutput startRecordingToOutputFileURL:pathUrl recordingDelegate:self];

The bug seems to have dissapeared. Im still unsure why the error only happened whenever the mic was added as an input device though.

Call this before recording,

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil];

and this before playback,

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

I think it should help considering you do not experience any issues unless trying to record audio.

Since you are using the same URL every time, you should delete it first

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *pathString = [documentsDirectory stringByAppendingPathComponent:@"movie.mov"];
NSURL *pathUrl = [NSURL fileURLWithPath:pathString];
[[NSFileManager defaultManager] removeItemAtURL:pathUrl error:nil];
[movieOutput startRecordingToOutputFileURL:pathUrl recordingDelegate:self];
sen abraham

You should set the fragmentInterval property of AVCaptureMovieFileOutput to a a higher limit. By default it is 10 sec.

I also had the same issue of my video recording using AVCaptureSession anything above 10 sec was not getting audio recorded. When i set the fragment interval to 300 sec which is the maximum second I allow to take video , and it recorded with audio.

AVCaptureMovieFileOutput *aMovieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
CMTime fragmentInterval = CMTimeMake(300,1);

[aMovieFileOutput setMovieFragmentInterval:fragmentInterval];

Its a repeat answer, but adding this to help someone starting out in ios like me, I tried KCMTimeInvalid but it just wasn't working for me. One frazzled hour later, I realised that I am setting movieFragmentInterval after calling startRecordingToOutputFileURL.

So for newbies who type blindly like me - keep the logical and very obvious sequence in mind

videoFileOutput.movieFragmentInterval = kCMTimeInvalid
videoFileOutput.startRecordingToOutputFileURL(filePath, recordingDelegate: recordingDelegate)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!