列出可用的输出音频目标AVAudioSession [英] List available output audio target AVAudioSession

查看:306
本文介绍了列出可用的输出音频目标AVAudioSession的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要列出可用于iOS应用程序的音频输出.我的问题与此类似:如何列出可用的音频输出在iOS上路由

I need to list the audio outputs available to an iOS application. My question is similar to this one: How to list available audio output route on iOS

我尝试了以下代码:

NSError *setCategoryError = nil;
BOOL success = [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback
                                                      error: &setCategoryError];

NSError *activationError = nil;
[[AVAudioSession sharedInstance] setActive: YES error: &activationError];

…
NSLog(@"session.currentRoute.outputs count %d", [[[[AVAudioSession sharedInstance] currentRoute] outputs ] count]);
for (AVAudioSessionPortDescription *portDesc in [[[AVAudioSession sharedInstance] currentRoute] outputs ]) {
    NSLog(@"-----");
    NSLog(@"portDesc UID %@", portDesc.UID);
    NSLog(@"portDesc portName %@", portDesc.portName);
    NSLog(@"portDesc portType %@", portDesc.portType);
    NSLog(@"portDesc channels %@", portDesc.channels);
}

但是,我总是只看到一个输出端口(计数为1),如果我有两个(Airplay和内置扬声器),我也总是看到. 如果我使用音乐"应用程序,则可以看到两个端口并在它们之间切换. 在我的应用中,我只会看到我选择的一个.

However I always see just one output port (the count is 1), also if I have two (an Airplay and a Built-in speaker). If I use the Music application I am able to see both ports and switch between them. In my app I only see the one that I have selected.

我还需要做些其他事情吗?

There is something else that I need to do?

谢谢

我也尝试过此代码:

CFDictionaryRef asCFType = nil;
UInt32 dataSize = sizeof(asCFType);
AudioSessionGetProperty(kAudioSessionProperty_AudioRouteDescription, &dataSize, &asCFType);
NSDictionary *audioRoutesDesc = (__bridge NSDictionary *)asCFType;
NSLog(@"audioRoutesDesc %@", audioRoutesDesc);

,但是字典仅列出一个输出目标.此外,输入源数组为空(我有iPhone 4s)

but the dictionary list just one output destinations. Moreover the input sources array is empty (I have a iPhone 4s)

使用MPVolumeView可以正常工作.该组件具有一个按钮,可让您选择输出音频路线,例如在Music App中.

I got something working using MPVolumeView . This component has a button that lets you choose the output audio route, like in the Music App.

如果需要,您可以使用以下方法隐藏滑块(仅包含按钮):

If you want you can hide the slider (and have only the button) using:

self.myMPVolumeView.showsVolumeSlider = NO;

推荐答案

这将取决于您的AVAudioSession类别.

It will depend on your AVAudioSession category.

您可以安全地假设在iPhone上至少有一个麦克风作为输入而一个扬声器作为输出.如果您想获取蓝牙/AirPlay输出的列表,首先必须确保您的会话类别正在向您报告它们:

You can safely assume on an iPhone that you have at least a microphone as input and a speaker as output. If you're trying to get a list of Bluetooth/AirPlay outputs, first you'd have to make sure your session category is reporting them to you:

do 
{
    try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions: .AllowBluetooth)
    try audioSession.setActive(true)
} 
catch let e
{
    debugPrint("failed to initialize audio session: \(e)")
}

然后非直观的方式来获取可用输出,就是检查AVAudioSession.availableInputs,因为通常蓝牙HFP设备也将带有麦克风..我现在可能会假设很多..但这是始终如一的唯一方式您的可用输出.

Then a non-intuitive way to get available outputs is to check AVAudioSession.availableInputs as usually a bluetooth HFP device would have a mic too.. I might be assuming a lot right now.. but that's the only way to consistently get your availableOutputs.

更好的方法是使用MultipleRoute类别,这将使您在访问AVAudioSessionPort

A better way is to use MultipleRoute category which will give you more freedom at accessing AVAudioSessionPort

这篇关于列出可用的输出音频目标AVAudioSession的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