我希望能够同时录制视频和播放音频。当开始录制视频时,预加载的音频开始播放,当停止视频录制时,音频也停止播放。
我尝试使用第三方库“SwiftyCam”(https://github.com/Awalz/SwiftyCam)录制视频,并在委托方法中开始播放音频。
主视图控制器代码如下:
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()
}
}
}
问题:有时候微秒级别的音频播放会出现延迟。当音频开始播放并且录制视频时,有时候声音同步正常,有时候声音有延迟。
我用合并原始音频来检查了这个问题,如下图所示存在一些延迟。这个问题并不总是出现,而是随机延迟并且不同步。
![enter image description here](https://istack.dev59.com/n7QC7.webp)
![enter image description here](https://istack.dev59.com/MXu5k.webp)
音频播放使用:AVAudioPlayer 视频录制使用:Swiftycam (AVCaptureSession)
AVMutableCompositionTrack
在程序中合并音频轨道,将音乐添加到其中。我之前也做过类似的任务,将音频与视频合并后最终导出了视频。@BhargavB.Bajani - Dhiru