diff --git a/Signal/src/environment/NotificationsManager.m b/Signal/src/environment/NotificationsManager.m index 7c2503dcf..a09ab3b65 100644 --- a/Signal/src/environment/NotificationsManager.m +++ b/Signal/src/environment/NotificationsManager.m @@ -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); } } }); diff --git a/SignalMessaging/environment/OWSSounds.h b/SignalMessaging/environment/OWSSounds.h index 506560d13..f2f311dd7 100644 --- a/SignalMessaging/environment/OWSSounds.h +++ b/SignalMessaging/environment/OWSSounds.h @@ -2,6 +2,8 @@ // Copyright (c) 2018 Open Whisper Systems. All rights reserved. // +#import + 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 diff --git a/SignalMessaging/environment/OWSSounds.m b/SignalMessaging/environment/OWSSounds.m index 53c36daad..ee515f258 100644 --- a/SignalMessaging/environment/OWSSounds.m +++ b/SignalMessaging/environment/OWSSounds.m @@ -16,6 +16,7 @@ NSString *const kOWSSoundsStorageGlobalNotificationKey = @"kOWSSoundsStorageGlob @interface OWSSounds () @property (nonatomic, readonly) YapDatabaseConnection *dbConnection; +@property (nonatomic, readonly) NSMutableDictionary *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 diff --git a/SignalMessaging/utils/OWSAudioPlayer.h b/SignalMessaging/utils/OWSAudioPlayer.h index 10a5acfaa..98a9a2f13 100644 --- a/SignalMessaging/utils/OWSAudioPlayer.h +++ b/SignalMessaging/utils/OWSAudioPlayer.h @@ -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; diff --git a/SignalMessaging/utils/OWSAudioPlayer.m b/SignalMessaging/utils/OWSAudioPlayer.m index bb146d117..7863704ef 100644 --- a/SignalMessaging/utils/OWSAudioPlayer.m +++ b/SignalMessaging/utils/OWSAudioPlayer.m @@ -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();