LoginSignup
26

More than 5 years have passed since last update.

iOS6以降のAVAudioSessionのdelegate設定方法

Last updated at Posted at 2013-08-09

iOS 6以降AVAudioSessiondelegateプロパティがdeprecatedになったんだけど、その代わりにどうやって設定するのかあんまり情報がなかった。"notification送る"とは書いてあったんだけど、サンプルコードがなかったので、動作確認したコードを載せておく。

各イベントに対応する通知名があるので、それをNSNotificationCenterに登録しておけばいい。

AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)options
{
    AVAudioSession *session = [AVAudioSession sharedInstance];

    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(sessionDidInterrupt:) name:AVAudioSessionInterruptionNotification object:nil];
    [center addObserver:self selector:@selector(sessionRouteDidChange:) name:AVAudioSessionRouteChangeNotification object:nil];
}

- (void)sessionDidInterrupt:(NSNotification *)notification
{
    switch ([notification.userInfo[AVAudioSessionInterruptionTypeKey] intValue]) {
        case AVAudioSessionInterruptionTypeBegan:
            NSLog(@"Interruption began");
            break;
        case AVAudioSessionInterruptionTypeEnded:
        default:
            NSLog(@"Interruption ended");
            break;
    }
}

- (void)sessionRouteDidChange
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

参考

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
26