与 AVFoundation 有点混淆

A Bit of Confusion With AVFoundation

所以我已经使用 AVFoundation 工作了几周了,真痛苦!

我在我的相机应用程序中尝试将正在录制的视频保存到相册中。我在 google 上四处查看,众所周知,Apple 的 AVFoundation 文档非常糟糕。

我不太确定如何将视频保存到相册。我已经阅读了很多关于如何处理图片的内容,但是视频有点不同,因为它们需要 URL.

我下面的代码不太行得通。它确实到达了 println("Succesfully saved the video to the photo album") 语句,但之后,它因错误

而崩溃

Succesfully saved the video to the photo album 2015-05-16 19:09:02.371 CopWatch[685:234398] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter' *** First throw call stack: (0x183e4e59c 0x19454c0e4 0x183e4e4dc 0x184c90528 0x184c90488 0x18eae665c 0x189f66090 0x100618e30 0x100618df0 0x100623854 0x10061c120 0x10062575c 0x100626f18 0x194d6d2e4 0x194d6cfa8) libc++abi.dylib: terminating with uncaught exception of type NSException

我不太明白这里的逻辑。我想我的 google 搜索技能达不到这个基础,似乎很难在这些东西上找到东西。有人可以帮我吗?

Blockquote

/**************************************************************************
    DID PRESS CAPTURE
    **************************************************************************/
    @IBAction func didPressCapture(sender: AnyObject) {

        if self.takingVideo == true {

            //------ MAKE SURE WE ARE NOT ALREADY RECORDING ------
            if self.weAreRecording == false {

                println("Taking a video")

                //------ MAKE SURE THE DEVICE IS AUTHORIZED TO TAKE A VIDEO ------
                if self.deviceAuthorized == AVAuthorizationStatus.Authorized {

                    println("Device is authorized to take video")

                    dispatch_async(self.sessionQueue) {

                        println("Getting Video Output Path")

                        //------ GRAB THE OUTPUT FILE URL TO SAVE TO PHOTO ALBUM ------
                        let outputPath = "\(self.documentsPath)/output.mov"

                        self.outputFileURL = NSURL(fileURLWithPath: outputPath)
                        self.session.startRunning()

                        println("Starting to record to output path")

                        self.movieFileOutput!.startRecordingToOutputFileURL(self.outputFileURL!, recordingDelegate: self)

                        self.isSessionRunning = true
                    }
                }
                else {

                    println("Device is not authorized to take video")

                }

                self.weAreRecording = true
            }
            else {

                //------ STOP THE VIDEO, PROCESS THE VIDEO HERE ------
                println("Stopping the video")

                dispatch_async(self.sessionQueue) {

                    println("Stopping the session from recording")

                    //------ STOP RECORDING AND SAVE TO VIDEO TO PHOTO ALBUM ------
                    self.session.stopRunning()
                    self.movieFileOutput!.stopRecording()

                    println("Saving the video to the photo albums")

                    UISaveVideoAtPathToSavedPhotosAlbum(String(contentsOfURL: self.outputFileURL!), nil, nil, nil)

                    println("Succesfully saved the video to the photo album")

                    self.isSessionRunning = false
                }

                self.weAreRecording = false
            }
        }
        else {

            //------ HANDLE TAKING A PICTURE HERE ------
            println("Taking a picture")
        }
    }

这与 AV 基金会没有什么特别的关系,只是一种巧合。问题在于 异步 方法的性质——几乎所有与 AVFoundation 相关的东西都是异步的。

因此,您收到控制台消息的顺序是:

(1)成功将视频保存到相册

(2) 2015-05-16 19:09:02.371 CopWatch[685:234398] * 由于未捕获的异常 'NSInvalidArgumentException' 而终止应用程序,原因:'* -[NSURL initFileURLWithPath:]: 无字符串参数'

...没有意义。 UISaveVideoAtPathToSavedPhotosAlbum 是 异步的 。这意味着它需要时间(实际上相当长的时间)并且在后台线程上运行。同时,您的主要代码只是愉快地进行。 然后,在其后台线程上,UISaveVideoAtPathToSavedPhotosAlbum 崩溃并烧毁 — 大概是因为与线程混淆。

要解决这个问题,首先返回并修复对 UISaveVideoAtPathToSavedPhotosAlbum 的调用,使其具有 完成处理程序 。除了在该完成处理程序中,什么都不做。因此,您的 println 将在完成处理程序中发生,因此您将真正看到视频是否已成功保存。