我想同时录制视频和播放音频。当开始录制视频,预加载音频开始播放和停止视频录制时,音频也会停止播放。
我尝试使用第三方库“ SwiftyCam ”(https://github.com/Awalz/SwiftyCam)录制视频,并在开始写视频录制文件时以委托方式开始播放音频。
主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()
}
}
}
问题:有时会在一微秒内开始播放音频。 当音频开始播放和视频录制中的声音记录时,某些时间同步正确并且声音有一些时间延迟。
我用合并音频与原始音频检查它,并有一些延迟,如下图所示。问题并不总是随机延迟而不是同步。
有什么解决方案? 如何录制视频和播放音频同时(单击按钮)启动,没有延迟和没有同步问题? 还有其他方式吗?
用于播放音频:AVAudioPlayer
用于录制的视频:Swiftycam(AVCaptureSession)
答案 0 :(得分:6)
你可以尝试一些事情:
1)在后台队列中播放音频:
dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0)) {
self.player.play()
}
2)将viewDidAppear
(包括prepareToPlay()
)中的代码移至viewDidLoad
。我认为这是一个不太有用的建议,因为播放是由用户启动的,但是prepareToPlay()
和音频准备就绪之间存在延迟。
如果你尝试这些并且延迟仍然太多,你可能只需要使用AVAudioPlayer
之外的其他内容,例如OpenAL
。