【问题标题】:Detecting inputs and outputs after app moves to background应用程序移至后台后检测输入和输出
【发布时间】:2015-01-31 01:38:24
【问题描述】:

我正在尝试检测headphones 何时插入 iPhone。我这样做是购买以下方法。

//Init the AVAudioSession
-(void)viewDidLoad
{
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&error];    
    NSError *activationError = nil;

    [[AVAudioSession sharedInstance] setActive: YES error: &activationError];
}

//Get the current audio session route
-(void)viewWillAppear
{
    AVAudioSessionRouteDescription  *route = [[AVAudioSession sharedInstance] currentRoute];
}

这很好用,route.inputsroute.outputs 将包含有线 iPhone 的 headphonesmicrophone headset,并在拔下耳机时显示内置接收器和内置麦克风。我遇到的问题是当我按下我的应用程序上的主页按钮时,它会移动到后台。当我将应用程序带回前台时, route.inputs 始终为空。我的问题是:当调用applicaionWillResign active 时,我是否需要在我的appDelegate 中设置[[AVAudioSession sharedInstance] setActive:NO error: &activiationError]?因为当 route.inputs 返回空时,耳机仍然插入并且应该被检测到。任何帮助,将不胜感激。谢谢。

【问题讨论】:

    标签: ios objective-c iphone


    【解决方案1】:

    我认为更好的方法是:

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

    我刚刚对此进行了测试,即使从后台返回前台,也可以在 viewDiDLoad 上运行

    链接到原始Answer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多