2013-03-26 10 views
21

मुझे आईओएस एप्लिकेशन में उपलब्ध ऑडियो आउटपुट सूचीबद्ध करने की आवश्यकता है। मेरा प्रश्न के समान है इस एक: How to list available audio output route on iOSउपलब्ध आउटपुट ऑडियो लक्ष्य AVAudioSession

मैं इस कोड की कोशिश की:

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), मैं भी अगर दो (एक एयरप्ले और एक को देखने के स्पीकर में लगा हुआ)। यदि मैं संगीत एप्लिकेशन का उपयोग करता हूं तो मैं दोनों बंदरगाहों को देखने और उनके बीच स्विच करने में सक्षम हूं। मेरे ऐप में मैं केवल वही देखता हूं जिसे मैंने चुना है।

मुझे कुछ और करने की ज़रूरत है?

मैं इस कोड की कोशिश की, भी:

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

लेकिन शब्दकोश सूची सिर्फ एक उत्पादन स्थलों

आप

संपादित करें धन्यवाद। इसके अलावा इनपुट सूत्रों सरणी रिक्त है (मैं एक iPhone 4s है)

EDIT2:

मैं MPVolumeView का उपयोग कर काम कर रहे कुछ मिला है। इस घटक में एक बटन है जो आपको संगीत ऐप में आउटपुट ऑडियो रूट चुनने देता है।

आप आप स्लाइडर छिपा कर सकते हैं चाहते हैं (और केवल बटन होता है) यदि का उपयोग करते हुए:

self.myMPVolumeView.showsVolumeSlider = NO; 

उत्तर

2

कुछ इस तरह की कोशिश करो, अपने अधिक आप की जरूरत की तुलना में, लेकिन आप इसे नीचे धीरे-धीरे कम कर सकते हैं:

