ios - 覆盖iOS音频输出

标签 ios objective-c iphone audio

我一直在为一个正在研究的项目做一些研究,试图确定是否可以通过编程方式重定向音频输出。假设我有一个通过USB连接到iPhone的音频混音器盒。该USB支持音频输入和输出路由。我知道如何“检测”外部USB附件是否已连接到iPhone,并且我知道如何检测AVAudioSession的输入和输出。现在,让我发布一些示例代码,并将其理解为到目前为止。

AVAudioSessionRouteDescription *route = [[AVAudioSession sharedInstance] currentRoute];

//Check the route inputs
for( AVAudioSessionPortDescription *inputDescription in route.inputs)
{
    NSLog(@"Port Name: %@", inputDescription.portName);
    NSLog(@"Port Description: %@", inputDescription.portType);

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Input Ports" message:[NSString stringWithFormat:@"INPUT PORT NAME NAME: %@ INPUT PORT DESCRIPTION: %@ INPUT ROUTE COUNT: %lu",inputDescription.portName, inputDescription.portType, (unsigned long)[route.outputs count]] delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok" , nil];
    [alert show];

    [self setInput:inputDescription];
}

if(route.inputs.count == 0)
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Input Ports" message:@"Did not detect any input connections" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok" , nil];

    [alert show];
}

//Check the route outputs
for( AVAudioSessionPortDescription *portDescription in route.outputs )
{ 
    NSLog(@"Port Name: %@", portDescription.portName);
    NSLog(@"Port Description: %@", portDescription.portType);

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Output Ports" message:[NSString stringWithFormat:@"OUTPUT PORT NAME NAME: %@ OUTPUT PORT DESCRIPTION: %@ OUTPUT ROUTE COUNT: %lu",portDescription.portName, portDescription.portType, (unsigned long)[route.outputs count]] delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok" , nil];
    [alert show];   
}

我也知道如何“替代” iOS输出,以便它涉及内置扬声器而不是耳机。
NSError *error;

AVAudioSession *session = [AVAudioSession sharedInstance];

BOOL overrideSuccess =  [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];

if(overrideSuccess)
{
    NSLog(@"Override succeeded");
}

理想情况下,我希望能够执行以下操作:
  • 检测任何输出并将其显示给用户。 (我可以这样做)
  • 允许用户“选择”他们希望使用
  • 的输出
  • 覆盖当前输出路径,以将输出重定向到用户希望使用的设备。

  • 我不确定是否可以做这最后一步。

    这个想法是让用户进行录音而不仅限于iOS设备的外部麦克风。如果我想要实现什么,将不胜感激任何提示或建议。另外,我已经广泛浏览了Apple文档,因此无法断定是否有100%的可能性。

    最佳答案

    如果使用AVAudioSession,则可以使用“overrideOutputAudioPort:error:”方法来实现。这是指向AVAudioSession类引用的链接:https://developer.apple.com/library/IOs/documentation/AVFoundation/Reference/AVAudioSession_ClassReference/index.html

    关于ios - 覆盖iOS音频输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27065122/

    相关文章:

    objective-c - NSPredicate 计数 NSString 的数量

    iphone - 呈现 UINavigationController 多个深度 View ,作为初始 View

    ios - 如何在 xcode 7 中的 xcode 8 中为标签添加更多字体大小,它在字体旁边显示 '+' 符号

    objective-c - hitTestForEvent :inRect:ofView is invoked twice in my NSOutlineView cells

    objective-c - 在 NSTableView 中按部分显示记录

    iphone - iOS电话簿中的竖排字母叫什么?

    iPhone UILabel 文字软阴影

    objective-c - iOS 开发和提供私有(private)应用程序

    ios - 使用GAP Socket进行通信。编译器说未找到 PGPlugin.h

    ios - iOS Dev- Hook 到应用程序音频输出