AVCaptureFileOutputRecordingDelegate not writing to file, Swift 2

My view controller contains a preview layer, which projects the image from my camera live. When pressing and holding a button, my code is supposed to record a video, and write it to a temporary file locally. This worked well with Swift 1.2 and Xcode 6, but stopped working after I converted the code to Swift 2 when updating to Xcode 7. When I let go of the button, the captureOutput doesn´t get called, and there is no file written to the given path.


The relevent code lies here: http://stackoverflow.com/questions/34030277/avcapturefileoutputrecordingdelegate-not-writing-to-file-swift-2 (I couldn't figure out how to paste it here smoothly).


I would appreciate any help!

Accepted Answer

(I couldn't figure out how to paste it here smoothly).

You just can paste codes from plain-text editor (Xcode source editor will do) using <> icon, you better try more about properly showing needed information. Not many readers follow external links.


And, your code is completely broken around these lines:

outputPath = (NSURL(fileURLWithPath: NSTemporaryDirectory())).URLByAppendingPathComponent("movie.mov").absoluteString as NSString
outputURL = NSURL(fileURLWithPath: outputPath as String)

Insert print(outputPath) after them and you'll find something like this:

file:///Users/...(a long long path).../tmp/movie.mov

This is not a valid `file path`, it's a string representation of URL, not a file path string.


At least you need to change the two lines as follows:

outputURL = NSURL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true).URLByAppendingPathComponent("movie.mov")
outputPath = outputURL.path!

With this change, print(outputPath) shows a valid file path:

/Users/...(a long long path).../tmp/movie.mov


I haven't checked other parts of your code, so you may need more fixes. But first, see what happens after fixing the two lines.

Thank you so much! I have worked on this bug for almost two weeks now, without getting anywhere, but this totally did it. This also made the differences in URL and path clearer to me.

AVCaptureFileOutputRecordingDelegate not writing to file, Swift 2
 
 
Q