If I put device into silent mode using switch, AudioServicesAddSystemSoundCompletion
callback method doesn't get called. If the switch is on, I mean if device is NOT in silent mode, method gets called perfectly.
Has anyone experienced something like this. Is this a bug?
I had the same problem. It is not a bug. If the sound is not played, due to the device being muted the call back never gets called.
A work around is to use an NSTimer that is the same length as the sound to be played. If the sound doesn't play the timer call back gets called. Which can perform the same code as your callback.
Here is how you can use the NSTimer for soundDidFinishPlaying callback even in Silent Mode.
- (IBAction)playSelectedSound:(id)sender {
if (!self.isPlaying)
{
// playing the sound
NSString *fileName = [soundsFileNames objectAtIndex:self.selectedIndex];
SystemSoundID topClick;
NSBundle *bundle = [NSBundle mainBundle];
NSString *topClikFile = [bundle pathForResource:fileName ofType:@"aiff"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL URLWithString:topClikFile], &topClick);
AudioServicesPlaySystemSound(topClick);
// getting the file duration
AudioFileID audioFileID;
AudioFileOpenURL((__bridge CFURLRef)[NSURL URLWithString:topClikFile], kAudioFileReadPermission, 0, &audioFileID);
NSTimeInterval seconds;
UInt32 propertySize = sizeof(seconds);
OSStatus st = AudioFileGetProperty(audioFileID, kAudioFilePropertyEstimatedDuration, &propertySize, &seconds);
// fire the timer
if (st == 0)
{
[NSTimer scheduledTimerWithTimeInterval:seconds target:self selector:@selector(soundDidFinishPlaying) userInfo:nil repeats:NO];
}
self.isPlaying = YES;
}
}
- (void)soundDidFinishPlaying {
self.isPlaying = NO;
}
The source code for Sound Switch (with demo project) indicates that the callback does in fact occur also if the device is in silent mode.
I've just tried this with iOS 8.1 on an iPhone 5 and silent mode turned off/on with the button on the device.