ioscore-audioavaudiosessionaudiosession

iOS audio output only to headphone jack


Is it possible to output audio only to the iOS device's headphone jack and nowhere else? (e.g. not speakers nor bluetooth)

We're experimenting on a hardware add-on that receives input as tones from the audio jack. We could try detecting whether the headphone jack is connected, but that still leaves cases when there are bluetooth audio output devices are connected – we don't want the tones to be heard in the user's bluetooth headsets or speakers.


Solution

  • You need read AudioSessionProgrammingGuide

    You can get the current audio 'route' by calling AudioSessionGetProperty with the kAudioSessionProperty_AudioRoute property. This gives you a string such as "Headphone" or "Speaker" and play audio file receptively 'route' value

    CFStringRef route;
    UInt32 propertySize = sizeof(CFStringRef);
    AudioSessionInitialize(NULL, NULL, NULL, NULL);
    AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &route);
    
    NSLog(@"%@ %@",route,kAudioSessionInputRoute_Headphone);
    NSLog(@"%ld",CFStringGetLength(route));
    if ([(__bridge NSString *)route isEqualToString:@"Headphone"])
    {
        // play audio sound
    }
    else
    {
         // not play audio sound
    }
    

    I hope it help you