как остановить звук при вызове таймера сна iOS

Я хочу остановить свое аудиоприложение, когда вызывается таймер сна iOS.

Так же, как приложение Pandora.
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;
    }
}

person Non Umemoto    schedule 17.06.2013    source источник


Ответы (1)


Хитрость заключается не в том, чтобы обнаружить источник прерывания, а в том, чтобы узнать, должно ли ваше приложение возобновлять работу после прерывания.

API AVAudioSession отправит уведомление, когда аудиосеанс будет прерван. В этом уведомлении ОС дает «подсказку» о том, должно ли приложение возобновить воспроизведение или нет.

Увидеть ниже:

    //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;
                                                      }
                                                  }];
}
person doogilasovich    schedule 24.01.2014