当我的应用程序处于后台模式时,检测从 iPhone 插入或拔出耳机插孔

Posted

技术标签:

【中文标题】当我的应用程序处于后台模式时,检测从 iPhone 插入或拔出耳机插孔【英文标题】:Detect plugIn or unplug of headphone jack from iPhone when my app is in background mode 【发布时间】:2013-07-17 15:05:36 【问题描述】:

当我的应用程序处于background 模式时,我想在我的耳机插孔插入或从iPhone/iPod/iPad 拔出时通知用户。 这里我有在foreground 模式下检测到的代码。

- (void)viewDidLoad

    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

     AudiosessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, audioSessionPropertyListener, nil);


BOOL isHeadsetPluggedIn() 

    UInt32 routeSize = sizeof (CFStringRef);
    CFStringRef route;

    OSStatus error = AudioSessionGetProperty (kAudioSessionProperty_AudioRoute,
                                          &routeSize,
                                          &route
                                          );
    NSLog(@"%@", route);
    return (!error && (route != NULL) && ([( NSString*)route rangeOfString:@"Head"].location != NSNotFound));


void audioSessionPropertyListener(void* inClientData, AudioSessionPropertyID inID,UInt32 inDataSize, const void* inData)

    UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;

    // Determines the reason for the route change, to ensure that it is not
    //      because of a category change.
    CFDictionaryRef routeChangeDictionary = inData;
    CFNumberRef routeChangeReasonRef = CFDictionaryGetValue (routeChangeDictionary,CFSTR (kAudioSession_AudioRouteChangeKey_Reason));

    SInt32 routeChangeReason;
    CFNumberGetValue (routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason);

    // "Old device unavailable" indicates that a headset was unplugged, or that the
    //  device was removed from a dock connector that supports audio output.
    //    if (routeChangeReason != kAudioSessionRouteChangeReason_OldDeviceUnavailable)
    //        return;

    if (!isHeadsetPluggedIn())
    
        AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);

        NSLog(@"With out headPhone");
    
    else
    
        UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
        AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride);
        NSLog(@"headPhone");
    


【问题讨论】:

【参考方案1】:

您可以从 Apple Docs 中尝试:

- (void)applicationDidEnterBackground:(UIApplication *)application



    bgTask = [application beginBackgroundTaskWithExpirationHandler:^

        // Clean up any unfinished task business by marking where you

        // stopped or ending the task outright.

        [application endBackgroundTask:bgTask];

        bgTask = UIBackgroundTaskInvalid;

];



    // Start the long-running task and return immediately.

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^



        // Do the work associated with the task, preferably in chunks.



        [application endBackgroundTask:bgTask];

        bgTask = UIBackgroundTaskInvalid;

    );


我认为 Apple 在他们的文档中提供了这些数据:Background Execution and Multitasking

【讨论】:

以上是关于当我的应用程序处于后台模式时,检测从 iPhone 插入或拔出耳机插孔的主要内容,如果未能解决你的问题,请参考以下文章

当应用程序处于后台模式时,是不是可以在 iPhone 中闪烁手电筒?

如何在后台保持 iphone ios websocket 连接处于活动状态?

iPhone 休眠时无法检测到 Beacon 设备。(进入后台)

当 iphone 处于睡眠模式/锁定时,如何使用视频播放?

当我的应用程序处于后台或关闭时,当远程推送通知到达时从有效负载中获取数据

离子应用程序从后台打开