18

Uygulamamı bir AVCaptureSession kullanarak kameradan video kaydetmek için ayarlamam var, ancak, onunla ses yok. Sesi kaydetmek ve dosya için videoOutput'a eklemek için ne yapmam gerekir? İşte video kaydı benim kodudur:AVCaptureSession Sesli Video Kaydetme

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]; 

Ben ses için başka girişi eklendi ama alışkanlık arka planda MPMoviePlayerController ile çalışır. Bir video oynayabilecek ve aynı anda kameradan ses ve video kaydı yapabilecek bir şey var mı? beginConfiguration ve commitConfiguration arasında

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

:

+0

@MDT arasındaki kodunun altına ekle? Bir bağlantı göndermek için zaman alacaksan, neden bana yardım edeceğini düşündüğün şeyin bir bağlantısı yapmasın? – user717452

+0

Herhangi bir sebepten dolayı – user717452

cevap

28

Ses cihazını dahil etmedik. Çalışacak!

+1

'u eklemek için son paragrafı ekledim, AudioInput eklediğimde ve kaydetmeye başladığımda önizleme donuyor ve video çıkış dosyası kaydedilen çekimlerin 0 sn'ını bildirmeye devam ediyor ... ses yorumunu yaptığım anda, tekrar çalışmaya başlıyor :( – Moonwalker

7

ben yapacağım ne öylesine beginConfiguration() ve 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

Soru, ObjectiveC'nin Swift – NSNoob

+2

değil, bugün için güncellenmiş bir cevabı var. –

İlgili konular