2010-12-15 10 views
15

एक परीक्षण के दौरान, एक ग्राहक ने देखा कि आईफोन में वीडियो प्लेबैक रोकता है जब हेडफोन अनप्लग होते हैं। वह ऑडियो प्लेबैक के लिए समान कार्यक्षमता चाहता था, और शायद एक संदेश पॉप अप करने की क्षमता।क्या कोई घटना है जब हेडफ़ोन अनप्लग हो जाते हैं?

क्या किसी को पता है कि किसी प्रकार की घटना है, जिससे मैं इसे संभव बना सकता हूं?

उत्तर

18

ऑडियो सत्र प्रोग्रामिंग मार्गदर्शिका से Responding to Route Changes देखें।

-marc

+0

धन्यवाद! यह पृष्ठ, वहां से जुड़ा हुआ है, http://developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/Cookbook/Cookbook.html#//apple_ref/doc/uid/TP40007875-CH6-SW8। उदाहरण के लिए कोड भी है 'जब कोई उपयोगकर्ता हेडसेट प्लग या अनप्लग करता है या डिवाइस को अनदेखा करता है, तो सिस्टम कॉलबैक को आमंत्रित करता है-जिससे ऑडियो कनेक्शन जोड़ना या निकालना'। अभी तक इसे आजमाया नहीं है, लेकिन सही दिखता है। सिर के लिए चीयर्स। –

+0

बहुत बढ़िया :)। कॉलबैक के लिए कोई सिरदर्द – iEinstein

1

यहाँ पूर्ण कार्यान्वयन मैं अंत में जब हेडफ़ोन में खामियों को दूर कर रहे हैं (और अनप्लग) ईवेंट भेजने के लिए इस्तेमाल किया है।

पृष्ठभूमि से ऐप वापस आने के बाद भी काम करने के लिए आवश्यक जटिलता की एक उचित मात्रा थी।

CVAudioSession.h फ़ाइल

#import <Foundation/Foundation.h> 

#define kCVAudioInputChangedNotification @"kCVAudioInputChangedNotification" 
#define kCVAudioInterruptionEnded @"kCVAudioInterruptionEnded" 

@interface CVAudioSession : NSObject 
+(void) setup; 
+(void) destroy; 
+(NSString*) currentAudioRoute; 
+(BOOL) interrupted; 
@end 

CVAudioSession.m फ़ाइल

#import "CVAudioSession.h" 
#import <AudioToolbox/AudioToolbox.h> 

@implementation CVAudioSession 

static BOOL _isInterrupted = NO; 

+(void) setup { 
    NSLog(@"CVAudioSession setup"); 

    // Set up the audio session for recording 
    OSStatus error = AudioSessionInitialize(NULL, NULL, interruptionListener, (__bridge void*)self); 

    if (error) NSLog(@"ERROR INITIALIZING AUDIO SESSION! %ld\n", error); 
    if (!error) { 
     UInt32 category = kAudioSessionCategory_RecordAudio; // NOTE CANT PLAY BACK WITH THIS 
     error = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(category), &category); 
     if (error) NSLog(@"couldn't set audio category!"); 

     error = AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, propListener, (__bridge void*) self); 
     if (error) NSLog(@"ERROR ADDING AUDIO SESSION PROP LISTENER! %ld\n", error); 
     UInt32 inputAvailable = 0; 
     UInt32 size = sizeof(inputAvailable); 

     // we do not want to allow recording if input is not available 
     error = AudioSessionGetProperty(kAudioSessionProperty_AudioInputAvailable, &size, &inputAvailable); 
     if (error) NSLog(@"ERROR GETTING INPUT AVAILABILITY! %ld\n", error); 

     // we also need to listen to see if input availability changes 
     error = AudioSessionAddPropertyListener(kAudioSessionProperty_AudioInputAvailable, propListener, (__bridge void*) self); 
     if (error) NSLog(@"ERROR ADDING AUDIO SESSION PROP LISTENER! %ld\n", error); 

     error = AudioSessionSetActive(true); 
     if (error) NSLog(@"CVAudioSession: AudioSessionSetActive (true) failed"); 
    } 
} 

+ (NSString*) currentAudioRoute { 
    UInt32 routeSize = sizeof (CFStringRef); 
    CFStringRef route; 

    AudioSessionGetProperty (kAudioSessionProperty_AudioRoute, 
          &routeSize, 
          &route); 

    NSString* routeStr = (__bridge NSString*)route; 
    return routeStr; 
} 