+ (NSString *) demonstrateInputSelection 
{ 
    NSError* theError = nil; 
    BOOL result = YES; 
    NSMutableString *info = [[NSMutableString alloc] init]; 
    [info appendString: @"  Device Audio Input Hardware\n"]; 

    NSString *str = nil; 
    if(iOSMajorVersion < 7){ 
     str = @"No input device information available"; 
     NSLog(@"%@",str); 
     [info appendFormat:@"%@\n",str]; 

     return info; 
    } 

    AVAudioSession* myAudioSession = [AVAudioSession sharedInstance]; 

    result = [myAudioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&theError]; 
    if (!result) 
    { 
     NSLog(@"setCategory failed"); 
    } 

    result = [myAudioSession setActive:YES error:&theError]; 
    if (!result) 
    { 
     NSLog(@"setActive failed"); 
    } 

    // Get the set of available inputs. If there are no audio accessories attached, there will be 
    // only one available input -- the built in microphone. 
    NSArray* inputs = [myAudioSession availableInputs]; 
    str = [NSString stringWithFormat:@"\n--- Ports available on %@: %d ---", [UIDevice currentDevice].name , [inputs count]]; 
    NSLog(@"%@",str); 
    [info appendFormat:@"%@\n",str]; 

    // Locate the Port corresponding to the built-in microphone. 
    AVAudioSessionPortDescription* builtInMicPort = nil; 
    AVAudioSessionDataSourceDescription* frontDataSource = nil; 

    for (AVAudioSessionPortDescription* port in inputs) 
    { 
     // Print out a description of the data sources for the built-in microphone 
     str = @"\n**********"; 
     NSLog(@"%@",str); 
     [info appendFormat:@"%@\n",str]; 
     str = [NSString stringWithFormat:@"Port :\"%@\": UID:%@", port.portName, port.UID ]; 
     NSLog(@"%@",str); 
     [info appendFormat:@"%@\n",str]; 
     if([port.dataSources count]){ 
      str = [NSString stringWithFormat:@"Port has %d data sources",(unsigned)[port.dataSources count] ]; 
      NSLog(@"%@",str); 
      [info appendFormat:@"%@\n",str]; 
     } 

     str = [NSString stringWithFormat:@">%@", port.dataSources]; 
     NSLog(@"%@",str); 
    //  [info appendFormat:@"%@\n",str]; 

     if([port.portType isEqualToString:AVAudioSessionPortLineIn]){ 
      str = @"Line Input found"; 
      NSLog(@"%@",str); 
      [info appendFormat:@"%@\n",str]; 
     } 
     else if([port.portType isEqualToString:AVAudioSessionPortUSBAudio]){ 
      str = @"USB Audio found"; 
      NSLog(@"%@",str); 
      [info appendFormat:@"%@\n",str]; 
     } 
     else if ([port.portType isEqualToString:AVAudioSessionPortBuiltInMic]){ 
      builtInMicPort = port; 
      str = @"Built-in Mic found"; 
      NSLog(@"%@",str); 
      [info appendFormat:@"%@\n",str]; 
     } 
     else if ([port.portType isEqualToString:AVAudioSessionPortHeadsetMic]){ 
      builtInMicPort = port; 
      str = @"Headset Mic found"; 
      NSLog(@"%@",str); 
      [info appendFormat:@"%@\n",str]; 
     } 
     else{ 
      str = @"Other input source found"; 
      NSLog(@"%@",str); 
      [info appendFormat:@"%@\n",str]; 
     } 

     // loop over the built-in mic's data sources and attempt to locate the front microphone 
     for (AVAudioSessionDataSourceDescription* source in port.dataSources) 
     { 
      str = [NSString stringWithFormat:@"\nName:%@ (%d) \nPolar:%@ \nType:%@ \nPatterns:%@", source.dataSourceName, [source.dataSourceID intValue], source.selectedPolarPattern, port.portType, source.supportedPolarPatterns]; 
      NSLog(@"%@",str); 
      [info appendFormat:@"%@\n",str]; 

      //   if ([source.orientation isEqual:AVAudioSessionOrientationFront]) 
      //   { 
      //    frontDataSource = source; 
      //    break; 
      //   } 
     } // end data source iteration 

    } 

    str = @"\n---- Current Selected Ports ----\n"; 
    NSLog(@"%@",str); 
    [info appendFormat:@"%@",str]; 

    NSArray *currentInputs = myAudioSession.currentRoute.inputs; 
// str = [NSString stringWithFormat:@"\n%d current input ports", [currentInputs count]]; 
// NSLog(@"%@",str); 
// [info appendFormat:@"%@\n",str]; 
    for(AVAudioSessionPortDescription *port in currentInputs){ 
     str = [NSString stringWithFormat:@"\nInput Port :\"%@\":", port.portName ]; 
     NSLog(@"%@",str); 
     [info appendFormat:@"%@\n",str]; 
     if([port.dataSources count]){ 
      str = [NSString stringWithFormat:@"Port has %d data sources",(unsigned)[port.dataSources count] ]; 
      NSLog(@"%@",str); 
      [info appendFormat:@"%@\n",str]; 

      str = [NSString stringWithFormat:@"Selected data source:%@", port.selectedDataSource.dataSourceName]; 
      NSLog(@"%@",str); 
      [info appendFormat:@"%@\n",str]; 

      if([port.selectedDataSource.supportedPolarPatterns count] > 0){ 
       str = [NSString stringWithFormat:@"Selected polar pattern:%@", port.selectedDataSource.selectedPolarPattern]; 
       NSLog(@"%@",str); 
       [info appendFormat:@"%@\n",str]; 
      } 
     } 
    } 

    NSArray *currentOutputs = myAudioSession.currentRoute.outputs; 
// str = [NSString stringWithFormat:@"\n%d current output ports", [currentOutputs count]]; 
// NSLog(@"%@",str); 
// [info appendFormat:@"%@\n",str]; 
    for(AVAudioSessionPortDescription *port in currentOutputs){ 
     str = [NSString stringWithFormat:@"\nOutput Port :\"%@\":", port.portName ]; 
     NSLog(@"%@",str); 
     [info appendFormat:@"%@\n",str]; 
     if([port.dataSources count]){ 
      str = [NSString stringWithFormat:@"Port has %d data sources",(unsigned)[port.dataSources count] ]; 
      NSLog(@"%@",str); 
      [info appendFormat:@"%@\n",str]; 

      str = [NSString stringWithFormat:@"Selected data source:%@", port.selectedDataSource.dataSourceName]; 
      NSLog(@"%@",str); 
      [info appendFormat:@"%@\n",str]; 
     } 

    } 

// str = [NSString stringWithFormat:@"\Current Route: %@ Source:%@\n", myAudioSession.currentRoute.portName, myAudioSession.preferredInput.selectedDataSource.dataSourceName]; 
// NSLog(@"%@",str); 
// [info appendFormat:@"%@\n",str]; 


    if(myAudioSession.preferredInput.portName){ 
     str = [NSString stringWithFormat:@"\nPreferred Port: %@ Source:%@\n", myAudioSession.preferredInput.portName, myAudioSession.preferredInput.selectedDataSource.dataSourceName]; 
    } else { 
     str = @"\nNo Preferred Port set"; 
    } 
    NSLog(@"%@",str); 
    [info appendFormat:@"%@\n",str]; 

    return info; 

    if (frontDataSource) 
    { 
     NSLog(@"Currently selected source is \"%@\" for port \"%@\"", builtInMicPort.selectedDataSource.dataSourceName, builtInMicPort.portName); 
     NSLog(@"Attempting to select source \"%@\" on port \"%@\"", frontDataSource, builtInMicPort.portName); 

     // Set a preference for the front data source. 
     theError = nil; 
     result = [builtInMicPort setPreferredDataSource:frontDataSource error:&theError]; 
     if (!result) 
     { 
      // an error occurred. Handle it! 
      NSLog(@"setPreferredDataSource failed"); 
     } 
    } 

    // Make sure the built-in mic is selected for input. This will be a no-op if the built-in mic is 
    // already the current input Port. 
    theError = nil; 
    result = [myAudioSession setPreferredInput:builtInMicPort error:&theError]; 
    if (!result) 
    { 
     // an error occurred. Handle it! 
     NSLog(@"setPreferredInput failed"); 
    } 

    return info; 
} 
+0

