From d7fcac8a5a96645b7aaf3712100ff17e51f96aca Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Mon, 5 Mar 2018 17:59:09 -0500 Subject: [PATCH] In-App notifications don't pause background audio // FREEBIE --- .../ConversationViewController.m | 2 +- .../attachments/OWSVideoPlayer.swift | 2 +- .../environment/OWSAudioSession.swift | 30 +++++++++++-------- SignalMessaging/environment/OWSSounds.m | 2 +- SignalMessaging/utils/OWSAudioPlayer.h | 3 ++ SignalMessaging/utils/OWSAudioPlayer.m | 10 ++++++- 6 files changed, 32 insertions(+), 17 deletions(-) diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m index 95361da33..3206a1854 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m @@ -3203,7 +3203,7 @@ typedef enum : NSUInteger { NSURL *fileURL = [NSURL fileURLWithPath:filepath]; // Setup audio session - BOOL configuredAudio = [OWSAudioSession.shared setRecordCategoryWithAudioActivity:self.voiceNoteAudioActivity]; + BOOL configuredAudio = [OWSAudioSession.shared startRecordingAudioActivity:self.voiceNoteAudioActivity]; if (!configuredAudio) { OWSFail(@"%@ Couldn't configure audio session", self.logTag); [self cancelVoiceMemo]; diff --git a/SignalMessaging/attachments/OWSVideoPlayer.swift b/SignalMessaging/attachments/OWSVideoPlayer.swift index f5405e64b..c5b86db31 100644 --- a/SignalMessaging/attachments/OWSVideoPlayer.swift +++ b/SignalMessaging/attachments/OWSVideoPlayer.swift @@ -38,7 +38,7 @@ public class OWSVideoPlayer: NSObject { } public func play() { - OWSAudioSession.shared.setPlaybackCategory(audioActivity: self.audioActivity) + OWSAudioSession.shared.startPlaybackAudioActivity(self.audioActivity) guard let item = avPlayer.currentItem else { owsFail("\(logTag) video player item was unexpectedly nil") diff --git a/SignalMessaging/environment/OWSAudioSession.swift b/SignalMessaging/environment/OWSAudioSession.swift index e892975ff..62a5baa62 100644 --- a/SignalMessaging/environment/OWSAudioSession.swift +++ b/SignalMessaging/environment/OWSAudioSession.swift @@ -33,13 +33,23 @@ public class OWSAudioSession: NSObject { private var currentActivities: [Weak] = [] - // Ignores hardware mute switch, plays through external speaker - public func setPlaybackCategory(audioActivity: AudioActivity) { + // Respects hardware mute switch, plays through external speaker, mixes with backround audio + // appropriate for foreground sound effects. + public func startAmbientAudioActivity(_ audioActivity: AudioActivity) { + Logger.debug("\(logTag) in \(#function)") + + startAudioActivity(audioActivity) + + do { + try avAudioSession.setCategory(AVAudioSessionCategoryAmbient) + } catch { + owsFail("\(logTag) in \(#function) failed with error: \(error)") + } + } + + // Ignores hardware mute switch, plays through external speaker + public func startPlaybackAudioActivity(_ audioActivity: AudioActivity) { Logger.debug("\(logTag) in \(#function)") - - // In general, we should have put the audio session back to it's default - // category when we were done with whatever activity required it to be modified - assert(avAudioSession.category == AVAudioSessionCategorySoloAmbient) startAudioActivity(audioActivity) @@ -50,13 +60,9 @@ public class OWSAudioSession: NSObject { } } - public func setRecordCategory(audioActivity: AudioActivity) -> Bool { + public func startRecordingAudioActivity(_ audioActivity: AudioActivity) -> Bool { Logger.debug("\(logTag) in \(#function)") - // In general, we should have put the audio session back to it's default - // category when we were done with whatever activity required it to be modified - assert(avAudioSession.category == AVAudioSessionCategorySoloAmbient) - assert(avAudioSession.recordPermission() == .granted) startAudioActivity(audioActivity) @@ -104,8 +110,6 @@ public class OWSAudioSession: NSObject { } do { - try avAudioSession.setCategory(AVAudioSessionCategorySoloAmbient) - // When playing audio in Signal, other apps audio (e.g. Music) is paused. // By notifying when we deactivate, the other app can resume playback. try avAudioSession.setActive(false, with: [.notifyOthersOnDeactivation]) diff --git a/SignalMessaging/environment/OWSSounds.m b/SignalMessaging/environment/OWSSounds.m index 86f4afa44..a9247f583 100644 --- a/SignalMessaging/environment/OWSSounds.m +++ b/SignalMessaging/environment/OWSSounds.m @@ -224,7 +224,7 @@ NSString *const kOWSSoundsStorageGlobalNotificationKey = @"kOWSSoundsStorageGlob [self.audioPlayer stop]; self.audioPlayer = [OWSSounds audioPlayerForSound:sound quiet:quiet]; if (shouldRespectSilentSwitch) { - [self.audioPlayer playWithCurrentAudioCategory]; + [self.audioPlayer playWithAmbientAudioCategory]; } else { [self.audioPlayer playWithPlaybackAudioCategory]; } diff --git a/SignalMessaging/utils/OWSAudioPlayer.h b/SignalMessaging/utils/OWSAudioPlayer.h index 98a9a2f13..f8aee4045 100644 --- a/SignalMessaging/utils/OWSAudioPlayer.h +++ b/SignalMessaging/utils/OWSAudioPlayer.h @@ -38,6 +38,9 @@ typedef NS_ENUM(NSInteger, AudioPlaybackState) { // respects silent switch - (void)playWithCurrentAudioCategory; +// respects silent switch, mixes with others +- (void)playWithAmbientAudioCategory; + // will ensure sound is audible, even if silent switch is enabled - (void)playWithPlaybackAudioCategory; diff --git a/SignalMessaging/utils/OWSAudioPlayer.m b/SignalMessaging/utils/OWSAudioPlayer.m index e01efcaab..5f935d507 100644 --- a/SignalMessaging/utils/OWSAudioPlayer.m +++ b/SignalMessaging/utils/OWSAudioPlayer.m @@ -99,7 +99,15 @@ NS_ASSUME_NONNULL_BEGIN - (void)playWithPlaybackAudioCategory { OWSAssertIsOnMainThread(); - [OWSAudioSession.shared setPlaybackCategoryWithAudioActivity:self.audioActivity]; + [OWSAudioSession.shared startPlaybackAudioActivity:self.audioActivity]; + + [self play]; +} + +- (void)playWithAmbientAudioCategory +{ + OWSAssertIsOnMainThread(); + [OWSAudioSession.shared startAmbientAudioActivity:self.audioActivity]; [self play]; }