AudioServicesAddSystemSoundCompletion callback not

2019-05-11 14:02发布

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?

3条回答
贼婆χ
2楼-- · 2019-05-11 14:50

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.

查看更多
SAY GOODBYE
3楼-- · 2019-05-11 15:01

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.

查看更多
别忘想泡老子
4楼-- · 2019-05-11 15:03

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;
}
查看更多
登录 后发表回答