Bug 38551 - AVFoundation: AVAudioSession.SetPreferredInputNumberOfChannels() throwing error
Summary: AVFoundation: AVAudioSession.SetPreferredInputNumberOfChannels() throwing error
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: XI 9.4 (iOS 9.2)
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2016-02-08 19:10 UTC by Richard Hopkins
Modified: 2016-02-08 19:44 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Reference sample (17.13 KB, application/zip)
2016-02-08 19:10 UTC, Richard Hopkins
C# partial implementation (28.76 KB, application/zip)
2016-02-08 19:14 UTC, Richard Hopkins

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Richard Hopkins 2016-02-08 19:10:59 UTC
Created attachment 14947 [details]
Reference sample

I'm attempting to resolve a customer issue with extracting Int16ChannelData from a buffer using AVAudioEngine.InputNode.InstallTapOnBus().

As a last resort trying to fix the issue in their code, I took a reference sample in Objective C and transcoded it into C# line by line.

Both the reference code and the Xamarin solution throw the same error on the simulator:

2016-02-08 17:45:59.149 AudioTest[7773:2330675] Unable to set preferred number of channels
2016-02-08 17:45:59.502 AudioTest[7773:2330675] 17:45:59.493 ERROR:    [0x3930b000] AVAudioNode.mm:564: CreateRecordingTap: required condition is false: IsFormatSampleRateAndChannelCountValid(format)

However, the reference code runs on the device, but the Xamarin solution doesn't - instead it throws the same error as on the simulator.

Attached are the reference sample and the C# version up to the point where the code crashes.  In both cases it seems to occur at the point of the input.InstallTapOnBus line, but the problem may be related to an earlier issue in the code where the channelcount is set:

okState= [audioSession setPreferredInputNumberOfChannels:kNumChannel error:&outError];
okState = audioSession.SetPreferredInputNumberOfChannels(audioSession.CurrentHardwareInputNumberOfChannels, out error);

I haven't fully implemented the entire reference sample in C# as this is just to show the issue.
Comment 1 Richard Hopkins 2016-02-08 19:14:49 UTC
Created attachment 14948 [details]
C# partial implementation
Comment 2 Rolf Bjarne Kvinge [MSFT] 2016-02-08 19:44:00 UTC
You didn't port the audioSessionManagement method from the ObjC sample, which is important.

Adding the following lines to ViewDidLoad fixes the C# sample:

	var audioSession = AVAudioSession.SharedInstance();
	audioSession.SetCategory (AVAudioSessionCategory.PlayAndRecord, AVAudioSessionCategoryOptions.MixWithOthers);
	audioSession.SetActive (true);