【问题标题】:how to stop audio when iOS Sleep Timer gets called调用iOS睡眠定时器时如何停止音频
【发布时间】:2013-06-12 22:23:06
【问题描述】:

我想在 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;
    }
}

【问题讨论】:

    标签: iphone ios audio timer audiosession


    【解决方案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;
                                                          }
                                                      }];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-09
      • 2012-06-20
      • 1970-01-01
      • 1970-01-01
      • 2011-06-02
      • 2019-12-22
      • 2012-08-23
      相关资源
      最近更新 更多