LoginSignup
1
1

More than 5 years have passed since last update.

[iOS] 電話やSiriによる割り込み時の制御

Last updated at Posted at 2017-04-23

アプリ内で音楽再生や録音中に通話やアラーム、Siriなどによる割り込みがあった場合の制御方法です。
これを行う事でAudioPlayerで音楽の再生中に割り込みが発生した後に、再度再生させるなどの処理を実装することが可能です。

実装方法

以下のNotificationを登録することで、割り込み時に通知を受け取る事ができます。

// 割り込みが発生した場合の通知を登録
[[NSNotificationCenter defaultCenter] addObserver:self                                          
                                         selector:@selector(audioInterrupted:)
                                             name:AVAudioSessionInterruptionNotification
                                           object:nil];

割り込み発生時の処理内で、割り込み開始か終了かを取得し処理を行います。
割り込みの開始/終了はAVAudioSessionInterruptionTypeKeyから取得することができます。

- (void)audioInterrupted:(NSNotification*) notification{
    NSNumber *interruptType = [notification.userInfo objectForKey:@"AVAudioSessionInterruptionTypeKey"];
    if ([interruptType unsignedIntegerValue] == AVAudioSessionInterruptionTypeBegan){
        //割り込み開始
    }else if([interruptType unsignedIntegerValue] == AVAudioSessionInterruptionTypeEnded){
        //割り込み終了
    }
}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1