简体   繁体   English

在应用程序终止之前获取 AVAudio Recording URL

[英]Get AVAudio Recording URL Before App is Terminated

I've played with several recording apps and when I swipe to dismiss/terminate while recording, when I come back to the app the audio that was recording prior to the app begin terminated, is available for me to listen to.我玩过几个录音应用程序,当我在录音时滑动关闭/终止时,当我回到应用程序时,在应用程序开始终止之前录制的音频可供我收听。 From my understanding that audio is only available via audioRecorderDidFinishRecording .据我了解,音频只能通过audioRecorderDidFinishRecording

In my app I tried to do the same.在我的应用程序中,我尝试做同样的事情。 If a user is recording audio and the app is suddenly terminated (they swipe it away), I stop the recording.如果用户正在录制音频并且应用程序突然终止(他们将其滑开),我会停止录制。 But in the delegate audioRecorderDidFinishRecording I can't seem to get the url .但是在代表audioRecorderDidFinishRecording中,我似乎无法获得url

When the app is terminated the docs says we have 5 seconds当应用程序终止时, 文档说我们有 5 秒

Your implementation of this method has approximately five seconds to perform any tasks and return.您对此方法的实现大约有五秒钟的时间来执行任何任务并返回。 If the method does not return before time expires, the system may terminate the process altogether.如果该方法在时间到期之前没有返回,则系统可以完全终止该过程。

The url usually returns way before 5 seconds but for some reason the recording delegate is never reached. url通常会在 5 秒之前返回,但由于某种原因,从未到达录制委托。 What's the issue?有什么问题? In the example below steps 2 or 5 never get reached.在下面的示例中,步骤 2 或 5 永远不会到达。

NotificationCenter.default.addObserver(self, selector: #selector(applicationWillTerminate(notification:)),
                                               name: UIApplication.willTerminateNotification,
                                               object: nil)

@objc func applicationWillTerminate(notification: Notification) {

     if let micRecorder = micRecorder, micRecorder.isRecording {

         micRecorder.stop() // 1. stop recording
     }
}

func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {

    // 2. print("if successful the url should be returned")

    if flag {

        let url = recorder.url

        // 3. save url to FileManager then to CoreData

    } else {

        // 4. something went awry
    }
}

func audioRecorderEncodeErrorDidOccur(_ recorder: AVAudioRecorder, error: Error?) {

    // 5. print("something went wrong")

    print(error.localizedDescription)
}

When you initialize AVAudioRecorder , you pass it a URL.当你初始化AVAudioRecorder时,你传递给它一个 URL。 This URL is where the audio data is recorded.这个 URL 是记录音频数据的地方。 That URL will be the same as the URL that you're attempting to get via the delegate methods. URL 将与您尝试通过委托方法获取的 URL 相同。

Instead of trying to retrieve the URL via those delegate methods, you can just store the initial URL that you created or just make sure that you can reliably recreate it.无需尝试通过这些委托方法检索 URL,您只需存储您创建的初始 URL 或确保您可以可靠地重新创建它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM