Manually setup AVAudioSession (allow background music playback)

This commit is contained in:
Marc Rousavy 2021-02-23 10:27:31 +01:00
parent bcdf30fa9d
commit 49e5dd67dc
3 changed files with 31 additions and 13 deletions

View File

@ -133,17 +133,25 @@ enum FormatError {
} }
} }
enum SessionError: String { enum SessionError {
case cameraNotReady = "camera-not-ready" case cameraNotReady
case audioSessionSetupFailed(reason: String)
var code: String { var code: String {
return rawValue switch self {
case .cameraNotReady:
return "camera-not-ready"
case .audioSessionSetupFailed:
return "audio-session-setup-failed"
}
} }
var message: String { var message: String {
switch self { switch self {
case .cameraNotReady: case .cameraNotReady:
return "The Camera is not ready yet! Wait for the onInitialized() callback!" return "The Camera is not ready yet! Wait for the onInitialized() callback!"
case let .audioSessionSetupFailed(reason):
return "The audio session failed to setup! \(reason)"
} }
} }
} }

View File

@ -228,6 +228,16 @@ final class CameraView: UIView {
} }
// INPUTS // INPUTS
// Audio Setup
do {
captureSession.automaticallyConfiguresApplicationAudioSession = false
try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playAndRecord, options: [.mixWithOthers, .allowBluetooth, .defaultToSpeaker])
try AVAudioSession.sharedInstance().setActive(true)
} catch let error as NSError {
// not critical, so don't return
invokeOnError(.session(.audioSessionSetupFailed(reason: error.description)))
}
// Video Input // Video Input
do { do {
if let videoDeviceInput = self.videoDeviceInput { if let videoDeviceInput = self.videoDeviceInput {

View File

@ -20,7 +20,7 @@ export type FormatError =
| 'format/invalid-low-light-boost' | 'format/invalid-low-light-boost'
| 'format/invalid-format' | 'format/invalid-format'
| 'format/invalid-preset'; | 'format/invalid-preset';
export type SessionError = 'session/camera-not-ready'; export type SessionError = 'session/camera-not-ready' | 'session/audio-session-setup-failed';
export type CaptureError = export type CaptureError =
| 'capture/invalid-photo-format' | 'capture/invalid-photo-format'
| 'capture/encoder-error' | 'capture/encoder-error'