-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAndroidAudioRecorder.cs
59 lines (47 loc) · 1.9 KB
/
AndroidAudioRecorder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System.Threading;
using System.Threading.Tasks;
using Android.Media;
using NAudio.Wave;
using TunerAndMetronome.AudioRecorders;
using Xamarin.Essentials;
namespace TunerAndMetronome.Android.AudioRecorders;
public class AndroidAudioRecorder : AudioRecorder
{
private static AudioRecord _audioRecord;
private CancellationTokenSource _cancellationTokenSource;
public override event BufferUpdatedHandler BufferUpdated;
public override async Task Start()
{
if (IsRecording) return;
var status = await Permissions.RequestAsync<Permissions.Microphone>();
if (status != PermissionStatus.Granted)
return;
_cancellationTokenSource = new CancellationTokenSource();
if (_audioRecord == null)
_audioRecord = new AudioRecord(AudioSource.Mic, 44100, ChannelIn.Stereo, Encoding.Pcm16bit, 32000);
_audioRecord.StartRecording();
Task.Run(async () =>
{
var minSize = AudioRecord.GetMinBufferSize(44100, ChannelIn.Stereo, Encoding.Pcm16bit);
var size = 16000;
var buffer = new byte[size < minSize ? minSize : size];
while (_audioRecord.RecordingState == RecordState.Recording &&
!_cancellationTokenSource.IsCancellationRequested)
{
await _audioRecord.ReadAsync(buffer, 0, buffer.Length);
BufferUpdated?.Invoke(buffer,
new WaveFormat(_audioRecord.Format.SampleRate,
_audioRecord.Format.FrameSizeInBytes * 8 / _audioRecord.Format.ChannelCount,
_audioRecord.Format.ChannelCount));
}
}, _cancellationTokenSource.Token);
IsRecording = true;
}
public override async Task Pause()
{
if (!IsRecording) return;
_audioRecord.Stop();
_cancellationTokenSource.Cancel();
IsRecording = false;
}
}