diff options
author | Matthew S <matthewsot@outlook.com> | 2016-12-26 23:47:44 -0800 |
---|---|---|
committer | Matthew S <matthewsot@outlook.com> | 2016-12-26 23:47:44 -0800 |
commit | a09dbd1be6f8ecd866485288b65430aef53dcc90 (patch) | |
tree | f2bfa27a79a5d8e70ae3ef566c243f7380e71ad3 | |
parent | 848aabdfb9fa834242c16c473ffacf60a0b98b97 (diff) |
Android WAV recorder
-rw-r--r-- | WAVRecorder/WAVRecorder.Android/WAVRecorder.cs | 66 | ||||
-rw-r--r-- | WAVRecorder/WAVRecorder.UWP/WAVRecorder.cs | 7 | ||||
-rw-r--r-- | WAVRecorder/WAVRecorder/IWAVRecorder.cs | 4 |
3 files changed, 53 insertions, 24 deletions
diff --git a/WAVRecorder/WAVRecorder.Android/WAVRecorder.cs b/WAVRecorder/WAVRecorder.Android/WAVRecorder.cs index 8aa8ce1..96573cc 100644 --- a/WAVRecorder/WAVRecorder.Android/WAVRecorder.cs +++ b/WAVRecorder/WAVRecorder.Android/WAVRecorder.cs @@ -1,55 +1,87 @@ using Android.Media; using System; -using System.Collections.Generic; -using System.Linq; +using System.IO; +using System.Threading; using System.Threading.Tasks; namespace WAVRecorder.Android { public class WAVRecorder : IWAVRecorder { - public IDisposable RawWAVStream { get; private set; } - - public System.IO.Stream WAVStream - { + public IDisposable RawWAVStream { get { - return null; + return WAVStream; } } + public System.IO.Stream WAVStream { get; private set; } + private AudioRecord audioRecorder; private byte[] audioBuffer; //See: https://raw.githubusercontent.com/xamarin/monodroid-samples/master/Example_WorkingWithAudio/Example_WorkingWithAudio/LowLevelRecordAudio.cs - public async Task<bool> RequestPermissionsAndInitAsync() + public Task<bool> RequestPermissionsAndInitAsync() { audioBuffer = new Byte[100000]; audioRecorder = new AudioRecord( - // Hardware source of recording. AudioSource.Mic, - // Frequency 16000, - // Mono or stereo ChannelIn.Mono, - // Audio encoding Encoding.Pcm16bit, - // Length of the audio clip. audioBuffer.Length ); - return true; + WAVStream = new MemoryStream(); + + return new Task<bool>(() => true); } - public async void StartRecordingAsync() + private bool endRecording; + + private async Task HandleBufferAsync() + { + while (true) + { + if (endRecording) + { + endRecording = false; + break; + } + + try + { + // Keep reading the buffer while there is audio input. + int numBytes = await audioRecorder.ReadAsync(audioBuffer, 0, audioBuffer.Length); + //TODO: does this reset the audioRecorder? + await WAVStream.WriteAsync(audioBuffer, 0, numBytes); + } + catch (Exception ex) + { + Console.Out.WriteLine(ex.Message); + //break; + } + } + } + + public Task StartRecordingAsync() { audioRecorder.StartRecording(); // Off line this so that we do not block the UI thread. - await ReadAudioAsync(); + HandleBufferAsync(); + + return new Task(() => { }); } - public async void StopRecordingAsync() + public Task StopRecordingAsync() { + endRecording = true; + while (endRecording == true) + { + Thread.Sleep(500); + } + + return new Task(() => { }); } } } diff --git a/WAVRecorder/WAVRecorder.UWP/WAVRecorder.cs b/WAVRecorder/WAVRecorder.UWP/WAVRecorder.cs index 31f47d7..0205043 100644 --- a/WAVRecorder/WAVRecorder.UWP/WAVRecorder.cs +++ b/WAVRecorder/WAVRecorder.UWP/WAVRecorder.cs @@ -1,8 +1,5 @@ using System; -using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Text; using System.Threading.Tasks; using Windows.Media.Capture; using Windows.Media.MediaProperties; @@ -41,12 +38,12 @@ namespace WAVRecorder.UWP return true; } - public async void StartRecordingAsync() + public async Task StartRecordingAsync() { await mediaCapture.StartRecordToStreamAsync(mediaProfile, (InMemoryRandomAccessStream)RawWAVStream); } - public async void StopRecordingAsync() + public async Task StopRecordingAsync() { await mediaCapture.StopRecordAsync(); } diff --git a/WAVRecorder/WAVRecorder/IWAVRecorder.cs b/WAVRecorder/WAVRecorder/IWAVRecorder.cs index dc3f0f7..1478de1 100644 --- a/WAVRecorder/WAVRecorder/IWAVRecorder.cs +++ b/WAVRecorder/WAVRecorder/IWAVRecorder.cs @@ -22,8 +22,8 @@ namespace WAVRecorder IDisposable RawWAVStream { get; } - void StartRecordingAsync(); + Task StartRecordingAsync(); - void StopRecordingAsync(); + Task StopRecordingAsync(); } } |