Guest User

Untitled

a guest
Jul 15th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. import UIKit
  2. import AudioToolbox
  3.  
  4. open class GciSound: NSObject {
  5. fileprivate var mSoundId = kSystemSoundID_Vibrate
  6. var mAction:() -> Void = {}
  7.  
  8. public init(SystemFile sysfile:String) {
  9. super.init()
  10. let path = "/System/Library/Audio/UISounds/\(sysfile).caf"
  11. let url = URL(fileURLWithPath:path)
  12. var theSoundID:SystemSoundID = 0
  13. let error = AudioServicesCreateSystemSoundID(url as CFURL, &theSoundID)
  14. if error == kAudioServicesNoError {
  15. mSoundId = theSoundID
  16. }
  17. }
  18.  
  19. public init(FilePath path:String) {
  20. super.init()
  21. let url = URL(fileURLWithPath:path)
  22. var theSoundID:SystemSoundID = 0
  23. let error = AudioServicesCreateSystemSoundID(url as CFURL, &theSoundID)
  24. if error == kAudioServicesNoError {
  25. mSoundId = theSoundID
  26. }
  27. }
  28.  
  29. //震动
  30. open static func PlayVibrate(){
  31. AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate));
  32. }
  33.  
  34. //声音
  35. open func play(){
  36. AudioServicesPlaySystemSound(mSoundId)
  37. }
  38.  
  39. //声音振动
  40. open func playAlter(){
  41. AudioServicesPlayAlertSound(mSoundId)
  42. }
  43.  
  44. //循环声音
  45. open func playAlertAndCompletionAction() {
  46. AudioServicesAddSystemSoundCompletion(mSoundId, nil, nil, { (sounid, pointer) in
  47. AudioServicesPlaySystemSound(sounid)
  48. }, nil)
  49. AudioServicesPlaySystemSound(mSoundId)
  50. }
  51.  
  52. //循环震动
  53. open func playVibrateAndCompletionAction() {
  54. AudioServicesAddSystemSoundCompletion(SystemSoundID(kSystemSoundID_Vibrate), nil, nil, { (sounid, pointer) in
  55. AudioServicesPlayAlertSound(sounid)
  56. }, nil)
  57. AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
  58. }
  59.  
  60. //循环声音振动
  61. open func playAlterVibrateAndCompletionAction() {
  62. AudioServicesPlayAlertSound(mSoundId)
  63. AudioServicesAddSystemSoundCompletion(mSoundId, nil, nil, { (sounid, pointer) in
  64. AudioServicesPlayAlertSound(sounid)
  65. }, nil)
  66. }
  67.  
  68. //停止一切
  69. open func stopPlay() {
  70. AudioServicesRemoveSystemSoundCompletion(SystemSoundID(kSystemSoundID_Vibrate))
  71. AudioServicesRemoveSystemSoundCompletion(mSoundId)
  72. }
  73.  
  74. deinit {
  75. AudioServicesRemoveSystemSoundCompletion(SystemSoundID(kSystemSoundID_Vibrate))
  76. AudioServicesRemoveSystemSoundCompletion(mSoundId)
  77. AudioServicesDisposeSystemSoundID(SystemSoundID(kSystemSoundID_Vibrate))
  78. AudioServicesDisposeSystemSoundID(mSoundId)
  79. }
  80. }
Add Comment
Please, Sign In to add comment