如果视频超过12秒则没有声音

时间:2017-03-19 19:19:15

标签: swift avfoundation video-capture

我使用AVFoundation创建了一个自定义的vido录制应用程序。 问题是当录音达到13秒长时,声音完全从结果视频中消失。 我设置摄像头是代码:

private func configure(_ mode: CameraMode) {
    guard let devices = AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo) as? [AVCaptureDevice] else {return}
    for device in devices {
        if device.position == .back {
            do {
                let input = try AVCaptureDeviceInput(device: device)
                if session.canAddInput(input) {
                    session.addInput(input)
                }
                configureVideoOutput(for: device)
                session.startRunning()
            } catch (let error) {
                print(error)
            }
        }
    }

}


private func configureVideoOutput(for device: AVCaptureDevice) {
    if device.hasMediaType(AVMediaTypeVideo) && session.canAddOutput(sessionVideoOutput) {
        session.beginConfiguration()
        session.sessionPreset = AVCaptureSessionPresetHigh
        guard let devices = AVCaptureDevice.devices(withMediaType: AVMediaTypeAudio) as? [AVCaptureDevice] else {return}
        for device in devices {
            do {
                currentDevice = device
                let input = try AVCaptureDeviceInput(device: device)
                if session.canAddInput(input) {
                    session.addInput(input)
                    break
                }
            } catch (let error) {
                print(error)
                session.commitConfiguration()
            }
        }
        session.addOutput(sessionVideoOutput)
        session.commitConfiguration()
    }
}
使用此代码

和星级录制视频:

 @objc private func takeVideo(_ gesture: UILongPressGestureRecognizer) {
    switch gesture.state {
    case .began:
        print("begin take video")
        configure(.video)
        if let _ = sessionVideoOutput.connection(withMediaType: AVMediaTypeAudio) {
            let fileName = "mysavefile.mp4";
            let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
            let filePath = documentsURL.appendingPathComponent(fileName)
            sessionVideoOutput.startRecording(toOutputFileURL: filePath, recordingDelegate: self)
            setCaptureButtonToVideoMode(on: true)
        }
    case .ended, .cancelled:
        print("end take video")
        sessionVideoOutput.stopRecording()
        setCaptureButtonToVideoMode(on: false)
    default:
        break
    }
}

最后我将视频保存到相机胶卷:

func capture(_ captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAt outputFileURL: URL!, fromConnections connections: [Any]!, error: Error!) {
    print("capture did finish")
    print(captureOutput)
    print(outputFileURL)
    PHPhotoLibrary.shared().performChanges({
        PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: outputFileURL)
    }) { saved, error in
    }
}

如果视频小于13秒,一切都按预期工作。

1 个答案:

答案 0 :(得分:1)

当我录制电影时,我使用VCaptureMovieFileOutput()作为会话输出,它具有名为movieFragmentInterval的属性,默认设置为10秒。将此属性更改为kCMTimeInvalid可解决此问题。