iOS:AVCaptureSession 不会录制超过 11 秒的音频 [重复]
Posted
技术标签:
【中文标题】iOS:AVCaptureSession 不会录制超过 11 秒的音频 [重复]【英文标题】:iOS: AVCaptureSession won't record audio longer than 11 seconds [duplicate] 【发布时间】:2018-06-05 11:12:37 【问题描述】:我正在构建这个 iPad 应用程序来录制带音频的视频。当视图出现时它开始记录,然后您可以使用按钮停止记录,否则它会在计时器达到 0 时停止。
这是整个 Controller 类。
import UIKit
import AVKit
class RecorderController: UIViewController, AVCaptureFileOutputRecordingDelegate
@IBOutlet weak var previewView: UIView!
@IBOutlet weak var countdownLabel: UILabel!
var videoUrl: URL?
var seconds = 11 // Works with 11 seconds, any thing longer audio disappears
var timer = Timer()
var captureSession: AVCaptureSession?
var videoPreviewLayer: AVCaptureVideoPreviewLayer?
var fileName: String = ""
var documentsURL: URL?
var filePath: URL?
let videoFileOutput = AVCaptureMovieFileOutput()
override func viewDidLoad()
super.viewDidLoad()
previewView.layer.cornerRadius = 15
previewView.clipsToBounds = true
override func viewDidAppear(_ animated: Bool)
startVideoRecording()
let recordingDelegate: AVCaptureFileOutputRecordingDelegate? = self
videoPreviewLayer!.frame.size = previewView.frame.size
videoFileOutput.startRecording(to: filePath!, recordingDelegate: recordingDelegate!)
runTimer()
func runTimer()
countdownLabel.text = "\(seconds)"
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
@objc func updateTimer()
seconds -= 1 //This will decrement(count down)the seconds.
countdownLabel.text = "\(seconds)" //This will update the label.
if (seconds == 0)
timer.invalidate()
stopRecording()
func startVideoRecording()
let captureDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .front)
let captureAudio = AVCaptureDevice.default(for: .audio)
fileName = "mysavefile.mp4"
documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
filePath = documentsURL?.appendingPathComponent(fileName) //.URLByAppendingPathComponent(fileName)
do
let input = try AVCaptureDeviceInput(device: captureDevice!)
let audio = try AVCaptureDeviceInput(device: captureAudio!)
captureSession = AVCaptureSession()
captureSession?.sessionPreset = .high
captureSession?.addInput(input)
captureSession?.addInput(audio)
videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession!)
videoPreviewLayer?.videoGravity = .resizeAspectFill
videoPreviewLayer?.frame.size = previewView.frame.size
previewView.layer.addSublayer(videoPreviewLayer!)
self.captureSession!.addOutput(videoFileOutput)
captureSession?.startRunning()
catch
print(error)
@IBAction func stopButton(_ sender: Any)
stopRecording()
func stopRecording()
videoFileOutput.stopRecording()
func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?)
videoUrl = outputFileURL
captureSession?.stopRunning()
videoPreviewLayer?.removeFromSuperlayer()
self.performSegue(withIdentifier: "showViewer", sender: self)
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
let transition = CATransition()
transition.duration = 0.5
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromRight
transition.timingFunction = CAMediaTimingFunction(name:kCAMediaTimingFunctionEaseInEaseOut)
self.view.window!.layer.add(transition, forKey: kCATransition)
guard let vc = segue.destination as? ViewerController else return
vc.videoUrl = self.videoUrl
self.present(vc, animated: false, completion: nil)
如果我将计时器设置为 11 秒或更低,则视频会与音频完美结合。但真正奇怪的是,如果我将计时器增加到超过 11 秒,并在计时器达到 0 时停止录音,录音绝对没有声音。
有什么可能导致这种情况的想法吗?
【问题讨论】:
【参考方案1】:我遇到了同样的问题,并在这个 SO 帖子中找到了解决方案:https://***.com/a/31089253/1117968
您应该能够通过设置来解决您的问题:
videoFileOutput.movieFragmentInterval = kCMTimeInvalid
在开始录制之前。
【讨论】:
请将问题标记为重复,而不是再次发布相同的答案。谢谢。 我该怎么做?没关系,只是看到你已经这样做了......以上是关于iOS:AVCaptureSession 不会录制超过 11 秒的音频 [重复]的主要内容,如果未能解决你的问题,请参考以下文章
当发生 AVCaptureSessionInterruptionReasonAudioDeviceInUseByAnotherClient 时 AVCaptureSession 不在 ios 中录制视