录制视频时,在后台线程AudioSessionRequestRecordPermission_block_invoke上引发错误

时间:2018-08-15 04:30:42

标签: ios swift avfoundation

我已采用Apple示例代码来快速录制带音频的视频。它似乎在这里以Obj-C的形式开源,但现在不支持。 https://developer.apple.com/library/archive/samplecode/AVCam/Introduction/Intro.html。我发现它已移植到https://github.com/Lax/iOS-Swift-Demos/blob/master/AVCam/Swift/AVCam/CameraViewController.swift

的Swift中

我有Info.Plist条目

<key>NSCameraUsageDescription</key>
<string>to record the video as you tell you stories to the camera</string>
<key>NSMicrophoneUsageDescription</key>
<string>to record the audio as you tell you stories to the camera</string> 

在代码中,我有:

    switch AVCaptureDevice.authorizationStatus(for: .video) {
        case .authorized: // The user has previously granted access to the camera
            break
        case .notDetermined: // The user has not yet been asked for camera access.
            sessionQueue.suspend()
            AVCaptureDevice.requestAccess(for: .video, completionHandler: { granted in
                if !granted {
                    self.setupResult = .notAuthorized
                }
                else {
                    self.sessionQueue.resume()
                }
            })

        case .denied: // The user has previously denied access.
            return
        case .restricted: // The user can't grant access due to restrictions.
            return
        default:
            // The user has previously denied access.
            setupResult = .notAuthorized
    }

我在使用商店中应用程序的各种设备上看到此错误。

libdispatch.dylib
_dispatch_queue_resume$VARIANT$armv81
AudioToolbox
__AudioSessionRequestRecordPermission_block_invoke
TCC
__TCCAccessRequest_block_invoke.77
TCC
__tccd_send_block_invoke
libxpc.dylib
_xpc_connection_reply_callout
libxpc.dylib
_xpc_connection_call_reply_async
libdispatch.dylib
_dispatch_client_callout3
libdispatch.dylib
_dispatch_mach_msg_async_reply_invoke$VARIANT$armv81
libdispatch.dylib
_dispatch_queue_override_invoke$VARIANT$armv81
libdispatch.dylib
_dispatch_root_queue_drain
libdispatch.dylib
_dispatch_worker_thread3
libsystem_pthread.dylib
_pthread_wqthread
libsystem_pthread.dylib
start_wqthread

是什么原因造成的?在某些iOS版本或设备上不一致。

我看到了另一个也请求音频的链接,但我读过其他不需要https://github.com/imaginary-cloud/CameraManager/blob/1e22e296be0eb8def2eb1b1c2e3bb899f5a20ed2/camera/CameraManager.swift#L340

的地方
/**
 Asks the user for camera permissions. Only works if the permissions are not yet determined. Note that it'll also automaticaly ask about the microphone permissions if you selected VideoWithMic output.

 :param: completion Completion block with the result of permission request
 */
open func askUserForCameraPermission(_ completion: @escaping (Bool) -> Void) {
    AVCaptureDevice.requestAccess(for: AVMediaType.video, completionHandler: { (allowedAccess) -> Void in
        if self.cameraOutputMode == .videoWithMic {
            AVCaptureDevice.requestAccess(for: AVMediaType.audio, completionHandler: { (allowedAccess) -> Void in
                DispatchQueue.main.async(execute: { () -> Void in
                    completion(allowedAccess)
                })
            })
        } else {
            DispatchQueue.main.async(execute: { () -> Void in
                completion(allowedAccess)
            })
        }
    })
}

0 个答案:

没有答案