Proximity playsback through earpiece

This is kind of unfortunate because playback doesn't allow the earpiece as an
output device.
pull/1/head
Michael Kirk 7 years ago
parent 3b4188f34b
commit 4b4b18c625

@ -102,6 +102,8 @@ public class OWSAudioSession: NSObject {
if aggregateOptions.contains(.record) { if aggregateOptions.contains(.record) {
assert(avAudioSession.recordPermission() == .granted) assert(avAudioSession.recordPermission() == .granted)
try avAudioSession.setCategory(AVAudioSessionCategoryRecord) try avAudioSession.setCategory(AVAudioSessionCategoryRecord)
} else if aggregateOptions.contains(.proximitySwitchesToEarPiece) {
try ensureCategoryForProximityState()
} else if aggregateOptions.contains(.playback) { } else if aggregateOptions.contains(.playback) {
try avAudioSession.setCategory(AVAudioSessionCategoryPlayback) try avAudioSession.setCategory(AVAudioSessionCategoryPlayback)
} else { } else {
@ -110,41 +112,36 @@ public class OWSAudioSession: NSObject {
if aggregateOptions.contains(.proximitySwitchesToEarPiece) { if aggregateOptions.contains(.proximitySwitchesToEarPiece) {
self.device.isProximityMonitoringEnabled = true self.device.isProximityMonitoringEnabled = true
self.shouldAdjustAudioForProximity = true
} else { } else {
self.device.isProximityMonitoringEnabled = false self.device.isProximityMonitoringEnabled = false
self.shouldAdjustAudioForProximity = false
} }
ensureProximityState()
return true return true
} catch { } catch {
owsFailDebug("failed with error: \(error)") owsFailDebug("failed with error: \(error)")
return false return false
} }
} }
var shouldAdjustAudioForProximity: Bool = false @objc
func proximitySensorStateDidChange(notification: Notification) { func proximitySensorStateDidChange(notification: Notification) {
if shouldAdjustAudioForProximity { do {
ensureProximityState() try ensureCategoryForProximityState()
} catch {
owsFailDebug("error in response to proximity change: \(error)")
} }
} }
// TODO: externally modified proximityState monitoring e.g. CallViewController func ensureCategoryForProximityState() throws {
// TODO: make sure we *undo* anything as appropriate if there are concurrent audio activities if aggregateOptions.contains(.proximitySwitchesToEarPiece) {
func ensureProximityState() { if self.device.proximityState {
if self.device.proximityState { Logger.debug("proximityState: true")
Logger.debug("proximityState: true")
try! self.avAudioSession.overrideOutputAudioPort(.none) try avAudioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
} else { try avAudioSession.overrideOutputAudioPort(.none)
Logger.debug("proximityState: false") } else {
do { Logger.debug("proximityState: false")
try self.avAudioSession.overrideOutputAudioPort(.speaker) try avAudioSession.setCategory(AVAudioSessionCategoryPlayback)
} catch {
Logger.error("error: \(error)")
} }
} }
} }
@ -157,16 +154,11 @@ public class OWSAudioSession: NSObject {
defer { objc_sync_exit(self) } defer { objc_sync_exit(self) }
currentActivities = currentActivities.filter { return $0.value != audioActivity } currentActivities = currentActivities.filter { return $0.value != audioActivity }
do {
if aggregateOptions.contains(.proximitySwitchesToEarPiece) { try ensureCategoryForProximityState()
self.device.isProximityMonitoringEnabled = true } catch {
self.shouldAdjustAudioForProximity = true owsFailDebug("error in ensureProximityState: \(error)")
} else {
self.device.isProximityMonitoringEnabled = false
self.shouldAdjustAudioForProximity = false
} }
ensureProximityState()
ensureAudioSessionActivationStateAfterDelay() ensureAudioSessionActivationStateAfterDelay()
} }

Loading…
Cancel
Save