3

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

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

4

3 回答 3

2

我有同样的问题。这不是一个错误。如果没有播放声音,由于设备被静音,回调永远不会被调用。

一种解决方法是使用与要播放的声音长度相同的 NSTimer。如果声音不播放,则调用计时器回调。它可以执行与您的回调相同的代码。

于 2012-01-26T20:04:31.293 回答
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;
}
于 2012-03-24T23:03:07.213 回答
0

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

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

于 2015-01-26T10:04:12.007 回答