AVAudioSession bluetooth

AVAudioSession 蓝牙支持源码

增加AVAudioSession蓝牙的支持实际上比你想象的代码要少的多。事实上,整个audio session API很简单,以至于开发要么很容易要么异常困难。猜猜哪个分类AVAudioSession蓝牙支持集成?

据我所知增加蓝牙到AVAudioSession包括很多特性。让我们逐一展开:

打开bluetooth audio

首先,你需要明确地通知AVAudioSession你允许声音可以被输出到蓝牙设备。以下是设定你的声音类别:

[session setCategory:AVAudioSessionCategoryPlayAndRecord

         withOptions:AVAudioSessionCategoryOptionAllowBluetooth

               error:&error];

记住,你的audio session category 告诉iOS系统你将如何在你的App中应用音频信号。比如,

AVAudioSessionCategoryPlayAndRecord 意思是拥有播放和录音的功能。

总之,我们感兴趣的是可选的参数。

通过增加AVAudioSessionCategoryOptionAllowBluetooth, 我们允许iOS通过蓝牙音频设备来播放我们App的声音。

注:我已经见过在一个项目中多次设置了AVAudioSession类别。不要轻易地使用setCategory是我从iOS中学习到的关于audio的知识。

实际上,在大多数情况下,你只需要设置一次。为什么?首先它是一个复杂的操作。

如果它在主线程中被执行,它将使你失去一秒钟的UI响应。更重要的是,到处设置category将导致奇怪的行为-你将无法知道在什么时候audio将送达扬声器或耳麦……

在audio route中转换

当你在实现AVAudioSession蓝牙功能的时候,你可能希望可以切换音频设备。

可惜,在文档中似乎没有一个清晰的定义方法。

不过有很多方法来实现。然而,我找到的最可行的方法是设置preferred audio input.

以下是例子:

Switching to bluetooth

- (AVAudioSessionPortDescription*)bluetoothAudioDevice

{

    NSArray* bluetoothRoutes = @[AVAudioSessionPortBluetoothA2DP, AVAudioSessionPortBluetoothLE, AVAudioSessionPortBluetoothHFP];

    return [self audioDeviceFromTypes:bluetoothRoutes];

}

 

- (AVAudioSessionPortDescription*)builtinAudioDevice

{

    NSArray* builtinRoutes = @[AVAudioSessionPortBuiltInMic];

    return [self audioDeviceFromTypes:builtinRoutes];

}

 

- (AVAudioSessionPortDescription*)speakerAudioDevice

{

    NSArray* builtinRoutes = @[AVAudioSessionPortBuiltInSpeaker];

    return [self audioDeviceFromTypes:builtinRoutes];

}

 

- (AVAudioSessionPortDescription*)audioDeviceFromTypes:(NSArray*)types

{

    NSArray* routes = [[AVAudioSession sharedInstance] availableInputs];

    for (AVAudioSessionPortDescription* route in routes)

    {

        if ([types containsObject:route.portType])

        {

            return route;

        }

    }

    return nil;

}

 

- (BOOL)switchBluetooth:(BOOL)onOrOff

{

    NSError* audioError = nil;

    BOOL changeResult = NO;

    if (onOrOff == YES)

    {

        AVAudioSessionPortDescription* _bluetoothPort = [self bluetoothAudioDevice];

        changeResult = [[AVAudioSession sharedInstance] setPreferredInput:_bluetoothPort

                                                     error:&audioError];

    }

    else

    {

        AVAudioSessionPortDescription* builtinPort = [self builtinAudioDevice];

        changeResult = [[AVAudioSession sharedInstance] setPreferredInput:builtinPort

                                                     error:&audioError];

    }

    return changeResult;

}

额,有好多的代码!好吧,最重要的地方是最后一个方法中设置preferred input。其他代码仅仅是获得正确的面对扬声器,耳麦或蓝牙的AVAudioSessionPortDescription对象。

Switching to speaker

- (BOOL)switchSpeaker:(BOOL)onOrOff

{

    NSError* audioError = nil;

    BOOL changeResult = NO;

    if (onOrOff == YES)

    {

        changeResult = [[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker

                                                           error:&audioError];

    }

    else

    {

        AVAudioSessionPortDescription* builtinPort = [self builtinAudioDevice];

        changeResult = [[AVAudioSession sharedInstance] setPreferredInput:builtinPort

                                                                    error:&audioError];

    }

    return changeResult;

}

Switching to earpiece

- (BOOL)switchEarphone:(BOOL)onOrOff

{

    return [self switchSpeaker:!onOrOff];

}

这个很容易。仅仅是上面方法的相反操作。

AVAudioSession蓝牙支持是一个令人却步的工作。

而且在网上也没有太多介绍它的信息。

我希望这篇文章能够帮助到你。

 

援引:http://devmonologue.com/ios/tutorials/avaudiosession-bluetooth-support/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AVAudioSession是iOS中用于管理应用程序的音频会话的类。它允许应用程序控制和配置音频功能,如音频输入、输出、音频路由、音频处理等。AVAudioSession的目的是提供一个统一的接口,以便应用程序可以与设备的音频系统进行交互,无论是播放音乐、录制语音还是进行VoIP通话。 AVAudioSession有几个重要的概念和属性: 1. Category(分类):用于描述应用程序对音频的使用情况,如播放音乐、录制音频、播放通话等。不同的分类具有不同的优先级和行为。应用程序可以根据需要选择适当的分类,并在需要时进行切换。 2. Mode(模式):用于描述音频会话的详细行为。不同的模式针对不同的应用场景进行了优化,如游戏、音乐播放、通话等。应用程序可以根据需要设置适当的模式。 3. Route(路由):描述音频信号的路径,如扬声器、听筒、耳机等。应用程序可以查询当前的音频路由,并根据需要进行调整。 4. Options(选项):用于配置音频会话的其他参数,如混音、静音、中断处理等。 AVAudioSession还提供了一些其他的功能和方法,如设置音频输入、输出的硬件设备、监测耳机插拔事件、处理音频会话的中断和恢复等。 总之,AVAudioSession是一个强大的音频管理类,允许应用程序轻松控制和配置音频功能,并为各种应用场景提供了灵活的接口。通过使用AVAudioSession,开发者可以实现各种复杂的音频操作,提供优质的音频体验给用户。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值