QTKit Capture - Disabling specific audio channels when recording

Q: How can I disable specific audio channels when recording?

A: Use the QTCaptureConnectionEnabledAudioChannelsAttribute attribute, an NSIndexSet value that specifies which audio channels should be sent through the connection.

IMPORTANT: The QTCaptureConnectionEnabledAudioChannelsAttribute attribute is only writable once there are samples running through the connection.

To use the attribute, run the session (start capturing data) and then observe when the format description of the connection changes. Applications can be notified of changes to a connection’s format by registering to receive the QTCaptureConnectionFormatDescriptionDidChangeNotification notification as shown in Listing 1.

Listing 1: Registering for the QTCaptureConnectionFormatDescriptionDidChangeNotification notification.

 // Register for changes to a connection's format
  [[NSNotificationCenter defaultCenter] addObserver:self 
    selector:@selector(connectionFormatDidChange:) 
    name:QTCaptureConnectionFormatDescriptionDidChangeNotification 
    object:nil];

Once this change notification fires, you should first check if the attribute is writable, and then use it to enable the desired audio channels on the connection.

Listing 2: Using the QTCaptureConnectionEnabledAudioChannelsAttribute attribute to enable audio channels on the connection.

 // This method called when the connection format changes
  - (void)connectionFormatDidChange:(NSNotification *)notification
  {
    // You may now use the QTCaptureConnectionEnabledAudioChannelsAttribute
    // attribute to enable audio channels for the connection
    [self enableAudioChannelsForConnection:[self myAudioCaptureConnection]];
  }

  // Specify the audio channels that should be used on the connection
  -(void) enableAudioChannelsForConnection:(QTCaptureConnection *)captureConnection
  {
    // Make sure attribute is writeable before we use it
    if ([captureConnection attributeIsReadOnly:
      QTCaptureConnectionEnabledAudioChannelsAttribute] == NO)
    {
      NSRange channelIndexes;
      channelIndexes.location = 0;
      channelIndexes.length = 2;
      NSIndexSet *channelIndexSet = 
        [NSIndexSet indexSetWithIndexesInRange:channelIndexes];

      // Example: enable audio channels 1,2 (only) for the connection
      [captureConnection setAttribute:channelIndexSet 
        forKey:QTCaptureConnectionEnabledAudioChannelsAttribute];
    }
  }

References

Technical Q&A QA1607 QTKit Capture - Disabling Audio Or Video When Capturing From a Muxed Device

Back to Top

Document Revision History

Date Notes
2008-10-13 First Version

Posted: 2008-10-13


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.