iphone, how to play sound even in silent or mute mode?

后端 未结 3 1746
执笔经年
执笔经年 2020-12-30 17:08

as topic... is it possible ?

Thanks

again, I have attached the code as follows, please check which step is wrong .thanks.

    //@step
AudioS         


        
相关标签:
3条回答
  • 2020-12-30 17:51

    It must be, there's been a couple games I've had which (even when its in mute mode) will play sound. Unfortunately this was discovered whilst attempting to play games covertly in class.

    As to how to actually do it, I really don't have any idea.

    0 讨论(0)
  • 2020-12-30 18:00

    If you look in the docs under Audio Session Categories, you'll find a number of modes that you can set to tell the system how your app plans to use audio. The default is AVAudioSessionCategorySoloAmbient which tracks the ring/silent switch and the screen lock.

    To have your app ignore the ring/silent switch settings, you could try changing the category:

    #import <AudioToolbox/AudioToolbox.h>
    
    AudioSessionInitialize (NULL, NULL, NULL, NULL);
    AudioSessionSetActive(true);
    
    UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
    AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, 
                             sizeof(sessionCategory),&sessionCategory);
    

    If you want to allow iPod audio to continue playing in the background, you'll also want to check kAudioSessionProperty_OverrideCategoryMixWithOthers.

    0 讨论(0)
  • 2020-12-30 18:14

    As of iOS 6, there is an alternative method that's more compact than Ramin's answer.

    #import <AVFoundation/AVFoundation.h>
    
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
                                           error:nil];
    

    To allow background audio from other apps to continue playing, add the AVAudioSessionCategoryOptionMixWithOthers option:

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
                                     withOptions:AVAudioSessionCategoryOptionMixWithOthers
                                           error:nil];
    

    There's further details in Apple's AVAudioSession Class Reference.

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