+(void) destroy { 
    NSLog(@"CVAudioSession destroy"); 

    // Very important - remove the listeners, or we'll crash when audio routes etc change when we're no longer on screen 
    OSStatus stat = AudioSessionRemovePropertyListenerWithUserData(kAudioSessionProperty_AudioRouteChange, propListener, (__bridge void*)self); 
    NSLog(@".. AudioSessionRemovePropertyListener kAudioSessionProperty_AudioRouteChange returned %ld", stat); 

    stat = AudioSessionRemovePropertyListenerWithUserData(kAudioSessionProperty_AudioInputAvailable, propListener, (__bridge void*)self); 
    NSLog(@".. AudioSessionRemovePropertyListener kAudioSessionProperty_AudioInputAvailable returned %ld", stat); 

    AudioSessionSetActive(false); // disable audio session. 
    NSLog(@"AudioSession is now inactive"); 
} 

+(BOOL) interrupted { 
    return _isInterrupted; 
} 

// Called when audio is interrupted for whatever reason. NOTE: doesn't always call the END one.. 
void interruptionListener( void * inClientData, 
          UInt32 inInterruptionState) { 

    if (inInterruptionState == kAudioSessionBeginInterruption) 
    { 
     _isInterrupted = YES; 

     NSLog(@"CVAudioSession: interruptionListener kAudioSessionBeginInterruption. Disable audio session.."); 

     // Try just deactivating the audiosession.. 
     OSStatus rc = AudioSessionSetActive(false); 
     if (rc) { 
      NSLog(@"CVAudioSession: interruptionListener kAudioSessionBeginInterruption - AudioSessionSetActive(false) returned %.ld", rc); 
     } else { 
      NSLog(@"CVAudioSession: interruptionListener kAudioSessionBeginInterruption - AudioSessionSetActive(false) ok."); 
     } 



    } else if (inInterruptionState == kAudioSessionEndInterruption) { 

     _isInterrupted = NO; 

     // Reactivate the audiosession 
     OSStatus rc = AudioSessionSetActive(true); 
     if (rc) { 
      NSLog(@"CVAudioSession: interruptionListener kAudioSessionEndInterruption - AudioSessionSetActive(true) returned %.ld", rc); 
     } else { 
      NSLog(@"CVAudioSession: interruptionListener kAudioSessionEndInterruption - AudioSessionSetActive(true) ok."); 
     } 

     [[NSNotificationCenter defaultCenter] postNotificationName:kCVAudioInterruptionEnded object:(__bridge NSObject*)inClientData userInfo:nil]; 
    } 
} 

// This is called when microphone or other audio devices are plugged in and out. Is on the main thread 
void propListener( void *     inClientData, 
        AudioSessionPropertyID inID, 
        UInt32     inDataSize, 
        const void *   inData) 
{ 
    if (inID == kAudioSessionProperty_AudioRouteChange) 
    { 
     CFDictionaryRef routeDictionary = (CFDictionaryRef)inData; 
     CFNumberRef reason = (CFNumberRef)CFDictionaryGetValue(routeDictionary, CFSTR(kAudioSession_AudioRouteChangeKey_Reason)); 
     SInt32 reasonVal; 
     CFNumberGetValue(reason, kCFNumberSInt32Type, &reasonVal); 
     if (reasonVal != kAudioSessionRouteChangeReason_CategoryChange) 
     { 
      NSLog(@"CVAudioSession: input changed"); 
      [[NSNotificationCenter defaultCenter] postNotificationName:kCVAudioInputChangedNotification object:(__bridge NSObject*)inClientData userInfo:nil]; 
     } 
    } 
    else if (inID == kAudioSessionProperty_AudioInputAvailable) 
    { 
     if (inDataSize == sizeof(UInt32)) { 
      UInt32 isAvailable = *(UInt32*)inData; 

      if (isAvailable == 0) { 
       NSLog(@"AUDIO RECORDING IS NOT AVAILABLE"); 
      } 
     } 
    } 
} 

@end 
5

यह iOS 7 बदल गया है, तो आप सिर्फ AVAudioSessionRouteChangeNotification

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioRouteChanged:) name:AVAudioSessionRouteChangeNotification object:nil]; 
+0

यह वीडियो प्लेयर के साथ काम करता है, लेकिन केवल तभी यदि आप 'AVAudioSession' का उपयोग करते हैं। – Legoless

2

स्विफ्ट नामित अधिसूचना को सुनने के लिए की जरूरत है 3.0 @ सांपियोइल का समाधान:

NotificationCenter.default.addObserver(self, selector: #selector(YourViewController.yourMethodThatShouldBeCalledOnChange), name: NSNotification.Name.AVAudioSessionRouteChange, object: nil) 
+0

वह शानदार है! आपका बहुत बहुत धन्यवाद! –