Skip to content

Commit

Permalink
- Sound Manager added which is needed to play various system sounds.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeeshan Haider committed Mar 10, 2017
1 parent d1138cc commit 6f5a292
Show file tree
Hide file tree
Showing 3 changed files with 332 additions and 0 deletions.
12 changes: 12 additions & 0 deletions SampleProject.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
952CF3801C8581B600CB80F6 /* VideoLaunchScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 952CF37F1C8581B600CB80F6 /* VideoLaunchScreen.swift */; };
952CF3881C8597C000CB80F6 /* SampleStorageHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 952CF3871C8597C000CB80F6 /* SampleStorageHelper.m */; };
952CF38C1C85C3AF00CB80F6 /* SPButton.m in Sources */ = {isa = PBXBuildFile; fileRef = 952CF38B1C85C3AF00CB80F6 /* SPButton.m */; };
9543841C1E72CAF9008F453B /* SoundManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9543841B1E72CAF9008F453B /* SoundManager.swift */; };
95470E871BF5A19C00AEA056 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95470E861BF5A19C00AEA056 /* ViewController.swift */; };
955FA9141C8431040020027A /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8EBD2CB31B566A6B00741E4B /* Constants.swift */; };
955FA95D1C845F4E0020027A /* UIView+ParentCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 955FA91B1C845F4E0020027A /* UIView+ParentCell.m */; };
Expand Down Expand Up @@ -158,6 +159,7 @@
952CF3871C8597C000CB80F6 /* SampleStorageHelper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SampleStorageHelper.m; sourceTree = "<group>"; };
952CF38A1C85C3AF00CB80F6 /* SPButton.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPButton.h; sourceTree = "<group>"; };
952CF38B1C85C3AF00CB80F6 /* SPButton.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPButton.m; sourceTree = "<group>"; };
9543841B1E72CAF9008F453B /* SoundManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SoundManager.swift; sourceTree = "<group>"; };
95470E861BF5A19C00AEA056 /* ViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ViewController.swift; path = "SampleProject/Classes/View Controllers/ViewController.swift"; sourceTree = SOURCE_ROOT; };
955FA91A1C845F4E0020027A /* UIView+ParentCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIView+ParentCell.h"; sourceTree = "<group>"; };
955FA91B1C845F4E0020027A /* UIView+ParentCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIView+ParentCell.m"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -331,6 +333,7 @@
7785037D1BA986380068185E /* Helper */ = {
isa = PBXGroup;
children = (
9543841A1E72CAB5008F453B /* SoundManager */,
95D0E5291E4451AB007160C5 /* MOProfile.swift */,
95D0E52A1E4451AB007160C5 /* UserManager */,
9582DD641E2518D90098B33D /* loggerswift.swift */,
Expand Down Expand Up @@ -548,6 +551,14 @@
path = UIButton;
sourceTree = "<group>";
};
9543841A1E72CAB5008F453B /* SoundManager */ = {
isa = PBXGroup;
children = (
9543841B1E72CAF9008F453B /* SoundManager.swift */,
);
path = SoundManager;
sourceTree = "<group>";
};
95470E7A1BF45DB700AEA056 /* Subclasses */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -965,6 +976,7 @@
95A8BB2D1E26547400A0F461 /* ArrayExtensions.swift in Sources */,
95D0E5321E4457CC007160C5 /* SPSegmentControl.swift in Sources */,
777CE0CA1E3659B300261DFC /* TextField.swift in Sources */,
9543841C1E72CAF9008F453B /* SoundManager.swift in Sources */,
777CE0EF1E3666CA00261DFC /* OverlayViewCell.swift in Sources */,
955FA9691C845F4E0020027A /* UICollectionView+Helper.m in Sources */,
955FA96F1C845F4E0020027A /* UILabel+Helper.m in Sources */,
Expand Down
Binary file not shown.
320 changes: 320 additions & 0 deletions SampleProject/Classes/Helper/SoundManager/SoundManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,320 @@
//
// SoundManager.swift
// SampleProject
//
// Created by Zeeshan Haider on 10/03/2017.
// Copyright © 2017 XYZco. All rights reserved.
//

import Foundation
import AudioToolbox

enum SMError: Error {
case kAudioServicesNoError
}

// MARK: - SoundManager struct

/// This struct adds some useful functions to play system sounds.
public class SoundManager {
// MARK: - Variables

/// Audio IDs enum.
///
/// More info [here](http://iphonedevwiki.net/index.php/AudioServices).
///
/// - NewMail: New Mail.
/// - MailSent: Mail Sent.
/// - VoiceMail: Voice Mail.
/// - RecivedMessage: Recived Message.
/// - SentMessage: Sent Message.
/// - Alarm: Alarm.
/// - LowPower: Low Power.
/// - SMSReceived1: SMS Received 1.
/// - SMSReceived2: SMS Received 2.
/// - SMSReceived3: SMS Received 3.
/// - SMSReceived4: SMS Received 4.
/// - SMSReceived5: SMS Received 5.
/// - SMSReceived6: SMS Received 6.
/// - TweetSent: Tweet Sent.
/// - Anticipate: Anticipate.
/// - Bloom: Bloom.
/// - Calypso: Calypso.
/// - ChooChoo: Choo Choo.
/// - Descent: Descent.
/// - Fanfare: Fanfare.
/// - Ladder: Ladder.
/// - Minuet: Minuet.
/// - NewsFlash: News Flash.
/// - Noir: Noir.
/// - SherwoodForest: Sherwood Forest.
/// - Spell: Spell.
/// - Suspence: Suspence.
/// - Telegraph: Telegraph.
/// - Tiptoes: Tiptoes.
/// - Typewriters: Typewriters.
/// - Update: Update.
/// - USSDAlert: USSD Alert.
/// - SIMToolkitCallDropped: SIM Toolkit Call Dropped.
/// - SIMToolkitGeneralBeep: SIM Toolkit General Beep.
/// - SIMToolkitNegativeACK: SIM Toolkit Negative ACK.
/// - SIMToolkitPositiveACK: SIM Toolkit Positive ACK.
/// - SIMToolkitSMS: SIM Toolkit SMS.
/// - Tink: Tink.
/// - CTBusy: CT Busy.
/// - CTCongestion: CT Congestion.
/// - CTPathACK: CT Path ACK.
/// - CTError: CT Error.
/// - CTCallWaiting: CT Call Waiting.
/// - CTKeytone: CT Keytone.
/// - Lock: Lock.
/// - Unlock: Unlock.
/// - FailedUnlock: Failed Unlock.
/// - KeypressedTink: Keypressed Tink.
/// - KeypressedTock: Keypressed Tock.
/// - Tock: Tock.
/// - BeepBeep: Beep Beep.
/// - RingerCharged: Ringer Charged.
/// - PhotoShutter: Photo Shutter.
/// - Shake: Shake.
/// - JBLBegin: JBL Begin.
/// - JBLConfirm: JBL Confirm.
/// - JBLCancel: JBL Cancel.
/// - BeginRecording: Begin Recording.
/// - EndRecording: End Recording.
/// - JBLAmbiguous: JBL Ambiguous.
/// - JBLNoMatch: JBL No Match.
/// - BeginVideoRecord: Begin Video Record.
/// - EndVideoRecord: End Video Record.
/// - VCInvitationAccepted: VC Invitation Accepted.
/// - VCRinging: VC Ringing.
/// - VCEnded: VC Ended.
/// - VCCallWaiting: VC Call Waiting.
/// - VCCallUpgrade: VC Call Upgrade.
/// - TouchTone1: Touch Tone 1.
/// - TouchTone2: Touch Tone 2.
/// - TouchTone3: Touch Tone 3.
/// - TouchTone4: Touch Tone 4.
/// - TouchTone5: Touch Tone 5.
/// - TouchTone6: Touch Tone 6.
/// - TouchTone7: Touch Tone 7.
/// - TouchTone8: Touch Tone 8.
/// - TouchTone9: Touch Tone 9.
/// - TouchTone10: Touch Tone 10.
/// - TouchToneStar: Touch Tone Star.
/// - TouchTonePound: Touch Tone Pound.
/// - HeadsetStartCall: Headset Start Call.
/// - HeadsetRedial: Headset Redial.
/// - HeadsetAnswerCall: Headset Answer Call.
/// - HeadsetEndCall: Headset End Call.
/// - HeadsetCallWaitingActions: Headset Call Waiting Actions.
/// - HeadsetTransitionEnd: Headset Transition End.
/// - Voicemail: Voicemail.
/// - ReceivedMessage: Received Message.
/// - NewMail2: New Mail 2.
/// - MailSent2: Mail Sent 2.
/// - Alarm2: Alarm 2.
/// - Lock2: Lock 2.
/// - Tock2: Tock 2.
/// - SMSReceived1_2: SMS Received 1_2.
/// - SMSReceived2_2: SMS Received 2_2.
/// - SMSReceived3_2: SMS Received 3_2.
/// - SMSReceived4_2: SMS Received 4_2.
/// - SMSReceivedVibrate: SMS Received Vibrate.
/// - SMSReceived1_3: SMS Received 1_3.
/// - SMSReceived5_3: SMS Received 5_3.
/// - SMSReceived6_3: SMS Received 6_3.
/// - Voicemail2: Voicemail 2.
/// - Anticipate2: Anticipate 2.
/// - Bloom2: Bloom 2.
/// - Calypso2: Calypso 2.
/// - ChooChoo2: Choo Choo 2.
/// - Descent2: Descent 2.
/// - Fanfare2: Fanfare 2.
/// - Ladder2: Ladder 2.
/// - Minuet2: Minuet 2.
/// - NewsFlash2: News Flash 2.
/// - Noir2: Noir 2.
/// - SherwoodForest2: Sherwood Forest 2.
/// - Spell2: Spell 2.
/// - Suspence2: Suspence 2.
/// - Telegraph2: Telegraph 2.
/// - Tiptoes2: Tiptoes 2.
/// - Typewriters2: Typewriters 2.
/// - Update2: Update 2.
/// - RingerVibeChanged: Ringer Vibe Changed.
/// - SilentVibeChanged: Silent Vibe Changed.
/// - Vibrate: Vibrate.
public enum AudioID: UInt32 {
case newMail = 1000
case mailSent = 1001
case voiceMail = 1002
case recivedMessage = 1003
case sentMessage = 1004
case alarm = 1005
case lowPower = 1006
case smsReceived1 = 1007
case smsReceived2 = 1008
case smsReceived3 = 1009
case smsReceived4 = 1010
case smsReceived5 = 1013
case smsReceived6 = 1014
case tweetSent = 1016
case anticipate = 1020
case bloom = 1021
case calypso = 1022
case chooChoo = 1023
case descent = 1024
case fanfare = 1025
case ladder = 1026
case minuet = 1027
case newsFlash = 1028
case noir = 1029
case sherwoodForest = 1030
case spell = 1031
case suspence = 1032
case telegraph = 1033
case tiptoes = 1034
case typewriters = 1035
case update = 1036
case ussdAlert = 1050
case simToolkitCallDropped = 1051
case simToolkitGeneralBeep = 1052
case simToolkitNegativeACK = 1053
case simToolkitPositiveACK = 1054
case simToolkitSMS = 1055
case tink = 1057
case ctBusy = 1070
case ctCongestion = 1071
case ctPathACK = 1072
case ctError = 1073
case ctCallWaiting = 1074
case ctKeytone = 1075
case lock = 1100
case unlock = 1101
case failedUnlock = 1102
case keypressedTink = 1103
case keypressedTock = 1104
case tock = 1105
case beepBeep = 1106
case ringerCharged = 1107
case photoShutter = 1108
case shake = 1109
case jblBegin = 1110
case jblConfirm = 1111
case jblCancel = 1112
case beginRecording = 1113
case endRecording = 1114
case jblAmbiguous = 1115
case jblNoMatch = 1116
case beginVideoRecord = 1117
case endVideoRecord = 1118
case vcInvitationAccepted = 1150
case vcRinging = 1151
case vcEnded = 1152
case vcCallWaiting = 1153
case vcCallUpgrade = 1154
case touchTone1 = 1200
case touchTone2 = 1201
case touchTone3 = 1202
case touchTone4 = 1203
case touchTone5 = 1204
case touchTone6 = 1205
case touchTone7 = 1206
case touchTone8 = 1207
case touchTone9 = 1208
case touchTone10 = 1209
case touchToneStar = 1210
case touchTonePound = 1211
case headsetStartCall = 1254
case headsetRedial = 1255
case headsetAnswerCall = 1256
case headsetEndCall = 1257
case headsetCallWaitingActions = 1258
case headsetTransitionEnd = 1259
case voicemail = 1300
case receivedMessage = 1301
case newMail2 = 1302
case mailSent2 = 1303
case alarm2 = 1304
case lock2 = 1305
case tock2 = 1306
case smsReceived1_2 = 1307
case smsReceived2_2 = 1308
case smsReceived3_2 = 1309
case smsReceived4_2 = 1310
case smsReceivedVibrate = 1311
case smsReceived1_3 = 1312
case smsReceived5_3 = 1313
case smsReceived6_3 = 1314
case voicemail2 = 1315
case anticipate2 = 1320
case bloom2 = 1321
case calypso2 = 1322
case chooChoo2 = 1323
case descent2 = 1324
case fanfare2 = 1325
case ladder2 = 1326
case minuet2 = 1327
case newsFlash2 = 1328
case noir2 = 1329
case sherwoodForest2 = 1330
case spell2 = 1331
case suspence2 = 1332
case telegraph2 = 1333
case tiptoes2 = 1334
case typewriters2 = 1335
case update2 = 1336
case ringerVibeChanged = 1350
case silentVibeChanged = 1351
case vibrate = 4095
}

// MARK: - Functions -

/// Play a system sound from the ID.
///
/// - Parameter audioID: ID of system audio from the AudioID enum.
public static func playSystemSound(audioID: AudioID) {
AudioServicesPlaySystemSound(SystemSoundID(audioID.rawValue))
}

/// Play system sound vibrate.
public static func vibrate() {
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
}

/// Play custom sound with url.
///
/// - Parameter soundURL: Sound URL.
/// - Returns: Returns the SystemSoundID.
/// - Throws: Throws Error error.
public static func playSound(soundURL: URL) throws -> SystemSoundID {
var soundID: SystemSoundID = 0

let error: OSStatus = AudioServicesCreateSystemSoundID(soundURL as CFURL, &soundID)
if error != Int32(kAudioServicesNoError) {
throw SMError.kAudioServicesNoError
}
return soundID
}

/// Dispose custom sound.
///
/// - Parameter soundID: SystemSoundID.
/// - Returns: Returns true if has been disposed, otherwise false.
public static func disposeSound(soundID: SystemSoundID) throws {
let error: OSStatus = AudioServicesDisposeSystemSoundID(soundID)
if error != Int32(kAudioServicesNoError) {
throw SMError.kAudioServicesNoError
}
}
}

/*
USAGE

SoundManager.vibrate()
SoundManager.playSystemSound(audioID: .receivedMessage)

*/

0 comments on commit 6f5a292

Please sign in to comment.