In-App notifications don't pause background audio

// FREEBIE
This commit is contained in:
Michael Kirk 2018-03-05 17:59:09 -05:00
parent b0b012046b
commit d7fcac8a5a
6 changed files with 32 additions and 17 deletions

View file

@ -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];

View file

@ -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")

View file

@ -33,13 +33,23 @@ public class OWSAudioSession: NSObject {
private var currentActivities: [Weak<AudioActivity>] = []
// 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])

View file

@ -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];
}

View file

@ -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;

View file

@ -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];
}