23

我的应用程序设置为使用 AVCaptureSession 从相机录制视频,但是,它没有音频。我需要做什么来录制音频,然后将其添加到文件的 videoOutput 中?这是我录制视频的代码:

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

我为音频添加了另一个输入,但它不适用于后台的 mpmovieplayercontroller。有什么想法可以播放一个视频并同时从相机录制音频和视频吗?

4

3 回答 3

33

您尚未包含音频设备:

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

beginConfiguration和之间commitConfiguration。它会工作的!

于 2012-06-07T14:42:40.773 回答
15

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.")
}
于 2016-03-23T04:57:12.810 回答
0

在 swift 5x 中,你可以使用这个:

do {
        guard let audioDevice = AVCaptureDevice.default(for: AVMediaType.audio) else {
            print("Default audio device is unavailable.")
            setupResult = .configurationFailed
            session.commitConfiguration()
            return
        }

        // Add audio input
        let audioInput = try AVCaptureDeviceInput(device: audioDevice)
        if session.canAddInput(audioInput) {
            session.addInput(audioInput)
        } else {
            print("Couldn't add audio device input to the session.")
            setupResult = .configurationFailed
            session.commitConfiguration()
            return
        }

    } catch {
        print("Couldn't create Audio device input: \(error)")
        setupResult = .configurationFailed
        session.commitConfiguration()
        return
    }
于 2021-01-23T04:18:25.093 回答