Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now the Silverlight works with your webcam, it's not surprising to learn that it can also capture audio with a microphone. The key component to audio capture is the AudioSink class. This class is your gateway to receiving audio information from Silverlight. For today's demo I'm using a Wav class written by a weblogger to persist my audio file. I've opened a project called WorkingWithMicrophone, and the first place I want to look is on my UI. I have three buttons here. I have a Record button, a Stop button, and a Save button. Now remember that I say we need an AudioSink, so I've created a drive class called DemoAudioSink.
As you can see, I'm deriving from the AudioSink class, and I've already written a number of methods. The most important one is down here, called OnSample. So every time information comes in from the microphone, I'm going to take this memory stream object that I've created here and I'm going to write the byte data into the memory stream. So it's going to be stored in memory inside my Silverlight application. Now let's see how we would use this. I need to save the information that's on future point onto the hard drive, so I have this class over here called WavManager.cs.
And as I said, this was written by a web blogger, and he has this mythical SavePcmToWav. I'm not going to go through all the details of this method, but basically, it takes this PCM file in memory and turns it into the WAV format so that I can play it back on a standard Windows playback device. Next, let's look at the code behind my MainPage.xaml. When you click on the record button, the first thing I do is I go to the operating system and I call it GetDefaultAudioCaptureDevice. This returns the default microphone for this computer.
I then configure a few other settings, and then I call a function I wrote called RecordAudio. RecordAudio turns on a busy indicator, checks to make sure that we have AllowedDeviceAccess that asks for the user's approval to use the microphone, and then down here we create that instance of the DemoAudioSink, tell it to use the microphone, which passes the data for microphone into my class, and then I say now start capturing that data. So it captures away for a few seconds and then I click on the stop button down here.
I turn off the busy indicator, check to make sure that I'm in the Started state, and so then I call Stop. Now it's no longer capturing information and I have this big chunk of audio data in my memory. Next I'll persist that to the hard drive. When you click on the save button I'll prompt the user for a location on their hard drive, create a stream from that file, and then I'm going to call that class WavManager SavePcmToWav. And I pass in the memory BackingStream, and then a few other pieces of information here, and then close the stream.
Now I should have the file persisted on my local desktop. Let's see how it works. Press Record. It ask me if it's okay to use my microphone. I say it is. You see my busy indicator pop up there. I talk for a minute or so, and then I click on Stop, and then I click on the Save button over here in the side of the screen. It prompts me for a location, demoWav. I'm going to save it to my Desktop.
And if everything worked according to plan, I can go out to my Desktop, there is the DemoWav, and I'll double-click on it and see if it will play on my computer. "I say it is. You see my busy indicator pop up there. I talk for a minute or so and then I click on Stop." Now I can see some enhancements to this application. For one, you might want to save the files to the web server. To do that, you can use the webClient class and upload the file. I have another movie in the series showing you how to do that.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98069 Viewers
61 Video lessons · 85308 Viewers
71 Video lessons · 69354 Viewers
56 Video lessons · 101717 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.