1

当 iOS 睡眠定时器被调用时,我想停止我的音频应用程序。

就像潘多拉应用程序一样。
http://help.pandora.com/customer/portal/articles/24324-ios-sleep-timer-with-pandora

点击时钟应用程序,点击计时器,选择时间,点击计时器结束时,点击停止播放

如果 Pandora 应用程序正在运行,这将使它进入睡眠状态。

我可以看到 inInterruptionState == kAudioSessionBeginInterruption 在 iOS 睡眠定时器结束时被调用,但是我如何检测它是睡眠定时器还是只是像电话呼叫这样的中断?

这是我的代码。目前,我的应用程序在 iOS 睡眠定时器结束后才重新开始播放。

// Audio Interruption Listener
void MyInterruptionListener(void *inClientData, UInt32 inInterruptionState) {

    if (inInterruptionState == kAudioSessionBeginInterruption) {        
        [[DOSpeechManager sharedInstance] audioSessionBeginInterruption];
    }

    if (inInterruptionState == kAudioSessionEndInterruption) {
        [[DOSpeechManager sharedInstance] audioSessionEndInterruption];
    }

}

- (void)audioSessionBeginInterruption {

    if ([_MyAcaTTS isSpeaking] && [_MyAcaTTS isPaused] == NO) {

        [_MyAcaTTS pauseSpeakingAtBoundary:AcapelaSpeechImmediateBoundary];
        [self setAudioSettionStatus:NO];
        _audioInterruptedWhileSpeaking = YES;
    }
}

- (void)audioSessionEndInterruption {

    if (_audioInterruptedWhileSpeaking) {

        [self setAudioSettionStatus:YES];
        [_MyAcaTTS continueSpeaking];
    }
}

- (void)setAudioSettionStatus:(BOOL)status {
    AudioSessionSetActive(status);
    [_MyAcaTTS setActive:status];

    //cancel audio interrupted flag
    if (status) {
        _audioInterruptedWhileSpeaking = NO;
    }
}
4

1 回答 1

1

诀窍不是检测中断的来源,而是知道您的应用程序是否应该在中断后恢复。

音频会话中断时,AVAudioSession API 将发送通知。在此通知中,操作系统会给出有关应用程序是否应恢复播放的“提示”。

见下文:

    //Add notification observer
    __weak typeof(self) weakSelf = self;
    self.audioSessionInterruptionNotification =
    [[NSNotificationCenter defaultCenter] addObserverForName:AVAudioSessionInterruptionNotification
                                                      object:nil
                                                       queue:[NSOperationQueue mainQueue]
                                                  usingBlock:^(NSNotification *note) {
                                                      NSNumber* interruptionType = note.userInfo[AVAudioSessionInterruptionTypeKey];
                                                      NSNumber* interruptionOption = note.userInfo[AVAudioSessionInterruptionOptionKey];
                                                      BOOL shouldResume = interruptionOption.integerValue == AVAudioSessionInterruptionOptionShouldResume;

                                                      switch (interruptionType.integerValue) {
                                                          case AVAudioSessionInterruptionTypeBegan:
                                                              [weakSelf beginInterruption];
                                                              break;
                                                          case AVAudioSessionInterruptionTypeEnded:
                                                              [weakSelf endInterruption:shouldResume];
                                                              break;
                                                          default:
                                                              break;
                                                      }
                                                  }];
}
于 2014-01-24T23:18:28.377 回答