我正在尝试用AudioUnit录制音频。
我有下面的代码:但是SetFormat方法返回PropertyNotWritable。我已经能够使用这样的代码来播放音频,使用相同的格式参数。但不能让它用来录制音频。
(请注意,Xamarin似乎缺少RemoteIO常量:( )
const int AudioUnitSubTypeRemoteIO = ('r' << 24) + ('i' << 16) + ('o' << 8) + 'c';
readonly AudioUnit.AudioUnit _audioUnit;
public AudioRecorderIos()
{
var audioComponentDescription = new AudioComponentDescription()
{
ComponentType = AudioComponentType.Output,
ComponentSubType = AudioUnitSubTypeRemoteIO,
ComponentManufacturer = AudioComponentManufacturerType.Apple,
ComponentFlags = 0,
ComponentFlagsMask = 0
};
AudioComponent defaultOutput = AudioComponent.FindNextComponent(null, ref audioComponentDescription);
_audioUnit = defaultOutput.CreateAudioUnit();
const int sampleRate = 8000;
const int channels = 1;
const int bytesPerSample = 2;
int framesPerPacket = 1;
var streamFormat = new AudioStreamBasicDescription()
{
SampleRate = sampleRate,
Format = AudioFormatType.LinearPCM,
FormatFlags = AudioFormatFlags.LinearPCMIsSignedInteger | AudioFormatFlags.IsPacked,
ChannelsPerFrame = channels,
BytesPerFrame = bytesPerSample * channels,
BitsPerChannel = bytesPerSample * 8,
BytesPerPacket = framesPerPacket * bytesPerSample * channels,
FramesPerPacket = framesPerPacket,
Reserved = 0
};
AudioUnitStatus status = _audioUnit.SetFormat(streamFormat, AudioUnitScopeType.Output);
if (status != AudioUnitStatus.OK)
{
throw new ArgumentOutOfRangeException($"Failed to set Audio Format with error {status}");
}
_audioUnit.SetRenderCallback(InputCallback, AudioUnitScopeType.Output);
_audioUnit.Initialize();Update:如果我忽略了SetFormat的输出,并在初始化后调用了GetFormat,那么我会得到非常不同的格式选项,如果我然后更新我的格式请求,使其与它所选择的相同,那么我仍然得到PropertyNotWritable。因此,我不认为这与我选择的格式值有关。我还在网上找到了一些使用与我完全相同的格式选项的人的例子。
发布于 2020-06-10 22:50:31
我需要调用_audioUnit.SetEnableIO(true,AudioUnitScopeType.Input,1)来启用记录。以及在Info.plist中添加“隐私-麦克风使用描述”
在SetFormat和SetInputCallback方法中,将audioUnitElement参数设置为1。
https://stackoverflow.com/questions/62292318
复制相似问题