2014-11-21 137 views
1

我一直在为我正在研究的项目进行一些研究,并试图确定是否可以通过编程重定向音频输出。假设我有一个音频混音器盒通过USB连接到iPhone。该USB支持音频输入和输出路由。我知道如何“检测”外部USB配件是否连接到iPhone,并且我知道如何检测AVAudioSession的输入和输出。让我发布一些示例代码,以我目前了解的内容。覆盖iOS音频输出

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"); 
} 

我非常希望能够做这样的事情:

  1. 检测到任何输出,它们显示给用户。 (我可以这样做)
  2. 允许用户“选择”他们希望使用的输出
  3. 覆盖当前输出路径以将输出重定向到用户希望使用的设备。

这最后一步我不确定我能做些什么。

这个想法是让用户录制,而不限于iOS设备的外接麦克风。希望任何提示或建议,如果我想实现是可能的。另外,我已经通过Apple文档深入了解了这一点,并且无法断定这是否是100%可能。

回答

0

如果您使用AVAudioSession,使用“overrideOutputAudioPort:error:”方法,您可以做到这一点。以下是AVAudioSession类参考的链接:https://developer.apple.com/library/IOs/documentation/AVFoundation/Reference/AVAudioSession_ClassReference/index.html

+0

啊我已经这样做了。这些选项是有限的,因为覆盖选项是AVAudioSessionOverrideSpeaker和AVAudioSessionOverrideNone。从苹果文档OverrideNone“使用此选项将音频路由到当前类别和模式的系统默认输出”。 – zic10 2014-11-21 18:04:22