3

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?

erkanyildiz
  • 13,044
  • 6
  • 50
  • 73

3 Answers3

2

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.

casillic
  • 1,837
  • 1
  • 24
  • 29
  • Do we have any proof that this is not bug, but a feature. I could not find any reference document about this issue. – erkanyildiz Jan 27 '12 at 13:52
  • No, But the very name of the function indicates the sound must complete before it is called. Since the sound never plays it could never complete hence it is working per design and in my opinion (for what that worth) is not a bug. I wish there was additional functions to handle this situation. – casillic Jan 28 '12 at 22:43
  • I don't think it's proper to ignore audio playing instructions when the device is muted via switch. If you decrease the sound level to 0 using volume down button (i mean, practically mute the device), nothing changes and method gets called properly. If Apple thought like "if there is nothing to hear, no audio playing necessary", why haven't they done the same thing for the volume buttons. – erkanyildiz Jan 29 '12 at 00:47
  • I think these function out of System Sound Service are more geared towards Shorts Sounds and vibrations, but vibration is key. If you look at it from the vibration perspective it make sense. Muting the device stops vibrations as well as sound. Lowering the volume doesn't stop vibration only sound. The docs also clearly indicate it does not support level control which implies on / off operation only. Any way at least there is a work around for the desired operation. – casillic Jan 29 '12 at 01:27
2

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;
}
grigorievs
  • 162
  • 6
  • On iOS 7.1 it does not work. soundDidFinishPlaying is called anyway. – Pavel May 25 '14 at 17:01
  • @Pavel "soundDidFinishPlaying" is a custom selector. – grigorievs May 28 '14 at 12:01
  • Thanks. I see. Probably, I didn't understand your post. I tried the way you proposed. The callback set in AudioServicesAddSystemSoundCompletion is called in 7.1. The time is wrong - immediately for the silent mode. – Pavel May 28 '14 at 15:06
0

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.

Drux
  • 11,992
  • 13
  • 66
  • 116