Tôi muốn ghi lại video và phát âm thanh cùng một lúc. khi bắt đầu quay video, bắt đầu phát âm thanh tải sẵn và khi dừng quay video, âm thanh cũng ngừng phát.Ghi video và phát âm thanh cùng lúc ios
Tôi cố gắng quay video bằng cách sử dụng thư viện của bên thứ ba "SwiftyCam" (https://github.com/Awalz/SwiftyCam) và bắt đầu phát âm thanh theo phương thức ủy quyền khi bắt đầu ghi tệp quay video.
đang ViewController chính là dưới đây:
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 Thư viện:
/**
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()
}
}
}
vấn đề: đôi khi nhận được một sự chậm trễ trong đầu phát âm thanh trong một micro giây. khi bắt đầu phát âm thanh và bản ghi âm thanh trong quay video đồng bộ hóa một số thời gian thích hợp và một số thời gian trễ trong âm thanh.
Tôi kiểm tra bằng âm thanh hợp nhất với âm thanh gốc và có một số độ trễ như hiển thị bên dưới hình ảnh. vấn đề không phải lúc nào cũng là sự chậm trễ ngẫu nhiên và không đồng bộ hóa.
là giải pháp nào không? làm thế nào để ghi lại video và phát âm thanh cùng một lúc (trên một nút bấm) bắt đầu và không có sự chậm trễ và không có vấn đề đồng bộ hóa? là cách nào khác không?
cho âm thanh chơi sử dụng: AVAudioPlayer
cho video ghi được sử dụng: Swiftycam (AVCaptureSession)
Có thể bạn có thể thử tiện ích SO này: http://stackoverflow.com/a/36912798/2859764 –
@AkshitZaveri ,,, Tôi có thể phát âm thanh và quay video cùng một lúc. Trên một nút bấm, bắt đầu phát âm thanh và bắt đầu quay video. nhưng vấn đề đang phát âm thanh là 0.5 giây bị trì hoãn (không phải lúc nào). –
Về lý thuyết, tôi nghĩ rằng nó có thể, nhưng không có ý tưởng làm thế nào để đồng bộ hóa hai nhiệm vụ. Hy vọng một người nào đó sẽ cho chúng ta thấy giải pháp thanh lịch – sage444