gpt4 book ai didi

ios - 无法控制 iPad 上的扬声器

转载 作者:可可西里 更新时间:2023-11-01 03:08:16 25 4
gpt4 key购买 nike

我遵循了关注我的扬声器状态的方法:

AudioSession.h

enum {
kAudioSessionOverrideAudioRoute_None = 0,
kAudioSessionOverrideAudioRoute_Speaker = 'spkr'
};

我的类(class)

@synthesize speakerEnabled;

...

- (void)setSpeakerEnabled:(BOOL)enable {
speakerEnabled = enable;
if(enable) {
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute
, sizeof (audioRouteOverride)
, &audioRouteOverride);
} else {
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_None;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute
, sizeof (audioRouteOverride)
, &audioRouteOverride);
}
}

但是它仅适用于 iPhone,对于 iPad - 没有任何反应。

  • 当我按下按钮:Speaker On 时,我进入 if(enable) 并且 AudioSessionSetProperty 收到 kAudioSessionOverrideAudioRoute_Speaker ;

  • 当我按下按钮:Speaker Off 时,我进入 else 并且 AudioSessionSetProperty 收到 kAudioSessionOverrideAudioRoute_None;

我开始调试,但没有发现设备之间的区别。

我有 iPad2 iOS 6.1。

我错过了什么吗?

请帮帮我

编辑

正如 LombaX 所说,我在启动应用程序时添加了 AVAudioSession 类别:

NSError *err = nil;
BOOL success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&err];

if(!success){
[MyLogger logc:MyLoggerLog format:"%@",[err localizedDescription]];
}

成功 = 是

还是不行。

最佳答案

我认为您误解了此属性的用途。您希望启用/禁用扬声器。这不是 kAudioSessionProperty_OverrideAudioRoute 的意图。相反,它会以相当有限的方式影响输出声音路由

这些是 iPhone 可用的各种可能的输出路径

extern const CFStringRef kAudioSessionOutputRoute_LineOut           
extern const CFStringRef kAudioSessionOutputRoute_Headphones
extern const CFStringRef kAudioSessionOutputRoute_BluetoothHFP
extern const CFStringRef kAudioSessionOutputRoute_BluetoothA2DP
extern const CFStringRef kAudioSessionOutputRoute_BuiltInReceiver
extern const CFStringRef kAudioSessionOutputRoute_BuiltInSpeaker
extern const CFStringRef kAudioSessionOutputRoute_USBAudio
extern const CFStringRef kAudioSessionOutputRoute_HDMI
extern const CFStringRef kAudioSessionOutputRoute_AirPlay

这些只是可能的路线 - 实际可用的路线取决于上下文。 Apple 严格限制您在应用程序内确定这些路线的能力,因为用户需要以设备一致的方式控制这些路线。它们中的大多数是由用户插入/拔出硬件(耳机、USB、HDMI、线路输出)隐式确定的,Apple 不希望您的应用在这里扰乱用户的期望。

如果媒体上下文正确(并且 airplay 可用),可以使用 MPVolumeViewrouteButton 选择 Airplay。蓝牙可以由 OverrideCategoryEnableBluetoothInput ( which controls both input and output )

引导

请特别注意,kAudioSessionOutputRoute_BuiltInReceiver 是您在打电话时放在耳边的 iPhone 上的低电平扬声器。如果没有插入外部设备(例如耳机),这是 iPhone 的默认音频输出路径。kAudioSessionOutputRoute_BuiltInSpeaker 是手机底部的“免提”扬声器。

您可以通过设置以下覆盖属性之一从当前默认值重新路由到此 BuiltInSpeaker:

key: kAudioSessionProperty_OverrideAudioRoute
values: kAudioSessionOverrideAudioRoute_Speaker
: kAudioSessionOverrideAudioRoute_None

Specifies whether or not to override the audio session category’s normal audio route.

key: kAudioSessionProperty_OverrideCategoryDefaultToSpeaker
values: TRUE
: FALSE

Specifies whether or not to route audio to the speaker (instead of to the receiver) when no other audio route, such as a headset, is connected.

这两个仅设计用于 kAudioSessionCategory_PlayAndRecord Audio Session 类别。

请注意,在这两种情况下,您都没有在任何输出路径中进行选择,您只是覆盖了“默认路径”以支持内置(大声)扬声器。

没有电话的 iPad 没有 BuiltInReceiver 类型的扬声器。在没有连接的小工具或 airplay 的情况下,它的默认路径是完全相同的 BuiltInSpeaker。因此,重写没有任何效果。

假设您真的想在您的应用中使音频静音,您如何实现这一点取决于您的应用设计的许多其他方面。如果您想使设备静音,Apple 宁愿用户通过响铃/静音开关来控制它。似乎他们不会有任何其他方式:

I've had a response from Apple on this. They've said they don't and never have provided a method for detecting hardware mute switch and don't intend to do so. https://stackoverflow.com/a/8009073/1375695

扬声器设置是对设备的覆盖,而不是对给定声音的覆盖
http://lists.apple.com/archives/coreaudio-api/2009/Mar/msg00300.html

关于ios - 无法控制 iPad 上的扬声器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16230831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com