2015-06-24 24 views
5

Mam aplikację, która po załadowaniu widoku rozpoczyna przechwytywanie wideo i audio, a po jej zakończeniu zapisuje ją zarówno do folderu dokumentów aplikacji, jak i do Rolki z aparatu na iPadzie to działa. Upewniłem się i dodałem w danych wejściowych do sesji zarówno audio, jak i wideo, ale kiedy przejdę do obejrzenia zapisanego wideo, nie ma z nim dźwięku. Czy ktokolwiek może wykryć w moim kodzie coś, co wskazywałoby na problem?Nie nagrywanie dźwięku w AVCaptureSession

AKTUALIZACJA: Nie wyświetlają się żadne komunikaty o błędach. Jednak znalazłem wspólny mianownik. Dźwięk zostanie nagrany, ale tylko wtedy, gdy nagranie trwa 10 sekund lub krócej. Jeśli trafi 11 sekund, dźwięk nie zostanie nagrany.

NSLog pokazuje

Finished with error: (null)

-(void)viewWillAppear:(BOOL)animated { 
    NSDate *today = [NSDate date]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"MMM d hh:mm:ss a"]; 
    // display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings 
    NSString *currentTime = [dateFormatter stringFromDate:today]; 
    NSError* error4 = nil; 
    AVAudioSession* audioSession = [AVAudioSession sharedInstance]; 
    [audioSession setCategory:AVAudioSessionCategoryAmbient error:&error4]; 
    OSStatus propertySetError = 0; 
    UInt32 allowMixing = true; 
    propertySetError |= AudioSessionSetProperty(kAudioSessionProperty_OtherMixableAudioShouldDuck, sizeof(allowMixing), &allowMixing); 

    // Activate the audio session 
    error4 = nil; 
    if (![audioSession setActive:YES error:&error4]) { 
     NSLog(@"AVAudioSession setActive:YES failed: %@", [error4 localizedDescription]); 
    } 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectoryPath = [paths objectAtIndex:0]; 

    session = [[AVCaptureSession alloc] init]; 
    [session beginConfiguration]; 
    session.sessionPreset = AVCaptureSessionPresetMedium; 
    self.navigationController.navigationBarHidden = YES; 

    NSError *error = nil; 

    AVCaptureDevice *audioCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio]; 
    NSError *error2 = nil; 
    AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioCaptureDevice error:&error2]; 


    AVCaptureDevice *device; 
    AVCaptureDevicePosition desiredPosition = AVCaptureDevicePositionBack; 
    // find the front facing camera 

    device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 


    // get the input device 
    AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error]; 




    AVCaptureMovieFileOutput *movieFileOutput = [[AVCaptureMovieFileOutput alloc] init]; 

    NSString *archives = [documentsDirectoryPath stringByAppendingPathComponent:@"archives"]; 
    NSString *editedfilename = [[@"ComeOnDown" lastPathComponent] stringByDeletingPathExtension]; 
    NSString *datestring = [[editedfilename stringByAppendingString:@" "] stringByAppendingString:currentTime]; 
    NSLog(@"%@", datestring); 
    NSString *outputpathofmovie = [[archives stringByAppendingPathComponent:datestring] stringByAppendingString:@".mp4"]; 
    NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputpathofmovie]; 
    [session addInput:audioInput]; 
    [session addInput:deviceInput]; 
    [session addOutput:movieFileOutput]; 
    [session commitConfiguration]; 
    [session startRunning]; 




    AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session]; 
    previewLayer.backgroundColor = [[UIColor blackColor] CGColor]; 
    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 
    previewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight; 

    CALayer *rootLayer = [vImagePreview layer]; 
    [rootLayer setMasksToBounds:YES]; 
    [previewLayer setFrame:[rootLayer bounds]]; 
    [rootLayer addSublayer:previewLayer]; 

    [movieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self]; 



    //session = nil; 
    if (error) { 
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle: 
            [NSString stringWithFormat:@"Failed with error %d", (int)[error code]] 
                  message:[error localizedDescription] 
                  delegate:nil 
                cancelButtonTitle:@"Dismiss" 
                otherButtonTitles:nil]; 
     [alertView show]; 

    } 
    [super viewWillAppear:YES]; 

} 

-(void)captureOutput:(AVCaptureFileOutput *)captureOutput didStartRecordingToOutputFileAtURL:(NSURL *)fileURL fromConnections:(NSArray *)connections { 

} 
-(void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{ 
    NSLog(@"Finished with error: %@", error); 
} 
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error { 
    //finished 
    NSLog(@"Finished"); 
    NSString *proud = [[NSString alloc] initWithString:[outputFileURL path]]; 
    UISaveVideoAtPathToSavedPhotosAlbum(proud, self, @selector(video:didFinishSavingWithError: contextInfo:), (__bridge void *)(proud)); 
} 
+0

Naprawdę nie widzę potrzeby sarkazmu. Przypisujesz 'audioInput', ale nigdy nie sprawdzaj' error2'. Biorąc pod uwagę, że 'audioInput' jest tam, gdzie masz problem, nie jest to rozsądna sugestia? – jaket

+0

Brak komunikatów o błędach. Jednak znalazłem wspólny mianownik. Dźwięk zostanie nagrany, ale tylko wtedy, gdy nagranie trwa 10 sekund lub krócej. Jeśli trafi 11 sekund, dźwięk nie zostanie nagrany. – user717452

Odpowiedz

7

Odpowiedź jest movieFileOutput.movieFragmentInterval = kCMTimeInvalid; Podobno jest ona ustawiona domyślnie do 10, a wszystko po, dźwięk nie będzie nagrywać. Odwołanie od AVCaptureSession audio doesn't work for long videos

było łatwo znaleźć odpowiedź raz zorientowali się czas był związany z problemem

+0

dzięki za odpowiedź. ale to nie ma żadnego sensu. –

0
videoFileOutput.movieFragmentInterval = kCMTimeInvalid 

rozwiązać to dla mnie.

Przypadkowo ustawię movieFragmentInterval po wywołaniu startRecordingToOutputFileURL. Bolesna godzina później uświadomiłem sobie mój błąd. Dla początkujących takich jak ja - zauważ tę NIEDZIELNĄ SEKWENCJĘ.

videoFileOutput.movieFragmentInterval = kCMTimeInvalid 
videoFileOutput.startRecordingToOutputFileURL(filePath, recordingDelegate: recordingDelegate) 
Powiązane problemy