Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

obliviga

macrumors newbie
Original poster
Jul 30, 2014
1
0
I have a UISwitch in my application. When it is switched ON, the phone vibrates and emits a sound until switched OFF. Currently, I can get the application to vibrate and emit the sound when switched ON, however when the switch is OFF, only the vibrate stops. I am able to get another sound to play when the switch is turned OFF, but the original sound persists. An idea I have is to replace the currently playing sound with a short sound when the switch is toggled OFF. Anyone have any ideas on how I can accomplish this?

Code:
- (IBAction)toggleVibrate:(id)sender {
static dispatch_queue_t my_queue = nil;

SystemSoundID noSound;
NSString *path2 = [[NSBundle mainBundle] pathForResource:@"noSound" ofType:@"wav"];
AudioServicesCreateSystemSoundID((CFURLRef)CFBridgingRetain([NSURL fileURLWithPath: path2]), &noSound);

SystemSoundID mySSID;
NSString *path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"wav"];
AudioServicesCreateSystemSoundID((CFURLRef)CFBridgingRetain([NSURL fileURLWithPath: path]), &mySSID);

if(my_queue == nil)
my_queue = dispatch_queue_create("com.obliviga.vibrate_queue", NULL);

if([sender isOn]){
// vibrate until toggled
dispatch_async(my_queue, ^{
while([sender isOn]) {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
AudioServicesPlaySystemSound(mySSID);
};
});


}
if([sender isOn] == NO) {
AudioServicesDisposeSystemSoundID(mySSID);
AudioServicesPlaySystemSound(noSound);
}
}

Thank you!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.