【问题标题】:Detecting if headset are plugged into iOS device检测耳机是否已插入 iOS 设备
【发布时间】:2012-05-27 22:58:24
【问题描述】:

ios 设备上有互动电影。当电影开始时(点击),视频开头的人会问你插入耳机,如果插入,那么视频应该自动跳转到故事(直接进入视频故事)。我该怎么办?以及如何编写代码?

【问题讨论】:

标签: ios xcode headphones


【解决方案1】:

首先,您必须注册 AudioRoute Changes:-

AudioSessionAddPropertyListener (kAudioSessionProperty_AudioRouteChange,
                                     audioRouteChangeListenerCallback,
                                 self);

在这里您可以描述更改路线的原因:-

CFDictionaryRef routeChangeDictionary = inPropertyValue;

  CFNumberRef routeChangeReasonRef =
  CFDictionaryGetValue (routeChangeDictionary,
                        CFSTR (kAudioSession_AudioRouteChangeKey_Reason));

  SInt32 routeChangeReason;

      CFNumberGetValue (routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason);

  if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) 
  {
       // your statements for headset unplugged

  }
  if (routeChangeReason == kAudioSessionRouteChangeReason_NewDeviceAvailable)
  {
       // your statements for headset plugged                             
  }

【讨论】:

  • 如果用户开始(点击)已经插入耳机怎么办?这是否意味着设置将默认设置?我的意思是,我可以写 [routeChangeReason == kAudioSessionRouteChangeReason_HeadsetUnavailable]...吗?
  • routeChangeReason == kAudioSessionRouteChangeReason_NewDeviceAvailable 我已经声明了这个声明插入耳机。
  • 谢谢,但我的意思是这个算法:如果最初拔掉电源,然后在 5 秒后播放 A 播放 B(因为可能有人没有耳机),如果按照上面的要求插入耳机,则直接播放 B . 如果在录制视频之前最初插入耳机,则直接播放 B. 如果在播放过程中拔掉插头,则转到外部扬声器。
  • 如何注册 AudoRoute Changes?
【解决方案2】:

这可能是另一种方式:

CFStringRef newRoute;
size = sizeof(CFStringRef);
XThrowIfError(AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &size, &newRoute), "couldn't get new audio route");
if (newRoute)
{
    CFShow(newRoute);
    if (CFStringCompare(newRoute, CFSTR("HeadsetInOut"), NULL) == kCFCompareEqualTo) // headset plugged in
          {
...
          }
    else if (CFStringCompare(newRoute, CFSTR("SpeakerAndMicrophone"), NULL) == kCFCompareEqualTo){
     ....
     }
}

【讨论】:

    【解决方案3】:

    首先检查设备是否连接到任何耳机。

    +(BOOL)isHeadsetPluggedIn {
    
    AVAudioSessionRouteDescription* route = [[AVAudioSession sharedInstance] currentRoute];
    for (AVAudioSessionPortDescription* desc in [route outputs]) {
        if ([[desc portType] isEqualToString:AVAudioSessionPortHeadphones])
            return YES;
    }
    return NO;
    }
    

    然后根据bool值,写出自己的条件。像下面这样..

    if (isHeadphonesConnected) {
        //Write your own code here
    }else{
    
    }
    

    您还可以注册一个通知,以防您想知道当您在屏幕中时耳机是否被移除。

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(audioRoutingListenerCallback:)
                                            name:AVAudioSessionRouteChangeNotification
                                               object:nil];
    
    - (void)audioRoutingListenerCallback:(NSNotification*)notification
    {
        NSDictionary *interuptionDict = notification.userInfo;
    
        NSInteger routeChangeReason = [[interuptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];
    
        switch (routeChangeReason) {
    
            case AVAudioSessionRouteChangeReasonNewDeviceAvailable:
    
                NSLog(@"Headphone/Line plugged in");
    
                /*Write your own condition.*/
    
                break;
    
            case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
    
                NSLog(@"Headphone/Line was pulled.");
    
                /*Write your own condition.*/                
                break;
    
            case AVAudioSessionRouteChangeReasonCategoryChange:
                // called at start - also when other audio wants to play
    
                break;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-04-17
      • 2011-09-09
      • 2011-04-13
      • 2011-04-04
      • 1970-01-01
      • 2017-04-24
      • 2015-05-09
      • 1970-01-01
      • 2016-02-25
      相关资源
      最近更新 更多