【问题标题】:Detect plugIn or unplug of headphone jack from iPhone when my app is in background mode当我的应用程序处于后台模式时,检测从 iPhone 插入或拔出耳机插孔
【发布时间】: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");
    }

}

【问题讨论】:

    标签: iphone ios headphones


    【解决方案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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-14
      • 2013-05-01
      • 2013-11-11
      • 2017-04-24
      • 2014-06-02
      • 2014-12-30
      • 2013-01-20
      • 2016-06-10
      相关资源
      最近更新 更多