मैं वीडियो रिकॉर्ड करना और ऑडियो एक साथ खेलना चाहता हूं। वीडियो रिकॉर्डिंग शुरू करते समय, प्रीलोडेड ऑडियो स्टार्ट प्ले और वीडियो रिकॉर्डिंग को रोकते समय, ऑडियो भी खेलना बंद कर देता है।रिकॉर्ड वीडियो और एक ही समय में ऑडियो चलाएं ios
मैं तीसरे पक्ष के पुस्तकालय "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)
हो सकता है कि आप इस SO asnwer को आजमा सकें: http://stackoverflow.com/a/36912798/2859764 –
@AkshitZaveri, मैं ऑडियो चला सकता हूं और वीडियो को एक ही बार रिकॉर्ड कर सकता हूं। एकल बटन पर ऑडियो प्ले और वीडियो रिकॉर्डिंग शुरू करें क्लिक करें। लेकिन मुद्दा ऑडियो बज रहा है 0.5 सेकंड देरी (हमेशा नहीं)। –
सिद्धांत रूप में मुझे लगता है कि यह संभव है, लेकिन कैसे उन दो कार्यों सिंक्रनाइज़ करने के लिए पता नहीं है। आशा है कि कोई हमें सुरुचिपूर्ण समाधान है – sage444