开发者

iPhone AudioServicesPlaySystemSound: route though headphones?

开发者 https://www.devze.com 2023-02-04 07:05 出处:网络
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 s

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号