Announcement

All important changes of my site and my blog will be announced here.

Computer security

This is my hobby. I spend my freetime for it. I am not professional in this area. But I like it very much.

Everything Else

Which doesn’t belong to other categories will land here. It maybe about my daily life, entertainment, music, game … or something like that

Programming

This is my job. That means everyday I must sit before the monitor. Tip something, be annoyed by bugs,… but I simply like it.

Tutorial

When I found something interesting, I would like to share it to everyone. That is the reason for this category comes.

Home » Programming

Windows Phone – Audio recorder

Submitted by on Saturday, 3 April 201024 Comments | 6,482 views

Audio recorder is a typical application of a mobile phone. Man can use it to record audio from microphone and use it for his ring phone, store audio note or record evidence of crimes as in Hollywood films, etc… Therefore today I decide to write a small audio recorder which should run on any mobile phone using windows phone OS. The application is very small but helpful. You can get source code in the end of this post.

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);
			...
		}
		);
	...
}

The complete source code of this audio recorder you can download here “Windows Phone Audio Recorder“. If the archive is corrupted, see source code here

http://hintdesk.com/Web/Source/Windows%20Phone%20Audio%20Recorder/

Popularity: 10% [?]

My strongly recommended books to read. Choose one and enjoy yourself.

Related Posts:

  • No Related Posts

24 Comments »

  • John said:

    The link works, but it’s pointing to an empty .zip file.

  • admin (author) said:

    @John: I tested the link. The zip is fine. There are source code files in it

  • Henrik said:

    I have the same problem as John – when opening in WinZip 9.0 on Windows XP it says “cannot open file: it does not appear to be a valid archive”. When “explore”‘ing in explorer.exe it says “The compressed (zipped) folder is invalid or corrupted”. The zipfile is 90.6 KB in size. Weird.

  • Victor said:

    Hi, I have the same problem – cannot open file: it does not appear to be a valid archive.

    Please, re-archive file.

  • admin (author) said:

    @Henrik and Victor: I updated the archive again. Try if you can open it. I use WinRAR to make ZIP file.

  • JohnBush said:

    Thanks for the great article.
    I use WM Sound Recorder to record my HTC Touch Pro phone conversation. It could also record outgoing and incoming calls automatically.

  • Dan Colasanti said:

    The zip file still appears to be corrupted – please replace it when you get a chance. Couldn’t open it with Windows 7 Explorer or WinRAR.

  • admin (author) said:

    @Dan: Update again using Windows 7. If it does not work, send me a feedback.

  • Dan Colasanti said:

    I tried again and still get the following error dialog in Windows 7 Professional Edition x64 when trying to open the zip file:

    “Windows cannot open the folder.
    The Compressed (zipped) Folder ‘C:\…\Windows Phone Audio Recorder.zip’ is invalid.

    Dan

  • Nick said:

    I’m seeing the same issue. WinRar will open the file but there only appear to be one, no extension file inside the archive.

  • admin (author) said:

    @Dan and Nick: Browse the source code directly here http://hintdesk.com/Web/Source/Windows%20Phone%20Audio%20Recorder/

  • Florin said:

    What about this error: “Cannot implicitly convert type ‘System.EventHandler’ to ‘System.EventHandler’ ….\MainPage.xaml.cs 48 39″ ?

  • Exelero said:

    2 author,
    thx!! Good sample!

    Florin,
    is it difficult to think? Just add there .

  • Exelero said:

    EventArgs

  • Windows Phone – Audio recorder | WP7爱好者- Windows Phone 7(www.iwp7.com) said:
  • Manivel said:

    hi

    please tell me about the ProgressPopup.IsOpen

    thanks

  • Ed said:

    I too am not able to unzip.

    I did manually type from article / code samples and can’t get it working.

    My issue seems to be that the m_Microphone_BufferReady event never fires. During playback my memory stream is 0 bytes long.

    I have a microphone attached to my development (Vista) computer and assume that Phone 7 emulator will automatically use the attached mic? If not, do I need to somehow configure the emulator to use the mic?

    Even if no mic is attached, should I not be receiving a byte stream every second?

    I also notice that you are saving raw data as a wav file in Isolated Storage. If I simply transfer that file to my desktop (e.g. thru a web service), should I be able to listen to that as a normal wav file? I read somewhere that a Wav file needs to store some sort of header info.

  • admin (author) said:

    @Ed: Use the second link to view the code directly.
    - The Emulator will recognize your mic. You do not need to configure anything. If the mic can not be attached, you can not record anything.
    - The .wav file can be played as normal .wav audio file.

  • Jelly said:

    hi
    Do someone knows where are the sounds stored, I want to use it binding with other function, but nothing found.
    Does it related to Isolated Storage?

  • admin (author) said:

    @Jelly: Yes it’s stored in IsolatedStorage.

  • Ultimate List of Free Windows Phone 7 Software & Resources from Microsoft | The Road to Know Where said:

    [...] via Push Notifications. (Thanks Anand)   WINDOWS PHONE 7 SOFTWARE & PROJECTS:   Audio Recorder application can record & save audio using the phone’s microphone.   [...]

  • balintn said:

    Thanks for the sample!
    However, I use VStudio Pro RTM, and it does not load the solution.
    To work around that, I created a new solution, and added the code into that – most things are fine.

    Only one problem left: I have no visualization, only numbers appear.

  • ueTripleFi10 said:

    The link works. Thank your for sample share.

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.