Skip to content

Commit

Permalink
Add audio services system sound API wrapper class.
Browse files Browse the repository at this point in the history
  • Loading branch information
niw committed Jan 30, 2018
1 parent 883cabc commit c14497c
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
6 changes: 6 additions & 0 deletions HapticKey.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
548E15431FD00412001C0D4C /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 548E15421FD00412001C0D4C /* main.m */; };
549F9236200AF543003A8D7B /* HTKTimer.m in Sources */ = {isa = PBXBuildFile; fileRef = 549F9235200AF543003A8D7B /* HTKTimer.m */; };
54A310B81FD390EB002DE7BC /* MultitouchSupport.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 54A310B71FD390EB002DE7BC /* MultitouchSupport.framework */; };
54ACC711201F15F70026CAFD /* HTKSystemSound.m in Sources */ = {isa = PBXBuildFile; fileRef = 54ACC710201F15F70026CAFD /* HTKSystemSound.m */; };
54E1E0941FE4613F007F2EE0 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 54E1E0961FE4613F007F2EE0 /* Localizable.strings */; };
54FBCF291FD4CDE0000EB4D3 /* HTKMultitouchActuator.m in Sources */ = {isa = PBXBuildFile; fileRef = 54FBCF281FD4CDE0000EB4D3 /* HTKMultitouchActuator.m */; };
54FBCF321FD52483000EB4D3 /* AppIcon.icns in Resources */ = {isa = PBXBuildFile; fileRef = 54FBCF301FD52483000EB4D3 /* AppIcon.icns */; };
Expand Down Expand Up @@ -52,6 +53,8 @@
549F9234200AF543003A8D7B /* HTKTimer.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = HTKTimer.h; sourceTree = "<group>"; };
549F9235200AF543003A8D7B /* HTKTimer.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = HTKTimer.m; sourceTree = "<group>"; };
54A310B71FD390EB002DE7BC /* MultitouchSupport.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MultitouchSupport.framework; path = ../../../../../../../System/Library/PrivateFrameworks/MultitouchSupport.framework; sourceTree = "<group>"; };
54ACC70F201F15F70026CAFD /* HTKSystemSound.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = HTKSystemSound.h; sourceTree = "<group>"; };
54ACC710201F15F70026CAFD /* HTKSystemSound.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = HTKSystemSound.m; sourceTree = "<group>"; };
54E1E0951FE4613F007F2EE0 /* en */ = {isa = PBXFileReference; fileEncoding = 10; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Localizable.strings; sourceTree = "<group>"; };
54E1E0971FE4614F007F2EE0 /* Base */ = {isa = PBXFileReference; fileEncoding = 10; lastKnownFileType = text.plist.strings; name = Base; path = Base.lproj/Localizable.strings; sourceTree = "<group>"; };
54E1E0981FE4621E007F2EE0 /* ja */ = {isa = PBXFileReference; fileEncoding = 10; lastKnownFileType = text.plist.strings; name = ja; path = ja.lproj/Localizable.strings; sourceTree = "<group>"; };
Expand Down Expand Up @@ -130,6 +133,8 @@
544C76352017311500FF155C /* HTKLoginItem.m */,
54FBCF271FD4CDE0000EB4D3 /* HTKMultitouchActuator.h */,
54FBCF281FD4CDE0000EB4D3 /* HTKMultitouchActuator.m */,
54ACC70F201F15F70026CAFD /* HTKSystemSound.h */,
54ACC710201F15F70026CAFD /* HTKSystemSound.m */,
5443CF061FE39293002D4086 /* HTKTapGestureEventListener.h */,
5443CF071FE39293002D4086 /* HTKTapGestureEventListener.m */,
549F9234200AF543003A8D7B /* HTKTimer.h */,
Expand Down Expand Up @@ -230,6 +235,7 @@
5443CF081FE39293002D4086 /* HTKTapGestureEventListener.m in Sources */,
548E153B1FD00412001C0D4C /* HTKAppDelegate.m in Sources */,
5443CEFC1FE38EA8002D4086 /* HTKEventListener.m in Sources */,
54ACC711201F15F70026CAFD /* HTKSystemSound.m in Sources */,
546E62F71FE24A1D00F287C9 /* HTKEventTap.m in Sources */,
5443CF051FE39049002D4086 /* HTKFunctionKeyEventListener.m in Sources */,
54FBCF291FD4CDE0000EB4D3 /* HTKMultitouchActuator.m in Sources */,
Expand Down
27 changes: 27 additions & 0 deletions HapticKey/Classes/HTKSystemSound.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// HTKSystemSound.h
// HapticKey
//
// Created by Yoshimasa Niwa on 1/29/18.
// Copyright © 2018 Yoshimasa Niwa. All rights reserved.
//

@import Foundation;

NS_ASSUME_NONNULL_BEGIN

@interface HTKSystemSound : NSObject

@property (nonatomic, readonly) NSString *path;

+ (instancetype)new NS_UNAVAILABLE;
- (instancetype)init NS_UNAVAILABLE;

- (instancetype)initWithPath:(NSString *)path NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithSystemSoundsGroup:(NSString *)group name:(NSString *)name;

- (void)play;

@end

NS_ASSUME_NONNULL_END
72 changes: 72 additions & 0 deletions HapticKey/Classes/HTKSystemSound.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
// HTKSystemSound.m
// HapticKey
//
// Created by Yoshimasa Niwa on 1/29/18.
// Copyright © 2018 Yoshimasa Niwa. All rights reserved.
//

#import "HTKSystemSound.h"

@import AudioToolbox;
@import os.log;

NS_ASSUME_NONNULL_BEGIN

static NSString * const kSystemSoundsPath = @"/System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds";

@interface HTKSystemSound ()

@property (nonatomic, readonly) SystemSoundID systemSoundID;

@end

@implementation HTKSystemSound

- (instancetype)init
{
[self doesNotRecognizeSelector:_cmd];
abort();
}

- (instancetype)initWithPath:(NSString *)path
{
if (self = [super init]) {
_path = [path copy];

SystemSoundID systemSoundID;
NSURL *url = [[NSURL alloc] initFileURLWithPath:path];
OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &systemSoundID);
if (error != noErr) {
os_log_error(OS_LOG_DEFAULT, "Fail to create system sound at path: %{public}@ code: %lu", path, (long)error);
return nil;
} else {
os_log_info(OS_LOG_DEFAULT, "Create system sound at path: %{public}@ id: %lu", path, (long)systemSoundID);
}
_systemSoundID = systemSoundID;
}
return self;
}

- (instancetype)initWithSystemSoundsGroup:(NSString *)group name:(NSString *)name
{
NSString * const path = [[kSystemSoundsPath stringByAppendingPathComponent:group] stringByAppendingPathComponent:name];
return [self initWithPath:path];
}

- (void)dealloc
{
OSStatus error = AudioServicesDisposeSystemSoundID(self.systemSoundID);
if (error != noErr) {
os_log_error(OS_LOG_DEFAULT, "Fail to dispose system sound id: %lu code: %lu", (long)self.systemSoundID, (long)error);
}
}

- (void)play
{
AudioServicesPlaySystemSoundWithCompletion(self.systemSoundID, NULL);
}

@end

NS_ASSUME_NONNULL_END

0 comments on commit c14497c

Please sign in to comment.