且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在静音模式下播放声音文件iPhone sdk?

更新时间:2022-02-10 22:18:42

感谢Buddy快速回复
i已找到解决方案

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

通过以下代码,您可以查看您的iPhone配置文件(常规/沉默)
,这里是代码

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");
}

关于此错误

error: expected identifier before '\x786f7073'

只需在每个类头文件中写下以下行

Just write below line in every class header file

#import <AudioToolbox/AudioToolbox.h>