I created a small app to record voice and play it, use this code to make a record button and a play button.
I tested the app, recorder worked fine (I used "Windows phone power tools" to get tempAudio.wav from isolated storage of WP emulator and this audio file can played), but the play button didn't play the sound, I can't find anything wrong with the btPlay button :(
XAML code (the two button is roundtoggle button and round button from coding4fun toolkit)
<StackPanel Orientation="Horizontal">
<toolkit1:RoundToggleButton x:Name="btRecorder" IconUri="..." Checked="btRecorder_Checked" checked="btRecorder_Unchecked"/>
<MediaElement x:Name="meVoicePlayer" AutoPlay="False"/>
<toolkit1:RoundButton x:Name="btPlay" Click="PlayAudio_Click" IconUri="..."/>
</StackPanel>
c# code
MicrophoneRecorder recorder = new MicrophoneRecorder();
private void btRecorder_Checked(object sender, RoutedEventArgs e)
{
recorder.Start();
}
private void btRecorder_Unchecked(object sender, RoutedEventArgs e)
{
recorder.Stop();
SaveTempAudio(recorder.Buffer);
}
public void SaveTempAudio(MemoryStream buffer)
{
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
var bytes = buffer.GetWavAsByteArray(recorder.SampleRate);
using (IsolatedStorageFileStream audio = new IsolatedStorageFileStream("TempAudio.wav",FileMode.Create,isf))
{
audio.Write(bytes, 0, bytes.Length);
}
}
}
private void PlayAudio_Click(object sender, RoutedEventArgs e)
{
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream audio = new IsolatedStorageFileStream("TempAudio.wav",FileMode.Open,isf))
{
meVoicePlayer.Stop();
meVoicePlayer.SetSource(audio);
meVoicePlayer.Position = new TimeSpan(0, 0, 0, 0);
meVoicePlayer.Play();
}
}
}