我知道,我必须将AudioSession设置为“播放”类别,即使静音开关打开,也允许音频。这就是我所做的,但是当开关打开的时候,声音还是会变得很安静。
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,sizeof(sessionCategory), &sessionCategory);
SystemSoundID soundID;
NSString *path = [[NSBundle mainBundle] pathForResource:soundString ofType:@"wav"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path],&soundID);
AudioServicesPlaySystemSound (soundID);编辑:顺便说一句,这个应用程序是一个音板。播放声音是应用程序的唯一目的。Apple对此的看法如下:
对于音频播放至关重要的应用程序,请使用此类别。您的音频播放,甚至在屏幕锁定和环/安静开关设置为静音。
编辑2:打开静音开关,声音甚至不会通过耳机播放。我知道用户是国王。我知道静音开关有它的用途。这不是问题所在。我试图得到这样一个事实的答案:将AudioSession类别设置为kAudioSessionCategory_MediaPlayback并不具有预期的结果。
编辑3:按照Jonathan的建议,我设置了AudioServices kAudioServicesPropertyIsUISound属性,但仍然没有成功。我是不是遗漏了什么?
// set the session property
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,sizeof(sessionCategory), &sessionCategory);
// creates soundID
SystemSoundID soundID;
NSString *path = [[NSBundle mainBundle] pathForResource:soundString ofType:@"wav"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path],&soundID);
// Jonathan Watmough suggestion
UInt32 flag = 0;
AudioServicesSetProperty(kAudioServicesPropertyIsUISound, sizeof(UInt32), &soundID, sizeof(UInt32), &flag);
AudioServicesPlaySystemSound (soundID);发布于 2010-06-26 17:23:41
再加上Jonathan的答案,实际上,似乎不可能让AudioServicesSystemSound覆盖静音开关。我最后所做的是使用OpenAL来播放声音,它将按照您指定的音频会话类别很好地播放。下面是我如何设置音频会话:
AudioSessionInitialize(NULL, NULL, NULL, NULL);
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,sizeof(sessionCategory), &sessionCategory);
AudioSessionSetActive(YES);为了播放声音,我使用了芬奇,一个简单的基于OpenAL的iPhone音效播放器。
发布于 2010-06-17 15:36:52
我还没有试过,但是您可能想要更改加载声音的属性:
kAudioServicesPropertyIsUISound a UInt32值,其中1表示对于在inSpecifier参数中传递的系统声音指定的音频文件,系统声音服务器尊重用户在声音效果首选项中的设置,并在用户关闭声音效果时保持沉默。默认情况下,此属性设置为1。将其设置为0,使系统声音在传递给AudioServicesPlaySystemSound时始终播放,而不考虑用户在声音首选项中的设置。可在iPhone OS2.0及更高版本中使用。在AudioServices.h中声明。
我希望做好其他事情后,将属性设置为0将告诉系统,您的声音是一个应用程序声音,而不是'UI‘声音,因此会受到静音的影响。
编辑:它看起来是无法用系统声音完成的。如果您通过OpenAL,显然您将能够播放静音按钮集。
我构建了SysSound示例,并添加了下面的代码来设置标志,就像上面所做的那样,它根本不对静音播放。
// Set the sound to always play
int setTo0 = 0;
AudioServicesSetProperty( kAudioServicesPropertyIsUISound,0,nil,
4,&setTo0 );发布于 2010-06-18 16:58:07
你打电话给setActive:错误:是为了AVAudioSession,对吗?下面是我用来初始化的代码。但我不是使用系统声音,而是使用AVAudioPlayer来播放声音。
avSession = [AVAudioSession sharedInstance]; // init session, important
// allow other audio to mix, such as iPod
const UInt32 categoryProp = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(categoryProp), &categoryProp);
UInt32 category = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(category), &category);
// Allow our audio to mix with any iPod audio that is playing
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(setProperty_YES), &setProperty_YES);
[avSession setActive:YES error:nil];https://stackoverflow.com/questions/3058507
复制相似问题