जबकि कुछ हद तक उपयोगी, यह कैसे _available_ आउटपुट को दिखाने के लिए इस सवाल का जवाब नहीं है तक पहुँचने में और अधिक स्वतंत्रता दे देंगे उपयोग करने के लिए के रूप में यह केवल _current_ आउटपुट से पता चलता है । ऐसा प्रतीत होता है कि फिलहाल उपलब्ध आउटपुट की सूची दिखाने का एकमात्र तरीका प्रदान किया गया है 'MPVolumeView' के रूट बटन के माध्यम से। – Stuart

+0

क्या इनपुट स्रोत चुनने के बाद आउटपुट स्रोत का चयन करने की संभावना है? मैं ऐसा करने में सक्षम नहीं लग सकता। –

1
AVAudioSessionRouteDescription *currentRoute = [[AVAudioSession sharedInstance] currentRoute]; 
    for (AVAudioSessionPortDescription *output in currentRoute.outputs) { 

    } 
1

यह आपके AVAudioSession श्रेणी पर निर्भर करेगा।

आप सुरक्षित रूप से एक आईफोन पर आश्वस्त कर सकते हैं कि आपके पास कम से कम एक माइक्रोफोन इनपुट और स्पीकर आउटपुट के रूप में है।

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

फिर एक गैर सहज उपलब्ध outputs है पाने के लिए जिस तरह से: क्या आप ब्लूटूथ/AirPlay आउटपुट की एक सूची प्राप्त करने की कोशिश कर रहे हैं, पहले आप सुनिश्चित करें कि आपके सत्र श्रेणी आप उन्हें रिपोर्ट कर रहा है बनाने के लिए होगा AVAudioSession.availableInputs की जांच करने के लिए आमतौर पर एक ब्लूटूथ एचएफपी डिवाइस में एक माइक भी होगा .. मैं अभी बहुत कुछ मान रहा हूं .. लेकिन यह आपके उपलब्ध आउटपुट को लगातार प्राप्त करने का एकमात्र तरीका है।

एक बेहतर तरीका, MultipleRoute श्रेणी है जो आप AVAudioSessionPort

संबंधित मुद्दे