AVAudioPlayer turns off iPod - how to work around?

后端 未结 1 1097
暗喜
暗喜 2020-12-06 03:23

I use AVAudioPlayer to play sounds in my app, but it turns off the iPod when a sound plays.

Is there a way to prevent this? I don\'t want to use System Sounds becaus

相关标签:
1条回答
  • 2020-12-06 03:47

    This should work for you (taken from my Ambiance app)

    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
    UInt32 doSetProperty = 1;
    AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
    [[AVAudioSession sharedInstance] setActive: YES error: nil];
    

    From the docs:

    kAudioSessionProperty_OverrideCategoryMixWithOthers Changes the mixing behavior of the kAudioSessionCategory_MediaPlayback and kAudioSessionCategory_PlayAndRecord audio session categories. Setting this property to TRUE (any nonzero value) allows mixing of iPod audio with application audio. Other aspects of these categories, such as their Ring/Silent switch behavior, are not affected.

    This property has value of FALSE (0) by default. When the audio session category changes, such as during an interruption, the value of this property reverts to FALSE. To regain mixing behavior you must then re-set this property.

    Always check to see if setting this property succeeds or fails, and react appropriately; behavior may change in future releases of iPhone OS.

    Available in iPhone OS 3.0 and later.

    Declared in AudioServices.h.

    0 讨论(0)
提交回复
热议问题