2013-03-26 19 views
21

Tôi cần liệt kê đầu ra âm thanh có sẵn cho ứng dụng iOS. Câu hỏi của tôi cũng tương tự như này: How to list available audio output route on iOSLiệt kê mục tiêu âm thanh đầu ra có sẵn AVAudioSession

tôi đã cố gắng mã này:

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

Tuy nhiên tôi luôn luôn nhìn thấy chỉ là một cổng đầu ra (đếm là 1), còn nếu tôi có hai (một Airplay và một Loa tích hợp). Nếu tôi sử dụng ứng dụng Nhạc, tôi có thể xem cả hai cổng và chuyển đổi giữa chúng. Trong ứng dụng của tôi, tôi chỉ thấy một cái mà tôi đã chọn.

Có điều gì khác mà tôi cần phải làm?

Cảm ơn bạn

EDIT:

tôi đã cố gắng mã này, quá:

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

nhưng danh sách từ điển chỉ là một địa điểm đầu ra. Hơn nữa mảng nguồn đầu vào là rỗng (Tôi có một iPhone 4s)

EDIT2:

tôi có một cái gì đó làm việc sử dụng MPVolumeView. Thành phần này có nút cho phép bạn chọn tuyến âm thanh đầu ra, như trong Ứng dụng nhạc.

Nếu bạn muốn, bạn có thể ẩn thanh trượt (và chỉ có nút) sử dụng:

self.myMPVolumeView.showsVolumeSlider = NO; 

Trả lời

2

Hãy thử một cái gì đó như thế này, nó nhiều hơn bạn cần nhưng bạn có thể pare nó xuống:

+ (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

Trong khi phần nào hữu ích, điều này không trả lời câu hỏi làm thế nào để hiển thị các kết quả đầu ra _available_, vì nó chỉ cho thấy các kết quả đầu ra _current_ . Có vẻ như cách duy nhất để hiển thị danh sách các đầu ra có sẵn tại thời điểm này là thông qua nút tuyến đường 'MPVolumeView' được cung cấp. – Stuart

+0

Có khả năng chọn nguồn đầu ra sau khi chọn nguồn đầu vào không? Tôi dường như không thể làm được điều này. –

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

    } 
1

Nó sẽ tùy thuộc vào danh mục AVAudioSession của bạn.

Bạn có thể giả sử một cách an toàn trên iPhone rằng bạn có ít nhất một micrô làm đầu vào và loa là đầu ra. Nếu bạn đang cố gắng để có được một danh sách các kết quả đầu ra/AirPlay Bluetooth, trước tiên bạn phải chắc chắn rằng loại phiên của bạn được báo cáo họ cho bạn:

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

Sau đó, một cách không trực quan để có được kết quả đầu ra có sẵn là để kiểm tra AVAudioSession.availableInputs như thường là một thiết bị HFP bluetooth sẽ có một mic quá .. Tôi có thể được giả định rất nhiều ngay bây giờ .. nhưng đó là cách duy nhất để luôn luôn có đượcOutputs có sẵn của bạn.

Cách tốt hơn là sử dụng MultipleRoute loại mà sẽ cung cấp cho bạn tự do hơn ở việc tiếp cận AVAudioSessionPort

Các vấn đề liên quan