录制video并同时播放audioios

我想同时录制video和播放audio。 当开始录制video时,预装audio开始播放,当停止录像时,audio也停止播放。

我尝试使用第三方库“ SwiftyCam ”( https://github.com/Awalz/SwiftyCam )录制video,并在开始写入video录制文件时以委托方法开始播放audio。

主要ViewCOntroller代码如下:

override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) var temp = Data() let audioPath = Bundle.main.path(forResource: "ab1-FAST", ofType: "mp3") temp = FileManager.default.contents(atPath: (audioPath)!)! do { player = try AVAudioPlayer(data: temp) } catch let error { print(error) } self.configureAudioSession() player!.delegate = self self.player!.prepareToPlay() } // MARK: - // MARK: - SwiftyCamViewControllerDelegate method func swiftyCam(_ swiftyCam: SwiftyCamViewController, didBeginRecordingVideo camera: SwiftyCamViewController.CameraSelection) { self.player?.play() } func swiftyCam(_ swiftyCam: SwiftyCamViewController, didFinishRecordingVideo camera: SwiftyCamViewController.CameraSelection) { if self.player != nil{ if (self.player?.isPlaying)! { self.player?.stop() } } } @IBAction func btnStartStopClick(_ sender: UIButton) { sender.isSelected = !sender.isSelected if sender.isSelected{ DispatchQueue.main.async { self.objVideoRecorderVC.startVideoRecording() } }else{ self.objVideoRecorderVC.stopVideoRecording() } } 

swiftycam库:

 /** Begin recording video of current session SwiftyCamViewControllerDelegate function SwiftyCamDidBeginRecordingVideo() will be called */ public func startVideoRecording() { guard let movieFileOutput = self.movieFileOutput else { return } if currentCamera == .rear && flashEnabled == true { enableFlash() } if currentCamera == .front && flashEnabled == true { flashView = UIView(frame: view.frame) flashView?.backgroundColor = UIColor.white flashView?.alpha = 0.85 previewLayer.addSubview(flashView!) } sessionQueue.async { [unowned self] in if !movieFileOutput.isRecording { if UIDevice.current.isMultitaskingSupported { self.backgroundRecordingID = UIApplication.shared.beginBackgroundTask(expirationHandler: nil) } // Update the orientation on the movie file output video connection before starting recording. let movieFileOutputConnection = self.movieFileOutput?.connection(withMediaType: AVMediaTypeVideo) //flip video output if front facing camera is selected if self.currentCamera == .front { movieFileOutputConnection?.isVideoMirrored = true } movieFileOutputConnection?.videoOrientation = self.getVideoOrientation() // Start recording to a temporary file. let outputFileName = UUID().uuidString let outputFilePath = (NSTemporaryDirectory() as NSString).appendingPathComponent((outputFileName as NSString).appendingPathExtension("mov")!) movieFileOutput.startRecording(toOutputFileURL: URL(fileURLWithPath: outputFilePath), recordingDelegate: self) self.isVideoRecording = true DispatchQueue.main.async { self.cameraDelegate?.swiftyCam(self, didBeginRecordingVideo: self.currentCamera) } } else { movieFileOutput.stopRecording() } } } 

问题:有时会在微秒内开始播放audio。 当audio开始播放时,video录制中的声音录制时间同步正确,声音延迟一段时间。

我检查与原始audio合并audio,并有一些延迟如下图所示。 这个问题并不总是随机的延迟,而不是同步。 在这里输入图像说明

在这里输入图像说明 有什么解决办法? 如何做loggingvideo和播放audio在同一时间(单击button)开始,没有延迟,没有同步问题? 有没有其他方法?

用于播放使用的audio:AVAudioPlayer

录制video使用:Swiftycam(AVCaptureSession)

夫妇的事情,你可以尝试:

1)在背景队列上播放audio:

 dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUN‌​D, 0)) { self.player.play() } 

2)将你在viewDidAppear (包括prepareToPlay() )中的代码移动到viewDidLoad 。 我认为这将是一个不太有用的build议,因为播放是用户发起的,但是在prepareToPlay()和你的audio准备好之间有一个延迟。

如果你尝试了这些,那么延迟还是太大了,你可能只能使用AVAudioPlayer以外的东西,比如OpenAL