Hi all,
in my Xamarin.Mac app I would like to capture audio from built-in microphone.
I can't convert the sample buffer into an audio file to play when the capture is end.
I start recording successfully in this method:
`
private bool _SetupMic()
{
// configure the capture session for low resolution, change this if your code
// can cope with more data or volume
captureSession = new AVCaptureSession()
{
SessionPreset = AVCaptureSession.PresetPhoto //AVCaptureSession.PresetiFrame1280x720
};
// create a device input and attach it to the session var captureDevice = AVCaptureDevice.GetDefaultDevice(AVMediaTypes.Audio); captureDeviceInput = AVCaptureDeviceInput.FromDevice(captureDevice); if (captureDeviceInput == null) { return false; } if (captureSession.CanAddInput(captureDeviceInput)) captureSession.AddInput(captureDeviceInput); else { return false; } AVCaptureAudioDataOutput output = new AVCaptureAudioDataOutput { }; output.AudioSettings = new AudioSettings(); CoreFoundation.DispatchQueue audioCaptureQueue = new CoreFoundation.DispatchQueue("Audio Capture Queue"); output.SetSampleBufferDelegateQueue(this, audioCaptureQueue); if (captureSession.CanAddOutput(output)) captureSession.AddOutput(output); else { return false; } captureSession.StartRunning(); return true; }`
Then I get each sample buffer and save it in a file:
`
[Export("captureOutput:didOutputSampleBuffer:fromConnection:")]
public virtual void DidOutputSampleBuffer(AVCaptureOutput captureOutput, CMSampleBuffer sampleBuffer, AVCaptureConnection connection)
{
// process samples (video frames) here
_SampleBuffer = sampleBuffer;
var blockBuffer = _SampleBuffer.GetDataBuffer();
var blockBufferDataLength = blockBuffer.DataLength;
var blockBufferData = new byte[blockBufferDataLength];
var status = blockBuffer.CopyDataBytes(0, blockBufferDataLength, out var destination); var path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory.Replace("MonoBundle", "Resources"), "mic.mp3"); using (var stream = new FileStream(path, FileMode.Append)) { stream.Write(destination, 0, destination.Length); } sampleBuffer.Dispose(); }`
The file is successfully created but if I try to play it in a player, it has 0 seconds length.
How can I save all sample buffer into one correct file audio that I can play in a second time?
Thanks in advance.
Answers
Nobody can help me?