4

I'm having trouble using AudioServicesPlaySystemSound. Things work great when output goes through the speakers. However, when a user plugs in headphones, there is no output. Is there an easy way to set up some kind of listener so that audio is automatically routed through the headphones when they are plugged in, and otherwise through the speaker?

I'm using the following method to play short, simple AIF sound samples:

-(void)playAif:(NSString *)filename {
    SystemSoundID soundID;
    NSString *path = [[NSBundle mainBundle]
       pathForResource:filename ofType:@"aif"];    

    if (path) { // test for path, to guard against crashes

    AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path],&soundID);
    AudioServicesPlaySystemSound (soundID);

        }
   }

I know I must be missing something, some bit of setup that would do this. Any ideas?

Chris Ladd
  • 2,795
  • 1
  • 29
  • 22
  • 1
    Check the documentation on AudioSessions -http://developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/Configuration/Configuration.html#//apple_ref/doc/uid/TP40007875-CH3-SW1 – Till Jan 16 '11 at 16:59
  • 1
    @Till thanks for teaching me how to fish! I'd checked the docs before, but missed the relevant info. Updating the question below with the solution for posterity. Thanks! – Chris Ladd Jan 16 '11 at 18:02

1 Answers1

4

Thanks @Till for pointing me to the relevant portion of the docs. For others with this issue, the solution is to explicitly set the session category, in my case to ambient sound. This code snipped from apple's docs:

    UInt32 sessionCategory = kAudioSessionCategory_AmbientSound;    // 1

    AudioSessionSetProperty (
                             kAudioSessionProperty_AudioCategory,                        // 2
                             sizeof (sessionCategory),                                   // 3
                             &sessionCategory                                            // 4
                             );

So, my method for playing audio now looks like this:

    -(void)playAif:(NSString *)filename {
    //  NSLog(@"play: %@", filename);

        SystemSoundID soundID;
        NSString *path = [[NSBundle mainBundle]
                          pathForResource:filename ofType:@"aif"];    


        if (path) { // test for path, to guard against crashes

            UInt32 sessionCategory = kAudioSessionCategory_AmbientSound;    // 1

            AudioSessionSetProperty (
                                     kAudioSessionProperty_AudioCategory,                        // 2
                                     sizeof (sessionCategory),                                   // 3
                                     &sessionCategory                                            // 4
                                     );

            AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path],&soundID);
            AudioServicesPlaySystemSound (soundID);

        }
    }

This solves my problem handily! The only worry I have is that setting it explicitly every single time I play a sound might be excessive. Anyone know a better and safer way to set it and forget it? Otherwise, this works delightfully.

Chris Ladd
  • 2,795
  • 1
  • 29
  • 22