18

Mam ustawioną aplikację do nagrywania wideo z kamery przy użyciu AVCaptureSession, jednak nie ma z nią dźwięku. Co muszę zrobić, aby nagrać dźwięk, a następnie dodać go do wideoOutput dla pliku? Tu jest mój kodu do nagrywania wideo:AVCaptureSession Record Video With Audio

AVCaptureSession *session = [[AVCaptureSession alloc] init]; 
[session beginConfiguration]; 
session.sessionPreset = AVCaptureSessionPresetMedium; 

CALayer *viewLayer = self.vImagePreview.layer; 
NSLog(@"viewLayer = %@", viewLayer); 

AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session]; 
captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 
captureVideoPreviewLayer.frame = self.vImagePreview.bounds; 

[self.vImagePreview.layer addSublayer:captureVideoPreviewLayer]; 

AVCaptureDevice *device = [self frontFacingCameraIfAvailable]; 

NSError *error = nil; 
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error]; 
if (!input) { 
    // Handle the error appropriately. 
    NSLog(@"ERROR: trying to open camera: %@", error); 
} 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectoryPath = [paths objectAtIndex:0]; 

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

NSString *archives = [documentsDirectoryPath stringByAppendingPathComponent:@"archives"]; 
NSString *outputpathofmovie = [[archives stringByAppendingPathComponent:@"Test"] stringByAppendingString:@".mp4"]; 
NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputpathofmovie]; 

[session addInput:input]; 
[session addOutput:movieFileOutput]; 
[session commitConfiguration]; 
[session startRunning]; 
[movieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self]; 

Dodałem kolejne wejście do dźwięku, ale to przyzwyczajenie praca z mpmovieplayercontroller że jest w tle. Czy są jakieś przemyślenia dotyczące czegoś, co może odtwarzać jedno wideo i jednocześnie rejestrować dźwięk i obraz z kamery?

+0

@MDT więc co mam zrobić? Jeśli masz zamiar poświęcić trochę czasu, aby opublikować link, dlaczego nie zrobić link do tego, co myślisz, że mi pomoże? – user717452

+0

Z jakiegoś powodu dodałem ostatni akapit do edycji – user717452

Odpowiedz

28

Nie włączone urządzenie audio:

AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio]; 
AVCaptureDeviceInput * audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:nil]; 
[session addInput:audioInput] 

między beginConfiguration i commitConfiguration. To zadziała !!!

+1

, gdy dodam AudioInput i rozpocznę nagrywanie, podgląd się zawiesza, a plik wyjściowy wideo rejestruje 0 s nagranego materiału ... jak tylko skomentuję wyjście audio, zacznie działać ponownie :( – Moonwalker

7

Dodaj poniżej kodu między beginConfiguration() i commitConfiguration()

// Add audio device to the recording 

let audioDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeAudio) 
do { 
    let audioInput = try AVCaptureDeviceInput(device: audioDevice) 
    self.captureSession.addInput(audioInput) 
} catch { 
    print("Unable to add audio device to the recording.") 
} 
+0

Pytanie dotyczy obiektu ObjectiveC Not Swift – NSNoob

+2

, ale ma zaktualizowaną odpowiedź na dziś. –

Powiązane problemy