PlaySystemSound与静音开关 [英] PlaySystemSound with mute switch on

查看:711
本文介绍了PlaySystemSound与静音开关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,我必须将 AudioSession 设置为播放类别,允许音频即使在静音开关已打开。这是我做的,但声音,当开关处于打开状态还是被静音。

  UInt32的sessionCategory = kAudioSessionCategory_MediaPlayback;
 AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,sizeof的(sessionCategory),放大器; sessionCategory); SystemSoundID soundID;
 * NSString的路径= [[一个NSBundle mainBundle] pathForResource:soundString ofType:@WAV]; AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:路径],&安培; soundID);
 AudioServicesPlaySystemSound(soundID);

结果的修改:顺便说一句,该应用程序是一个soundpad。播放声音是应用程序的唯一目的。下面是苹果的文件说一下:


  

使用此类别的应用程序
  其音频播放是首要
  重要性。您的音频播放,即使
  屏幕锁定,并与
  响铃/静音开关设定为静音。


结果的编辑2 :对,声音甚至不会通过耳机播放的静音开关。 我知道,用户为王。我知道静音开关有其目的。这不是问题。我试图让一个事实,即 AudioSession 类别设置为 kAudioSessionCategory_MediaPlayback 没有预期的答案结果。

结果修改3 :以下乔纳森Watmough的建议下,我设置了 AudioServices kAudioServicesPropertyIsUISound 财产,但仍然没有运气。我缺少的东西吗?

  //设置会话属性
UInt32的sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,sizeof的(sessionCategory),放大器; sessionCategory);//创建soundID
SystemSoundID soundID;
* NSString的路径= [[一个NSBundle mainBundle] pathForResource:soundString ofType:@WAV];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:路径],&安培; soundID);//乔纳森Watmough建议
UInt32的标志= 0;
AudioServicesSetProperty(kAudioServicesPropertyIsUISound,sizeof的(UInt32的),放大器; soundID,sizeof的(UInt32的),放大器;标志);AudioServicesPlaySystemSound(soundID);


解决方案

好上添加乔纳森Watmough的回答,事实上,它似乎并不可能让AudioServicesSystemSound覆盖静音开关。我落得这样做使用的OpenAL来播放声音,将发挥就好了以下指定的音频会话类别。下面是我设置了音频会话:

  AudioSessionInitialize(NULL,NULL,NULL,NULL);
UInt32的sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,sizeof的(sessionCategory),放大器; sessionCategory);
AudioSessionSetActive(YES);

要播放声音,我用芬奇,一个简单的基于OpenAL的声音效果播放器的iPhone。

I know, I have to set the AudioSession to the 'playback' category, which allows audio even when the mute switch is on. This is what I do, but sound still gets muted when switch is on.

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


EDIT: by the way, the app is a soundpad. Playing sound is the sole purpose of the app. Here's what Apple Doc says about this:

Use this category for an application whose audio playback is of primary importance. Your audio plays even with the screen locked and with the Ring/Silent switch set to silent.


EDIT 2: with the mute switch on, sound wont even play through the headphones. I know the user is king. I know the mute switch has its purpose. That is not the question. I'm trying to get an answer on the fact that setting the AudioSession category to kAudioSessionCategory_MediaPlayback doesn't have the expected result.


EDIT 3: following Jonathan Watmough's suggestion, I set the AudioServices kAudioServicesPropertyIsUISound property, but still no luck. Am I missing something?

// 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);

解决方案

Well to add on Jonathan Watmough's answer, indeed it doesn't seem possible to let AudioServicesSystemSound overwrite the mute switch. What I ended up doing is using OpenAL to play the sounds, which will play just fine following the Audio Session category you specify. Here's how I setup the Audio session:

AudioSessionInitialize(NULL, NULL, NULL, NULL);
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,sizeof(sessionCategory), &sessionCategory);
AudioSessionSetActive(YES);

To play the sounds, I used Finch, a simple OpenAL-based sound effect player for iPhone.

这篇关于PlaySystemSound与静音开关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