SoundManager.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // SoundManager.h
  3. //
  4. // Version 1.3.1
  5. //
  6. // Created by Nick Lockwood on 29/01/2011.
  7. // Copyright 2010 Charcoal Design
  8. //
  9. // Distributed under the permissive zlib license
  10. // Get the latest version from either of these locations:
  11. //
  12. // http://charcoaldesign.co.uk/source/cocoa#soundmanager
  13. // https://github.com/nicklockwood/SoundManager
  14. //
  15. // This software is provided 'as-is', without any express or implied
  16. // warranty. In no event will the authors be held liable for any damages
  17. // arising from the use of this software.
  18. //
  19. // Permission is granted to anyone to use this software for any purpose,
  20. // including commercial applications, and to alter it and redistribute it
  21. // freely, subject to the following restrictions:
  22. //
  23. // 1. The origin of this software must not be misrepresented; you must not
  24. // claim that you wrote the original software. If you use this software
  25. // in a product, an acknowledgment in the product documentation would be
  26. // appreciated but is not required.
  27. //
  28. // 2. Altered source versions must be plainly marked as such, and must not be
  29. // misrepresented as being the original software.
  30. //
  31. // 3. This notice may not be removed or altered from any source distribution.
  32. //
  33. //
  34. // ARC Helper
  35. //
  36. // Version 1.3
  37. //
  38. // Created by Nick Lockwood on 05/01/2012.
  39. // Copyright 2012 Charcoal Design
  40. //
  41. // Distributed under the permissive zlib license
  42. // Get the latest version from here:
  43. //
  44. // https://gist.github.com/1563325
  45. //
  46. #ifndef AH_RETAIN
  47. #if __has_feature(objc_arc)
  48. #define AH_RETAIN(x) (x)
  49. #define AH_RELEASE(x) (void)(x)
  50. #define AH_AUTORELEASE(x) (x)
  51. #define AH_SUPER_DEALLOC (void)(0)
  52. #define __AH_BRIDGE __bridge
  53. #else
  54. #define __AH_WEAK
  55. #define AH_WEAK assign
  56. #define AH_RETAIN(x) [(x) retain]
  57. #define AH_RELEASE(x) [(x) release]
  58. #define AH_AUTORELEASE(x) [(x) autorelease]
  59. #define AH_SUPER_DEALLOC [super dealloc]
  60. #define __AH_BRIDGE
  61. #endif
  62. #endif
  63. // ARC Helper ends
  64. #import <Availability.h>
  65. #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED)
  66. #import <UIKit/UIKit.h>
  67. #define SM_USE_AV_AUDIO_PLAYER
  68. #else
  69. #import <Cocoa/Cocoa.h>
  70. #if __MAC_OS_X_VERSION_MIN_REQUIRED > __MAC_10_6
  71. #define SM_USE_AV_AUDIO_PLAYER
  72. #endif
  73. #endif
  74. #ifdef SM_USE_AV_AUDIO_PLAYER
  75. #import <AVFoundation/AVFoundation.h>
  76. #define SM_SOUND AVAudioPlayer
  77. #else
  78. #define SM_SOUND NSSound
  79. #endif
  80. extern NSString *const SoundDidFinishPlayingNotification;
  81. typedef void (^SoundCompletionHandler)(BOOL didFinish);
  82. @interface Sound : NSObject
  83. //required for 32-bit Macs
  84. #ifdef __i386__
  85. {
  86. @private
  87. float baseVolume;
  88. float startVolume;
  89. float targetVolume;
  90. NSTimeInterval fadeTime;
  91. NSTimeInterval fadeStart;
  92. NSTimer *timer;
  93. Sound *selfReference;
  94. NSURL *url;
  95. SM_SOUND *sound;
  96. SoundCompletionHandler completionHandler;
  97. }
  98. #endif
  99. + (Sound *)soundNamed:(NSString *)name;
  100. + (Sound *)soundWithContentsOfFile:(NSString *)path;
  101. - (Sound *)initWithContentsOfFile:(NSString *)path;
  102. + (Sound *)soundWithContentsOfURL:(NSURL *)url;
  103. - (Sound *)initWithContentsOfURL:(NSURL *)url;
  104. @property (nonatomic, readonly, copy) NSString *name;
  105. @property (nonatomic, readonly, strong) NSURL *url;
  106. @property (nonatomic, readonly, getter = isPlaying) BOOL playing;
  107. @property (nonatomic, assign, getter = isLooping) BOOL looping;
  108. @property (nonatomic, copy) SoundCompletionHandler completionHandler;
  109. @property (nonatomic, assign) float baseVolume;
  110. @property (nonatomic, assign) float volume;
  111. - (void)fadeTo:(float)volume duration:(NSTimeInterval)duration;
  112. - (void)fadeIn:(NSTimeInterval)duration;
  113. - (void)fadeOut:(NSTimeInterval)duration;
  114. - (void)play;
  115. - (void)stop;
  116. @end
  117. @interface SoundManager : NSObject
  118. //required for 32-bit Macs
  119. #ifdef __i386__
  120. {
  121. @private
  122. Sound *currentMusic;
  123. NSMutableArray *currentSounds;
  124. BOOL allowsBackgroundMusic;
  125. float soundVolume;
  126. float musicVolume;
  127. NSTimeInterval soundFadeDuration;
  128. NSTimeInterval musicFadeDuration;
  129. }
  130. #endif
  131. @property (nonatomic, readonly, getter = isPlayingMusic) BOOL playingMusic;
  132. @property (nonatomic, assign) BOOL allowsBackgroundMusic;
  133. @property (nonatomic, assign) float soundVolume;
  134. @property (nonatomic, assign) float musicVolume;
  135. @property (nonatomic, assign) NSTimeInterval soundFadeDuration;
  136. @property (nonatomic, assign) NSTimeInterval musicFadeDuration;
  137. + (SoundManager *)sharedManager;
  138. - (void)prepareToPlayWithSound:(id)soundOrName;
  139. - (void)prepareToPlay;
  140. - (void)playMusic:(id)soundOrName looping:(BOOL)looping fadeIn:(BOOL)fadeIn;
  141. - (void)playMusic:(id)soundOrName looping:(BOOL)looping;
  142. - (void)playMusic:(id)soundOrName;
  143. - (void)stopMusic:(BOOL)fadeOut;
  144. - (void)stopMusic;
  145. - (void)playSound:(id)soundOrName looping:(BOOL)looping fadeIn:(BOOL)fadeIn;
  146. - (void)playSound:(id)soundOrName looping:(BOOL)looping;
  147. - (void)playSound:(id)soundOrName;
  148. - (void)stopSound:(id)soundOrName fadeOut:(BOOL)fadeOut;
  149. - (void)stopSound:(id)soundOrName;
  150. - (void)stopAllSounds:(BOOL)fadeOut;
  151. - (void)stopAllSounds;
  152. @end