Please see here for more information on this application: http://rongchaua.net/blog/windows-phone-audio-recorder/

 

The core of this application is class Microphone of Microsoft.Xna.Framework.Audio which can be used by referencing to Microsoft.Xna.Framework. By declaring a microphone device, setting some predefined features, then I can handle the event BufferReady to record sound section of 1 second from microphone into an array of byte and append it to memory stream.

Microphone m_micDevice = Microphone.Default;

private void btnStart_Click(object sender, RoutedEventArgs e)
{
	...
	m_micDevice.BufferDuration = TimeSpan.FromMilliseconds(1000);
	m_baBuffer = new byte[m_micDevice.GetSampleSizeInBytes(m_micDevice.BufferDuration)];
	m_micDevice.BufferReady +=new EventHandler(m_Microphone_BufferReady);
	m_micDevice.Start();
}

void m_Microphone_BufferReady(object sender, EventArgs e)
{
	m_micDevice.GetData(m_baBuffer);
	...
	m_msAudio.Write(m_baBuffer,0, m_baBuffer.Length);
}

When the users click on Stop button, they will be asked for saving the memory stream to IsolateStorageFile.

if (txtAudio.Text != "")
{
	IsolatedStorageFile isfData = IsolatedStorageFile.GetUserStoreForApplication();
	string strSource = txtAudio.Text;
	int nIndex = 0;
	while (isfData.FileExists(txtAudio.Text))
	{
		strSource = txtAudio.Text + nIndex.ToString().PadLeft(2, '0') + ".wav";
	}

	IsolatedStorageFileStream isfStream = new IsolatedStorageFileStream(txtAudio.Text, FileMode.Create, IsolatedStorageFile.GetUserStoreForApplication());
	isfStream.Write(m_msAudio.ToArray(), 0, m_msAudio.ToArray().Length);
	isfStream.Close();
}

So it’s very simple to write an audio recorder in Windows Phone 7. In my recorder I add some data visualizer to notify the user that the recording is going on. This visualizer uses first 100 value of audio data and shows them in a bar chart. These values will vary continuously and make visualizer animating. 

<phoneNavigation:PhoneApplicationPage.Resources>
	<DataTemplate x:Key="template">
		<StackPanel Orientation="Horizontal" VerticalAlignment="Bottom">
			<Rectangle Height="{Binding}" Width="5" Fill="Blue" />
			<Rectangle Width="2" />
		</StackPanel>
	</DataTemplate>
</phoneNavigation:PhoneApplicationPage.Resources>

<ItemsControl x:Name="icBar" ItemsSource="{Binding Path=AudioData}"
	ItemTemplate="{StaticResource template}" Margin="0,6,0,114">
	<ItemsControl.ItemsPanel>
		<ItemsPanelTemplate>
			<StackPanel Orientation="Horizontal"/>
		</ItemsPanelTemplate>
	</ItemsControl.ItemsPanel>
</ItemsControl>

 

As you can see in order to update record progressing on GUI, I used data binding for ItemsSource property of ItemsControl and update this source each time when the event BufferReady is fired.

void m_Microphone_BufferReady(object sender, EventArgs e)
{
	...
	this.Dispatcher.BeginInvoke(() =>
		{
			vm.LoadAudioData(m_baBuffer);
			...
		}
		);
	...
}

 

Last edited Aug 14, 2010 at 12:41 AM by Athtar, version 2

Comments

No comments yet.