首页 文章

AVAudioPlayer和AVAudioSession不会在iPad上播放BlueTooth立体声(2或其他)

提问于
浏览
8

我有一个播放录制音频和重复声音的应用程序 . 通过板载iPad扬声器可以正常播放声音,如果我从耳机插孔插入一根线到我的立体声音频输入,它也可以很好地播放 . 当我将iPad与我的蓝牙立体声输入配对时,来自我的其他应用程序(为iPhone编写,在我的iPad上运行)的所有声音都能正常工作,就像我设备上的所有其他声音一样 .

The problem is my app written for iPad is NOT playing over the bluetooth path, but instead plays from the built-in speakers.

在我的didFinishLaunchingWithOptions(...)方法的app委托中,我放置了以下内容:

NSError *error = nil;
[[AVAudioSession sharedInstance] setMode:AVAudioSessionModeDefault error:&error];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
[[AVAudioSession sharedInstance] setActive:YES error:&error];

正在调用此代码,并且不会返回任何错误 .

在我的控制器代码中,我记录了使用AVAudioPlayer播放的样本,如下所示:

audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:recordURL error:&error];
audioPlayer.numberOfLoops = 0;
[audioPlayer setDelegate:self];
[audioPlayer play];

在其他方面,我的无人机播放时间短 . 在线程控制的循环中重复播放.01秒声音,我使用OpenAL执行此操作:

alSourcePlay(sourceID);

这与我为iPhone编写的其他应用程序中的代码完全相同 .

我意识到还有其他关于蓝牙输入的线程,但我有一个特定的问题,我的iPad应用程序的音频声音的蓝牙输出 .

4 回答

  • 2

    不可能 .

    从非常有趣的Apple doc AVAudioSession - 选择麦克风QA1799:

    如果应用程序使用setPreferredInput:error:方法选择蓝牙HFP输入,则输出将自动更改为蓝牙HFP输出 . 此外,使用MPVolumeView的路由选择器选择蓝牙HFP输出将自动将输入更改为蓝牙HFP输入 . 因此,即使仅单独设置输入或输出,输入和输出也将始终在蓝牙HFP设备上结束 .

  • 2

    由于您的类别是播放和录制,因此您必须启用蓝牙作为输入才能将其作为输出支持(默认情况下,相同的接收器用于播放和录制模式下的输入/输出) . 为此,您必须在AVAudioSession上设置一个额外的属性:

    UInt32 allowBluetoothInput = 1;
    AudioSessionSetProperty (
                             kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,
                             sizeof (allowBluetoothInput),
                             &allowBluetoothInput
                            );
    

    您还需要检查是否通过在会话中设置 kAudioSessionProperty_OverrideCategoryDefaultToSpeaker 属性来强制输出到代码中任何位置的内置扬声器 .

  • 1

    这是目前的解决方案,但已被弃用,任何人都知道新的解决方案,但现在在您的应用程序上添加这部分代码,并且一切正常!

    UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
    AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    

    希望这对你有所帮助!

  • 1

    你用setOategory withOptions检查了吗?它从iOS 6开始

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:&error];
    

相关问题