How to play Sound File In Silent Mode iPhone sdk?

2019-04-02 03:08发布

问题:

How to play Sound File In Silent Mode iPhone sdk ?

I m trying to play sound file in silent mode but result is zero

i have tried this code

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

when i import in header file

#import <AudioToolbox/AudioToolbox.h>

create Error like

error: expected identifier before '\x786f7073'

Asnwer As Soon As Possible.....

Thanks In advance

Regard

StupidiPhoneDeveloper

回答1:

you have to define an audio session category that is not silenced by the mute switch.

Check out the audio session page on apple dev site : http://developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/AudioSessionCategories/AudioSessionCategories.html%23//apple_ref/doc/uid/TP40007875-CH4-SW1

Maybe the AVAudioSessionCategoryPlayAndRecord is the one you need.



回答2:

Thanks Buddy for such a quick reply i have found the solution

By the following code you can check your iPhone Profile ( Regular/Silent) and here is the code

CFStringRef state; 
UInt32 propertySize = sizeof(CFStringRef); 
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);

if(CFStringGetLength(state) == 0) { 
    //SILENT
NSLog(@"Silent switch is on");

    //create vibrate
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    //this 2 line below use to play audio even in silent/vibrator mode too      

    UInt32 audioCategory = kAudioSessionCategory_MediaPlayback;
    AudioSessionSetProperty( kAudioSessionProperty_AudioCategory, sizeof(UInt32), &audioCategory);
}
else {
    //NOT SILENT
    NSLog(@"Silent switch is off");
}

And About this error

error: expected identifier before '\x786f7073'

Just write below line in every class header file

#import <AudioToolbox/AudioToolbox.h>