Respect system alert volume for notifications while in app

// FREEBIE
pull/1/head
Michael Kirk 8 years ago
parent 363a1ae332
commit 3cb53f5f44

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

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

@ -16,6 +16,7 @@ NSString *const kOWSSoundsStorageGlobalNotificationKey = @"kOWSSoundsStorageGlob
@interface OWSSounds () @interface OWSSounds ()
@property (nonatomic, readonly) YapDatabaseConnection *dbConnection; @property (nonatomic, readonly) YapDatabaseConnection *dbConnection;
@property (nonatomic, readonly) NSMutableDictionary<NSString *, NSNumber *> *cachedSoundIDs;
@end @end
@ -51,6 +52,7 @@ NSString *const kOWSSoundsStorageGlobalNotificationKey = @"kOWSSoundsStorageGlob
OWSAssert(primaryStorage); OWSAssert(primaryStorage);
_dbConnection = primaryStorage.newDatabaseConnection; _dbConnection = primaryStorage.newDatabaseConnection;
_cachedSoundIDs = [NSMutableDictionary new];
OWSSingletonAssert(); OWSSingletonAssert();
@ -207,6 +209,33 @@ NSString *const kOWSSoundsStorageGlobalNotificationKey = @"kOWSSoundsStorageGlob
return url; 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 #pragma mark - Notifications
+ (OWSSound)defaultNotificationSound + (OWSSound)defaultNotificationSound

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

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

Loading…
Cancel
Save