How to play Sound File In Silent Mode iPhone sdk?

有些话、适合烂在心里 提交于 2019-12-05 06:47:25

问题


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>


来源:https://stackoverflow.com/questions/4322351/how-to-play-sound-file-in-silent-mode-iphone-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!