-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add audio services system sound API wrapper class.
- Loading branch information
Showing
3 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |