Respect system alert volume for notifications while in app

// FREEBIE
This commit is contained in:
Michael Kirk 2018-03-29 12:57:55 -04:00
parent 363a1ae332
commit 3cb53f5f44
5 changed files with 38 additions and 16 deletions

View File

@ -239,8 +239,9 @@
} else {
if (shouldPlaySound && [Environment.preferences soundInForeground]) {
OWSSound sound = [OWSSounds notificationSoundForThread:thread];
self.audioPlayer = [OWSSounds audioPlayerForSound:sound];
[self.audioPlayer playAsForegroundAlert];
SystemSoundID soundId = [OWSSounds systemSoundIDForSound:sound quiet:YES];
// Vibrate, respect silent switch, respect "Alert" volume, not media volume.
AudioServicesPlayAlertSound(soundId);
}
}
});
@ -345,8 +346,9 @@
} else {
if (shouldPlaySound && [Environment.preferences soundInForeground]) {
OWSSound sound = [OWSSounds notificationSoundForThread:thread];
self.audioPlayer = [OWSSounds audioPlayerForSound:sound];
[self.audioPlayer playAsForegroundAlert];
SystemSoundID soundId = [OWSSounds systemSoundIDForSound:sound quiet:YES];
// Vibrate, respect silent switch, respect "Alert" volume, not media volume.
AudioServicesPlayAlertSound(soundId);
}
}
});

View File

@ -2,6 +2,8 @@
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
#import <AudioToolbox/AudioServices.h>
NS_ASSUME_NONNULL_BEGIN
typedef NS_ENUM(NSUInteger, OWSSound) {
@ -57,6 +59,7 @@ typedef NS_ENUM(NSUInteger, OWSSound) {
+ (void)setGlobalNotificationSound:(OWSSound)sound transaction:(YapDatabaseReadWriteTransaction *)transaction;
+ (OWSSound)notificationSoundForThread:(TSThread *)thread;
+ (SystemSoundID)systemSoundIDForSound:(OWSSound)sound quiet:(BOOL)quiet;
+ (void)setNotificationSound:(OWSSound)sound forThread:(TSThread *)thread;
#pragma mark - AudioPlayer

View File

@ -16,6 +16,7 @@ NSString *const kOWSSoundsStorageGlobalNotificationKey = @"kOWSSoundsStorageGlob
@interface OWSSounds ()
@property (nonatomic, readonly) YapDatabaseConnection *dbConnection;
@property (nonatomic, readonly) NSMutableDictionary<NSString *, NSNumber *> *cachedSoundIDs;
@end
@ -51,6 +52,7 @@ NSString *const kOWSSoundsStorageGlobalNotificationKey = @"kOWSSoundsStorageGlob
OWSAssert(primaryStorage);
_dbConnection = primaryStorage.newDatabaseConnection;
_cachedSoundIDs = [NSMutableDictionary new];
OWSSingletonAssert();
@ -207,6 +209,33 @@ NSString *const kOWSSoundsStorageGlobalNotificationKey = @"kOWSSoundsStorageGlob
return url;
}
+ (SystemSoundID)systemSoundIDForSound:(OWSSound)sound quiet:(BOOL)quiet
{
return [self.sharedManager systemSoundIDForSound:(OWSSound)sound quiet:quiet];
}
- (SystemSoundID)systemSoundIDForSound:(OWSSound)sound quiet:(BOOL)quiet
{
NSString *cacheKey = [NSString stringWithFormat:@"%lu:%d", (unsigned long)sound, quiet];
NSNumber *cachedSoundId = self.cachedSoundIDs[cacheKey];
if (cachedSoundId) {
return (SystemSoundID)cachedSoundId.intValue;
}
NSURL *soundURL = [self.class soundURLForSound:sound quiet:quiet];
DDLogVerbose(@"%@ creating system sound for %@", self.logTag, soundURL.lastPathComponent);
SystemSoundID newSoundID;
OSStatus status = AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(soundURL), &newSoundID);
OWSAssert(status == 0);
OWSAssert(newSoundID);
self.cachedSoundIDs[cacheKey] = @(newSoundID);
return newSoundID;
}
#pragma mark - Notifications
+ (OWSSound)defaultNotificationSound

View File

@ -38,9 +38,6 @@ typedef NS_ENUM(NSInteger, AudioPlaybackState) {
// respects silent switch
- (void)playWithCurrentAudioCategory;
// respects silent switch, mixes with others
- (void)playAsForegroundAlert;
// will ensure sound is audible, even if silent switch is enabled
- (void)playWithPlaybackAudioCategory;

View File

@ -104,15 +104,6 @@ NS_ASSUME_NONNULL_BEGIN
[self play];
}
- (void)playAsForegroundAlert
{
OWSAssertIsOnMainThread();
[OWSAudioSession.shared startAmbientAudioActivity:self.audioActivity];
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
[self play];
}
- (void)play
{
OWSAssertIsOnMainThread();