如何在iPad应用程序中切换扬声器和耳机

时间:2013-11-27 11:17:30

标签: ios objective-c ipad avaudiosession

我正在编写一个将音频切换到扬声器的功能:

UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);

但是当我尝试使用它将其切换回耳机时:

UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_None;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);

此代码适用于iPhone但在iPad中不能从扬声器切换到耳机,任何一个指南有什么不同吗?

1 个答案:

答案 0 :(得分:0)

尝试将UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback更改为UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord