18

Tôi đã cài đặt ứng dụng của mình để quay video từ máy ảnh bằng AVCaptureSession, tuy nhiên, không có âm thanh với nó. Tôi cần phải làm gì để ghi lại âm thanh và sau đó thêm nó vào videoOutput cho tệp? Đây là mã của tôi để quay video:AVCaptureSession Ghi lại Video Với Âm thanh

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

Tôi đã thêm đầu vào khác cho âm thanh, nhưng nó sẽ không hoạt động với mpmovieplayercontroller ở chế độ nền. Có bất kỳ suy nghĩ nào về một thứ có thể phát một video và đồng thời ghi lại âm thanh và video từ máy ảnh không?

+0

@MDT vì vậy những gì tôi phải làm gì? Nếu bạn định dành thời gian để đăng một liên kết, tại sao không làm cho nó một liên kết đến những gì bạn nghĩ sẽ giúp tôi? – user717452

+0

Xem thêm đoạn cuối để chỉnh sửa – user717452

Trả lời

28

Bạn chưa bao gồm các thiết bị âm thanh:

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

giữa beginConfigurationcommitConfiguration. Nó sẽ hoạt động !!!

+1

vì lý do nào đó, khi tôi thêm AudioInput và bắt đầu ghi, bản xem trước bị đóng băng và tệp đầu ra video sẽ báo cáo 0 giây quay cảnh ... ngay sau khi tôi nhận xét âm thanh, nó bắt đầu hoạt động trở lại :( – Moonwalker

7

Thêm bên dưới mã giữa beginConfiguration()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

Câu hỏi là về ObjectiveC không Swift – NSNoob

+2

nhưng anh ấy có câu trả lời cập nhật cho hôm nay. –

Các vấn đề liên quan