我正在运行iOS 4.3.2。
可替换地:
这是我的音频会话代码:
@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");
}
答案 0 :(得分:1)
这实际上可能是iOS错误。切换到HDMI输出(也是iOS 4.3)时,我遇到了同样的问题。在50%的情况下,路线变化成功,音频从电视而不是iPad出来,正如预期的那样。在其他50%的情况下,插入HDMI适配器后,我根本没有音频,既没有电视,也没有iPad。一旦我再次取消适配器,我就会收回音频。
所以我不能真正为你提供一个很好的解决方案,只有一个圆满的工作。注册音频路由更改(通过为属性kAudioSessionProperty_AudioRouteChange设置属性侦听器)。当这个回调触发时,处理它,例如,通过重新初始化所有Core Audio内容。这不是一个非常好的解决方案,但有效。
<强>更新强>
在您的特殊情况下,系统的行为几乎与苹果公司所说的一样。让我引用Apple的iOS界面指南:
播放的情况类似,但结果不同,如图所示 在图的右侧。如果用户在此期间拔下耳机 播放时,你的回调应暂停音频。如果用户插入 播放过程中的耳机,您的回调应该只允许播放 继续。
Apple说:从内置扬声器切换到耳机 - &gt;播放继续。从耳机切换到内置扬声器 - &gt;播放暂停。根据这句话,这是你应该自己实现的行为,而不是系统为你强制实施的行为,但它与你实际看到的行为非常接近,不是吗?