AudioServicesAddSystemSoundCompletion回调未在静默模式下调用

时间:2011-11-27 21:20:09

标签: iphone objective-c audio callback silent

如果我使用switch将设备置于静默模式,则不会调用AudioServicesAddSystemSoundCompletion回调方法。如果开关打开,我的意思是如果设备未处于静音模式,方法将被完美调用。

有没有人经历过这样的事情。这是一个错误吗?

3 个答案:

答案 0 :(得分:2)

我遇到了同样的问题。这不是一个bug。如果没有播放声音,由于设备被静音,呼叫将永远不会被呼叫。

解决方法是使用与要播放的声音长度相同的NSTimer。如果声音没有播放,则会调用定时器回叫。哪个代码可以执行与回调相同的代码。

答案 1 :(得分:2)

即使在静默模式下,您也可以使用NSTimer进行soundDidFinishPlaying回调。

    - (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;
}

答案 2 :(得分:0)

Sound Switch的源代码(带演示项目)表示如果设备处于静默模式,回调确实也会发生。

我刚刚在iPhone 5上尝试使用iOS 8.1,并使用设备上的按钮关闭/打开静音模式。

相关问题