IOS 8 iPad AVCaptureMovieFileOutput падает/теряет/никогда не получает звуковую дорожку после 13-14 секунд записи
У меня есть следующий код, который работает для iOS 6 и 7.x.
В iOS 8.1 у меня странная проблема: если вы захватили сеанс в течение примерно 13 секунд или дольше, у полученного AVAsset только 1 трек (видео), звуковая дорожка просто отсутствует.
Если вы записываете на более короткий период времени, AVAsset имеет 2 дорожки (видео и аудио), как ожидалось. У меня много места на диске, у приложения есть разрешение на использование камеры и микрофона.
Я создал новый проект с минимальным кодом, он воспроизвел проблему.
Будем очень благодарны за любые идеи.
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
{
enum RecordingState { Recording, Stopped };
enum RecordingState recordingState;
AVCaptureSession *session;
AVCaptureMovieFileOutput *output;
AVPlayer *player;
AVPlayerLayer *playerLayer;
bool audioGranted;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self setupAV];
recordingState = Stopped;
}
-(void)setupAV
{
session = [[AVCaptureSession alloc] init];
[session beginConfiguration];
AVCaptureDevice *videoDevice = nil;
for ( AVCaptureDevice *device in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo] ) {
if ( device.position == AVCaptureDevicePositionBack ) {
videoDevice = device;
break;
}
}
AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
if (videoDevice && audioDevice)
{
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:nil];
[session addInput:input];
AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:nil];
[session addInput:audioInput];
NSURL *recordURL = [self tempUrlForRecording];
[[NSFileManager defaultManager] removeItemAtURL:recordURL error:nil];
output= [[AVCaptureMovieFileOutput alloc] init];
output.maxRecordedDuration = CMTimeMake(45, 1);
output.maxRecordedFileSize = 1028 * 1028 * 1000;
[session addOutput:output];
}
[session commitConfiguration];
}
- (IBAction)recordingButtonClicked:(id)sender {
if(recordingState == Stopped)
{
[self startRecording];
}
else
{
[self stopRecording];
}
}
-(void)startRecording
{
recordingState = Recording;
[session startRunning];
[output startRecordingToOutputFileURL:[self tempUrlForRecording] recordingDelegate:self];
}
-(void)stopRecording
{
recordingState = Stopped;
[output stopRecording];
[session stopRunning];
}
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error
{
AVAsset *cameraInput = [AVAsset assetWithURL:[self tempUrlForRecording]];
//DEPENDING ON HOW LONG RECORDED THIS DIFFERS (<14 SECS - 2 Tracks, >14 SECS - 1 Track)
NSLog(@"Number of tracks: %i", cameraInput.tracks.count);
}
-(id)tempUrlForRecording
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *path = @"camerabuffer.mp4";
NSString *pathCameraInput =[documentsDirectoryPath stringByAppendingPathComponent: path];
NSURL *urlCameraInput = [NSURL fileURLWithPath:pathCameraInput];
return urlCameraInput;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Ответы
Ответ 1
Это поможет вам исправить это.
[movieOutput setMovieFragmentInterval:kCMTimeInvalid];
Я думаю, что это ошибка. В документации указано, что таблица образцов не записывается, если запись не завершена успешно. Поэтому он будет автоматически записан, если он будет успешно завершен. Но теперь похоже, что это не так.
Любые идеи?