diff --git a/Plugin.AudioRecorder.Shared/AudioRecorderService.cs b/Plugin.AudioRecorder.Shared/AudioRecorderService.cs
index 1d542c4..a558996 100644
--- a/Plugin.AudioRecorder.Shared/AudioRecorderService.cs
+++ b/Plugin.AudioRecorder.Shared/AudioRecorderService.cs
@@ -82,6 +82,18 @@ public partial class AudioRecorderService
/// This event will be raised on a background thread to allow for any further processing needed. The audio file will be null in the case that no audio was recorded.
public event EventHandler AudioInputReceived;
+ ///
+ /// This event is raised when the most recent audio level has been calculated.
+ ///
+ /// This event will be raised on a background thread to allow for any further processing needed. The audio level will be 0.0f in the case that no audio was recorded.
+ public event EventHandler AudioLevelUpdated;
+
+ ///
+ /// Gets/sets a value indicating if the should write audio data to a file.
+ ///
+ /// Defaults to true
+ public bool WriteAudioDataToFile { get; set; } = true;
+
partial void Init ();
///
@@ -146,6 +158,8 @@ void AudioStream_OnBroadcast (object sender, byte [] bytes)
{
var level = AudioFunctions.CalculateLevel (bytes);
+ AudioLevelUpdated?.Invoke(this, level);
+
if (level < NearZero && !audioDetected) // discard any initial 0s so we don't jump the gun on timing out
{
Debug.WriteLine ("level == {0} && !audioDetected", level);
@@ -247,7 +261,9 @@ void InitializeStream (int sampleRate)
if (recorder == null)
{
- recorder = new WaveRecorder ();
+ recorder = new WaveRecorder {
+ WriteAudioDataToFile = WriteAudioDataToFile
+ };
}
}
catch (Exception ex)
@@ -264,5 +280,14 @@ public string GetAudioFilePath ()
{
return audioDetected ? FilePath : null;
}
+
+ ///
+ /// Gets a new to the audio data in readonly mode.
+ ///
+ /// A object that can be used to read the audio memory from the beginning.
+ public Stream GetAudioMemoryStream()
+ {
+ return recorder.GetAudioMemoryStream();
+ }
}
}
diff --git a/Plugin.AudioRecorder.Shared/WaveRecorder.cs b/Plugin.AudioRecorder.Shared/WaveRecorder.cs
index 50a6fd9..e4ac4a1 100644
--- a/Plugin.AudioRecorder.Shared/WaveRecorder.cs
+++ b/Plugin.AudioRecorder.Shared/WaveRecorder.cs
@@ -10,11 +10,18 @@ internal class WaveRecorder : IDisposable
{
string audioFilePath;
FileStream fileStream;
+ MemoryStream memoryStream;
StreamWriter streamWriter;
BinaryWriter writer;
int byteCount;
- IAudioStream audioStream;
-
+ IAudioStream audioStream;
+
+ ///
+ /// Gets/sets a value indicating if the should write audio data to a file.
+ ///
+ /// Defaults to true
+ public bool WriteAudioDataToFile { get; set; } = true;
+
///
/// Starts recording WAVE format audio from the audio stream.
///
@@ -38,8 +45,16 @@ public async Task StartRecorder (IAudioStream stream, string filePath)
audioFilePath = filePath;
audioStream = stream;
- fileStream = new FileStream (filePath, FileMode.Create, FileAccess.Write, FileShare.Read);
- streamWriter = new StreamWriter (fileStream);
+ if (WriteAudioDataToFile)
+ {
+ fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Read);
+ streamWriter = new StreamWriter(fileStream);
+ }
+ else
+ {
+ memoryStream = new MemoryStream();
+ streamWriter = new StreamWriter(memoryStream);
+ }
writer = new BinaryWriter (streamWriter.BaseStream, Encoding.UTF8);
byteCount = 0;
@@ -68,6 +83,15 @@ public Stream GetAudioFileStream ()
{
//return a new stream to the same audio file, in Read mode
return new FileStream (audioFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
+ }
+
+ ///
+ /// Gets a new to the audio data in readonly mode.
+ ///
+ /// A object that can be used to read the audio memory from the beginning.
+ public Stream GetAudioMemoryStream()
+ {
+ return memoryStream;
}
void StreamActiveChanged (object sender, bool active)
@@ -121,6 +145,7 @@ public void StopRecorder ()
writer.Dispose (); //this should properly close/dispose the underlying stream as well
writer = null;
fileStream = null;
+ memoryStream = null;
streamWriter = null;
}
diff --git a/Samples/Forms/AudioRecord.Forms/MainPage.xaml.cs b/Samples/Forms/AudioRecord.Forms/MainPage.xaml.cs
index e95c046..4563ac4 100644
--- a/Samples/Forms/AudioRecord.Forms/MainPage.xaml.cs
+++ b/Samples/Forms/AudioRecord.Forms/MainPage.xaml.cs
@@ -20,11 +20,16 @@ public MainPage ()
TotalAudioTimeout = TimeSpan.FromSeconds (15),
AudioSilenceTimeout = TimeSpan.FromSeconds (2)
};
+ recorder.AudioLevelUpdated += Recorder_AudioLevelUpdated;
player = new AudioPlayer ();
player.FinishedPlaying += Player_FinishedPlaying;
}
+ public void Recorder_AudioLevelUpdated (object sender, float level)
+ {
+ }
+
async void Record_Clicked (object sender, EventArgs e)
{
await RecordAudio ();