我尝试使用这些方法试图检测Ring/Silent开关是否处于活动状态:
但是在我的iPhone 4上,"状态"值始终是"扬声器"(并且CFStringGetLength(状态)返回的长度值始终为7).有没有人成功使用过这种方法?如果是这样,关于什么样的设备和SDK版本?
我这样称呼它:
- (BOOL)deviceIsSilenced {
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
OSStatus audioStatus = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
if (audioStatus == kAudioSessionNoError) {
NSLog(@"audio route: %@", state) // "Speaker" regardless of silent switch setting, but "Headphone" when my headphones are plugged in
return (CFStringGetLength(state) <= 0);
}
return NO;
}
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
audioSession.delegate = self;
[audioSession setCategory:AVAudioSessionCategoryAmbient error:nil];
[audioSession setActive:YES error:nil];
NSLog(@"muted? %i", [self deviceIsSilenced]);
...
}
Run Code Online (Sandbox Code Playgroud)
我想当手机上的物理开关被切换时,可能会触发其他一些(更准确的)kAudioSessionProperty事件.有人有主意吗?
顺便说一句,我正在使用AVAudioSessionCategoryAmbient类别和我的[AVAudioSession …
我正在开发一个应用程序.因为我想通过编码检测"iPhone是否处于静音模式?".我正在使用Objective-C的可可开发它.
如果有人知道它善意回复.
目前,我将音量设置为最大值,然后检查音量是否为最大值或更低值.如果它的值较低,则用户必须在其系统上具有音量锁定.
这工作正常,但我想知道是否有一些方法可以调用,或者属性检查哪个在代码中免费告诉我这个?我一直在网上看到如何做到这一点,但我似乎找不到任何东西.提前致谢!
编辑:事实证明,我之前设置最大音量的方法然后检查它是否更低以查看是否存在音量锁定在设备上不起作用.似乎音量与音量锁定一致,而不仅仅是被切断.
现在我完全坚持这个.是否有任何私人方法或属性可以用来检测这个?
我有一个项目,我必须记录来自蓝牙耳机的声音并使用默认的iPhone扬声器播放.我搜索了很多并得到了这段代码.
UInt32 allowBluetoothInput = 1;
AudioSessionSetProperty (
kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,
sizeof (allowBluetoothInput),
&allowBluetoothInput
);
Run Code Online (Sandbox Code Playgroud)
------------音频录音机启动和停止代码------------
- (IBAction)Record: (id)sender
{
UIButton *btn = (UIButton *)sender;
if([btn isSelected])
{
[audioRecorder stop];
[btn setSelected:NO];
[btn setTitle:@"Start Recording" forState:UIControlStateNormal];
}
else
{
[audioRecorder record];
[btn setSelected:YES];
[btn setTitle:@"Stop Recording" forState:UIControlStateNormal];
}
}
Run Code Online (Sandbox Code Playgroud)
我在此之后使用avaudiorecorder.我在这里似乎还有其他一些东西.
--------录音机代码---------
NSURL *soundFileURL = [NSURL fileURLWithPath:AUDIO_FILE];
NSDictionary *recordSettings = [NSDictionary
dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMin],
AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16],
AVEncoderBitRateKey,
[NSNumber numberWithInt: 2],
AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0],
AVSampleRateKey,
nil];
NSError *error = nil;
audioRecorder = [[AVAudioRecorder alloc] …Run Code Online (Sandbox Code Playgroud) 我想在前台,后台播放音频文件,它应该与静音开关一起工作,即如果静音开关打开则不能播放,如果静音开关关闭则应播放音频.
**基本上我正在开发SIP呼叫应用程序.当用户接到电话时,应该播放声音/铃声.它应该在app处于后台/前台时播放,如果硬件静音开关为ON/OFF,它应该静音/取消静音.
为此,我使用了AVPlyaer以下代码.
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:&error];
[session setActive:YES error:nil];
NSURL * audioFileUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test1" ofType:@"mp3"]];
AVPlayer *player = [[AVPlayer alloc] initWithURL: audioFileUrl];
[player play];
Run Code Online (Sandbox Code Playgroud)
此外,我还在info.plist中添加了"App播放音频或使用AirPlay传输音频/视频"到后台模式
这是在两种模式下播放,但在硬件静音开关打开时不静音.如果我使用"AVAudioSessionCategoryAmbient"不在后台模式上播放.我使用AVAudioPlayer但在硬件开关打开时无法找到静音
请帮我修复它或任何其他方法来实现这一目标.提前致谢.
我已经构建了一个计时器应用程序,我遇到的一个问题是当它背景时,如果用户关闭音量,我无法响铃.关闭音量或声音也会使通知静音,这是我用于在后台响铃的方法.
我刚买了一块瓷砖,发现它可以在静音时给你的手机打电话.我已经测试了它,它可以在iOS 9中运行,但我不确定如何复制这种行为.
iPhone如何在静音模式下响铃?后台刷新?动议和活动?别的什么?
现有技术:
可能重复:
以编程方式静音/静音iOS设备?
我需要以编程方式关闭设备卷.有谁知道吗?
到目前为止,我发现,也许我可以使用AudioSessionSetProperty()函数,并使用属性"kAudioSessionProperty_CurrentHardwareOutputVolume",但此属性只是只读.所以我不确定这是否有效:
float value = 0.0f;
AudioSessionSetProperty(kAudioSessionProperty_CurrentHardwareOutputVolume, sizeof(float), &value);
Run Code Online (Sandbox Code Playgroud)
App-Store中已有应用程序执行此操作,例如AutoSilent:
iphone ×6
ios ×5
audio ×3
ios4 ×2
objective-c ×2
audio-player ×1
bluetooth ×1
cocoa-touch ×1
ios5 ×1
ipad ×1
mute ×1
permissions ×1
settings ×1
uikit ×1
volume ×1