AudioServicesAddSystemSoundCompletion callback not getting called in silent mode

 ̄綄美尐妖づ 提交于 2019-12-10 09:52:00

问题


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?


回答1:


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.




回答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;
}



回答3:


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.



来源:https://stackoverflow.com/questions/8289141/audioservicesaddsystemsoundcompletion-callback-not-getting-called-in-silent-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!