我正在开发一个使用swift的应用程序,可以在一段精确的时间内录制视频。在这个例子中,这是7秒。 所有工作对于第一个记录都很好,但是连续记录不包含音频而只包含视频。 我读了一些关于更改AVCaptureMovieFileOutput实例的movieFragmentInterval的内容,我尝试但没有任何改变。
func setup(){
self.session = AVCaptureSession()
self.session.sessionPreset = AVCaptureSessionPresetHigh
// INPUT
var devices : Array<AVCaptureDevice> = AVCaptureDevice.devices() as Array<AVCaptureDevice>
var camera:AVCaptureDevice!
var microphone:AVCaptureDevice!
for device in devices{
println("Device name: \(device)")
if device.hasTorch && device.isTorchModeSupported(AVCaptureTorchMode.On){
self.torch = device
}
if device.hasMediaType(AVMediaTypeAudio){
microphone = device
}
if device.hasMediaType(AVMediaTypeVideo){
if device.position == .Back{
println("Device position: back.")
camera = device
} } }
var error:NSErrorPointer = nil
// audio
if (microphone != nil){
var audioInput = AVCaptureDeviceInput(device: microphone, error: error)
if error == nil{
// if !error{
if self.session.canAddInput(audioInput){
self.session.addInput(audioInput)
} } }
// video input
if (camera != nil){
var VideoInput = AVCaptureDeviceInput(device: camera, error: error)
// if !error{
if error == nil{
if self.session.canAddInput(VideoInput){
self.session.addInput(VideoInput)
} } }
self.output = AVCaptureMovieFileOutput()
var preferredTimeScale:Int32 = 30
var totalSeconds:Int64 = Int64(Int(7) * Int(preferredTimeScale)) // after 7 sec video recording stop automatically
var maxDuration:CMTime = CMTimeMake(totalSeconds, preferredTimeScale)
self.output.maxRecordedDuration = maxDuration
self.output.minFreeDiskSpaceLimit = 1024 * 1024
if session.canAddOutput(self.output){
session.addOutput(self.output)
}
self.connection = self.output.connectionWithMediaType(AVMediaTypeVideo)
if self.connection.supportsVideoStabilization == true{
println("video stabilization avaible")
self.connection.enablesVideoStabilizationWhenAvailable = true
}
self.connection.videoOrientation = .LandscapeRight
self.session.startRunning()
}
func startRecording(){
self.output.startRecordingToOutputFileURL(self.tempPathURL, recordingDelegate: self)
}
func stopRecording(){
self.output.stopRecording()
}
func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!){
println("Finish recording")
var success:Bool = false
if error != 0 && error != nil{
println("error")
let value: AnyObject? = error.userInfo?[AVErrorRecordingSuccessfullyFinishedKey]
if value == nil{
success = true
}else{
success = false
}
}
if success == true{
stopRecording()
}
}