15
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

iOSのプラグにヘッドフォンがささっているか確認

Last updated at Posted at 2014-06-17

出回っている情報がわりと古くなっていて、アプリの起動時からイヤフォンがささっているとうまく認識されなかったりするので、最新の情報を共有しておきます。

- (BOOL)headphonesArePlugged
{
  AVAudioSession * session = [AVAudioSession sharedInstance];
  
  __block BOOL plugged = NO;
  
  AVAudioSessionRouteDescription * routeDesc =  session.currentRoute;
  [routeDesc.outputs enumerateObjectsUsingBlock:^(AVAudioSessionPortDescription * desc, NSUInteger idx, BOOL *stop){
    if ([desc.portType isEqualToString:AVAudioSessionPortHeadphones]) {
      plugged = YES;
    }
  }];
  return plugged;
}

これで端末にヘッドフォンがささっているかを取得できます。

15
14
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
15
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?