iphone - 如何在调用 iOS sleep 定时器时停止音频

标签 iphone ios audio timer audiosession

我想在调用 iOS sleep 定时器时停止我的音频应用。

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

Tap the Clock app, Tap Timer, Select a time, Tap When Timer Ends, Tap Stop Playing

This will sleep your Pandora app if it is running.

我可以看到 inInterruptionState == kAudioSessionBeginInterruption 在 iOS sleep 定时器结束时被调用,但我如何检测它是 sleep 定时器还是只是电话调用之类的中断?

这是我的代码。 目前,我的应用程序只是在 iOS sleep 定时器结束后再次开始播放。

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

最佳答案

诀窍不是检测中断源,而是了解您的应用程序是否应在中断后恢复。

AVAudioSession API 会在 Audio Session 中断时发送通知。在此通知中,操作系统会就应用是否应恢复播放给出“提示”。

见下文:

    //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;
                                                      }
                                                  }];
}

关于iphone - 如何在调用 iOS sleep 定时器时停止音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17139486/

相关文章:

iphone - 适用于 iPhone 的文本转语音库

iphone - 如何比较当前和手动 NSDate

iphone - 如何捕获其他iPhone应用程序的屏幕截图?

javascript - 我无法使用此JS代码播放音频。语法正确,但是吗?

c++ - 配置 AVCodecContext 结构以从原始 PCM 编码为 u-law

windows-phone-7 - 显示UVC Windows Phone 7

iphone - NSObject 上的类别——保证它的安全

ios - SpriteKit 中的 CGAffineTransform

iphone - 将 2 个 UIImage 合并为一个,其中一个被旋转

ios - 使用 Core Graphics 从外部存储加载图像无法正常工作 iOS 13