I rewrite to different project . And this is the most basic to record an audio file(As i have seen). You can save to seperate file and load from file.
public partial class MainPage : PhoneApplicationPage
{
byte[] buffer;
MemoryStream stream = new MemoryStream();
SoundEffectInstance currentSound = null;
MediaLibrary library = new MediaLibrary();
Microphone microphone = Microphone.Default;
string fileName = "file.wav";
public MainPage()
{
InitializeComponent();
microphone.BufferDuration = TimeSpan.FromSeconds(1);
microphone.BufferReady += new EventHandler<eventargs>(microphone_BufferReady);
DispatcherTimer dt = new DispatcherTimer();
dt.Interval = TimeSpan.FromMilliseconds(33);
dt.Tick += new EventHandler(dt_Tick);
dt.Start();
}
private void OnAppbarRecordClick(object sender, EventArgs e)
{
stream.SetLength(0);
buffer = new byte[microphone.GetSampleSizeInBytes(microphone.BufferDuration)];
microphone.Start();
statusText.Text = "recording";
}
void OnAppbarPlayClick(object sender, EventArgs args)
{
using (var myIso = IsolatedStorageFile.GetUserStoreForApplication())
{
if (myIso.FileExists(fileName))
{
using (var fileStream = myIso.OpenFile(fileName, FileMode.Open))
{
stream.SetLength(0);
fileStream.CopyTo(stream);
SoundEffect sound = new SoundEffect(stream.ToArray(), microphone.SampleRate, AudioChannels.Stereo);
currentSound = sound.CreateInstance();
}
}
else
{
errorText.Text = "File Not exist";
}
}
currentSound.Play();
}
void OnAppbarPauseClick(object sender, EventArgs args)
{
currentSound.Pause();
}
private void OnAppbarStopClick(object sender, EventArgs e)
{
microphone.Stop();
statusText.Text = "strop record";
writeFile();
}
void microphone_BufferReady(object sender, EventArgs e)
{
microphone.GetData(buffer);
stream.Write(buffer, 0, buffer.Length);
}
void dt_Tick(object sender, EventArgs e)
{
try { FrameworkDispatcher.Update(); }
catch { }
}
void writeFile()
{
using (var myIso = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var fileStream = myIso.CreateFile(fileName))
{
var audioBuffer = stream.GetBuffer();
fileStream.Write(audioBuffer, 0, (int)stream.Length);
}
}
}
}</eventargs>