2

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

我正在尝试以静音模式播放声音文件,但结果为零

我试过这段代码

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

当我导入头文件时

#import <AudioToolbox/AudioToolbox.h>

像这样创建错误

错误:“\x786f7073”之前的预期标识符

尽快回答......

提前致谢

看待

愚蠢的iPhone开发者

4

2 回答 2

3

您必须定义一个不被静音开关静音的音频会话类别。

查看苹果开发网站上的音频会话页面:http: //developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/AudioSessionCategories/AudioSessionCategories.html%23//apple_ref/doc/uid/TP40007875 -CH4-SW1

也许 AVAudioSessionCategoryPlayAndRecord 是您需要的。

于 2010-12-01T08:58:54.813 回答
3

感谢好友这么快的回复,我找到了解决方案

通过以下代码,您可以检查您的 iPhone 配置文件(常规/静音),这是代码

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'

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

#import <AudioToolbox/AudioToolbox.h>
于 2010-12-01T09:04:59.717 回答