【问题标题】:How to Detetct Audio Jack is Plugged/not in my iPhone/iPad?如何检测音频插孔是否插入/未插入我的 iPhone/iPad?
【发布时间】:2013-12-20 12:18:18
【问题描述】:

我想检测我的 iPhone/iPad 设备中是否插入了音频插孔。
我已经尝试了许多代码,例如 stackOverflow 上可用的 C 函数,但它不起作用
告诉我与 iOS 5.0 到 7 兼容的代码

是否有任何教程/代码?然后告诉我

【问题讨论】:

    标签: ios iphone ios5 ios7 ios6.1


    【解决方案1】:

    经过大量谷歌搜索后,我找到了检测音频插孔的最佳且简单的解决方案

    首先在您的代码中添加以下方法

    - (BOOL)areHeadphonesPluggedIn {
        NSArray *availableOutputs = [[AVAudioSession sharedInstance] currentRoute].outputs;
        for (AVAudioSessionPortDescription *portDescription in availableOutputs) {
            NSLog(@"%@",portDescription.portType);
            if ([portDescription.portType isEqualToString:AVAudioSessionPortHeadphones]) {
                return YES;
            }
        }
        return NO;
    }
    

    然后像这样测试它

    if ([self areHeadphonesPluggedIn] == TRUE) {
        NSLog(@"Device Detetcted.  Headphone PluggedIn");
    }
    else {
        NSLog(@"Device Not Detetcted.  Headphone PluggedOut");
    }
    

    非常简单!

    【讨论】:

      猜你喜欢
      • 2011-08-19
      • 1970-01-01
      • 2012-11-15
      • 2013-05-08
      • 2017-04-24
      • 1970-01-01
      • 1970-01-01
      • 2011-04-04
      • 1970-01-01
      相关资源
      最近更新 更多