MessageSoundEffect.m 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // MessageSoundEffect.m
  3. //
  4. // Created by Jesse Squires on 2/15/13.
  5. // Copyright (c) 2013 Hexed Bits. All rights reserved.
  6. //
  7. //
  8. // The MIT License
  9. // Copyright (c) 2013 Jesse Squires
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  12. // associated documentation files (the "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. // copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
  15. // following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be included in
  18. // all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
  21. // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  22. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  23. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  24. // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. //
  26. #import "MessageSoundEffect.h"
  27. @interface MessageSoundEffect ()
  28. + (void)playSoundWithName:(NSString *)name type:(NSString *)type;
  29. @end
  30. static BOOL isPlaying = NO;
  31. @implementation MessageSoundEffect
  32. +(void)initialize
  33. {
  34. [super initialize];
  35. isPlaying = NO;
  36. }
  37. static void completionCallback (SystemSoundID mySSID, void *clientData) {
  38. // Play again after sound play completion
  39. isPlaying = NO;
  40. }
  41. + (void)playSoundWithName:(NSString *)name type:(NSString *)type
  42. {
  43. if (!isPlaying) {
  44. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:type];
  45. if([[NSFileManager defaultManager] fileExistsAtPath:path]) {
  46. NSURL *url = [NSURL fileURLWithPath:path];
  47. isPlaying = YES;
  48. SystemSoundID sound;
  49. AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &sound);
  50. AudioServicesAddSystemSoundCompletion (sound, NULL, NULL,
  51. completionCallback,
  52. (__bridge void*) self);
  53. AudioServicesPlaySystemSound(sound);
  54. // if ([name isEqualToString:@"messageReceived"]) {
  55. // AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
  56. // }
  57. }
  58. else {
  59. NSLog(@"**** Sound Error: file not found: %@", path);
  60. }
  61. }
  62. }
  63. + (void)playMessageReceivedSound
  64. {
  65. [MessageSoundEffect playSoundWithName:@"messageReceived" type:@"aiff"];
  66. }
  67. + (void)playMessageSentSound
  68. {
  69. [MessageSoundEffect playSoundWithName:@"messageSent" type:@"aiff"];
  70. }
  71. @end