iPad 处理不当路线改变扬声器 ->耳机
我运行的是 iOS 4.3.2。
- 我在 iPad 上启动应用程序(未插入任何电源)。
- 音频作品。
- 我插上耳机。
- iPad 和耳机都没有声音。
- 我拔下耳机,转到 (2)
或者:
- 我在插入耳机的情况下在 iPad 上启动我的应用程序。
- 音频从耳机中发出。
- 我拔掉耳机。
- 现在音频从 iPad 中发出。
- 我插上耳机。
- iPad 和耳机都没有声音。
- 我再次拔下耳机,转到 (4)。
这是我的音频会话代码:
@implementation AudioSession
+ (void) CreateAudioSessionWithInterruptionListener: (AudioSessionInterruptionListener) interruptionListener
returningSampleRate: (Float64 *) pHWSampleRate
{
OSStatus result = AudioSessionInitialize(NULL, NULL, interruptionListener, self);
if (result != kAudioSessionNoError)
{
NSLog(@"AudioSessionInitialize(...) failed!");
return;
}
SET_PROPERTY( kAudioSessionProperty_AudioCategory, UInt32, kAudioSessionCategory_PlayAndRecord );
SET_PROPERTY( kAudioSessionProperty_OverrideCategoryMixWithOthers, UInt32, (UInt32) YES );
SET_PROPERTY( kAudioSessionProperty_PreferredHardwareIOBufferDuration, Float32, .005 );
// GET not set h/w sampleRate
// Float64 hwSampleRate;
// UInt32 size = sizeof(Float64);
AssertOK(AudioSessionGetProperty(
kAudioSessionProperty_CurrentHardwareSampleRate,
& (UInt32) { sizeof(Float64) },
pHWSampleRate),
@"couldn't get hw sample rate");
//NSAssert(size == sizeof(Float64);
NSLog(@"H/W SampleRate: %d", (int) (* pHWSampleRate));
AssertOK(AudioSessionSetActive(true),
@"couldn't set audio session active\n");
}
I'm running iOS 4.3.2.
- I start my app on my iPad (nothing plugged in).
- Audio works.
- I plug in the headphones.
- No audio, neither from the iPad, nor from the headphones.
- I unplug the headphones, go to (2)
Alternatively:
- I start my app on my iPad with the headphones plugged in.
- Audio comes out of the headphones.
- I unplug the headphones.
- Now audio comes out of the iPad.
- I plug in the headphones.
- No audio, neither from the iPad, nor from the headphones.
- I unlpug the headphones again, goto (4).
Here is my audio session code:
@implementation AudioSession
+ (void) CreateAudioSessionWithInterruptionListener: (AudioSessionInterruptionListener) interruptionListener
returningSampleRate: (Float64 *) pHWSampleRate
{
OSStatus result = AudioSessionInitialize(NULL, NULL, interruptionListener, self);
if (result != kAudioSessionNoError)
{
NSLog(@"AudioSessionInitialize(...) failed!");
return;
}
SET_PROPERTY( kAudioSessionProperty_AudioCategory, UInt32, kAudioSessionCategory_PlayAndRecord );
SET_PROPERTY( kAudioSessionProperty_OverrideCategoryMixWithOthers, UInt32, (UInt32) YES );
SET_PROPERTY( kAudioSessionProperty_PreferredHardwareIOBufferDuration, Float32, .005 );
// GET not set h/w sampleRate
// Float64 hwSampleRate;
// UInt32 size = sizeof(Float64);
AssertOK(AudioSessionGetProperty(
kAudioSessionProperty_CurrentHardwareSampleRate,
& (UInt32) { sizeof(Float64) },
pHWSampleRate),
@"couldn't get hw sample rate");
//NSAssert(size == sizeof(Float64);
NSLog(@"H/W SampleRate: %d", (int) (* pHWSampleRate));
AssertOK(AudioSessionSetActive(true),
@"couldn't set audio session active\n");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实上这可能是一个 iOS 错误。切换到 HDMI 输出(也是 iOS 4.3)时我遇到了同样的问题。在 50% 的情况下,路线更改会成功,并且音频会像预期那样从电视而不是 iPad 中发出。在另外 50% 的情况下,插入 HDMI 适配器后,我根本没有音频,无论是电视还是 iPad。再次拔下适配器后,我会立即恢复音频。
所以我不能真正为您提供一个好的解决方案,只能提供一个解决方案。注册音频路由更改(通过为属性 kAudioSessionProperty_AudioRouteChange 设置属性侦听器)。当此回调触发时,请处理它,例如通过重新初始化所有核心音频内容。这不是一个非常好的解决方案,但却是一个有效的解决方案。
更新:
在您的特殊情况下,系统的行为几乎与苹果所说的一样。让我引用 Apple 的 iOS 界面指南:
所以苹果说:从内置扬声器切换到耳机 ->播放继续。从耳机切换到内置扬声器 ->播放暂停。根据这句话,这是一种您应该自己实现的行为,而不是系统为您强制执行的行为,但它非常接近您实际看到的行为,不是吗?
This might be an iOS bug in fact. I have the same issue when switching to HDMI output (also iOS 4.3). In 50% of all cases, the route change succeeds and audio comes out of the television instead of the iPad, as expected. In the other 50% of all cases, after plugging in the HDMI adapter, I have no audio at all, neither television, nor iPad. I get audio back as soon as I unlpug the adapter again.
So I cannot really offer you a nice solution, only a work-a-round. Register for audio route changes (by setting a property listener for the property kAudioSessionProperty_AudioRouteChange). When this callback fires, handle it, e.g. by re-initializing all your Core Audio stuff. This is not a very nice solution, but one that works.
Update:
In your special case, the system behaves pretty much how Apple says it shall behave. Let me quote from Apple's Interface Guidelines for iOS:
So Apple says: Switching from internal speaker to headphone -> playback continues. Switching from headphone to internal speaker -> playback pauses. According to this quote this is a behavior you should implement on your own, not one that the system enforces for you, but it is pretty close to the behavior you are actually seeing, isn't it?