Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Let's move on now to play back the sound that we've been recording. Again let's take a look at the solution. We've added a couple of additional images, a Play button and a Pause button, again, both from the Icons Library supplied with the Windows Phone SDK. And let's take a look at the XAML for the MainPage. We're dealing with the ApplicationBar here, so there is nothing we can see in the designer. What we can see down here at the bottom now is that not only do we have a button for recording, we have a button for play. And we'll see how we actually make that come alive, we'll take a look at the code.
So everything that we've seen so far is the same except now we have some variables that help us be able to play the sound back. We have a sound effects instance, which is what we're going to do to take that stream and play it back. And we're using sound effect instance instead of sound effect because we want to be able to turn the Play button from play to pause and handle pausing, which is something that we don't get updated for if we're just using sound effect. If we just used a sound effect object to play it, it would simply play, and that's it.
In addition, we also have a playbackStarted button, again, the same kind of thing we use for in recording mode when playback is started and you touch the button, we know that we should pause versus start playing over again. Now, we've added a little bit more code to the timer callback. Originally we just had this FrameworkDispatcher.Update. Now, we not only want to run the game loop, we want to update our user experience based on the state of the playback. This is how we know how to switch the sound. And we can't just do this when you tap the button, because we want to know when the playback has actually started.
So we keep our playbackStarted flag and every time through the timer here, first of all we say, oh, are we actually playing something? Great, if we are, then let's see, do we think playback has started? If not, and the sound is actually playing, then we set our playbackStarted logic to true. If we think we've started playing back, and now the sound.State has stopped, that's when we know that the sound has stopped playing, and we can turn the button back from the Pause button back into the Play button so we can play again. And so you can see when we've detected playback and then the sound finally stops playing, we set the ApplicationBar back to play and the play image and the TimerTextBlock back to an empty string, and set the sound to null and set our playbackStarted flag back to false, because now we're no longer playing.
All of our other recording logic still remains the same, so we don't have to change any of that. Our recordbutton_Click remains the same, but here now we have the playbutton_Click logic. So this is the Handler for our Play button. We, first of all, want to check to see if we're in recording mode, because we don't want to start playing back a sound if we're recording. Then we want to see whether we're in play mode, and we want to pause or we're in pause mode, and we want to resume. So the logic we use says, we come in and look at the text of the button. If it says play, then we change the text and the graphic to pause, and then we say is there a sound? If there is a sound, then that means we were already playing so we simply resume and exit here.
If you touch the Play button, and there is no sound playing, we come down here and allocate a new sound from the stream and play it. Otherwise, if the button text said pause, meaning we were actually playing something back, we pause the sound, set the button back to play and set the TextBlock to pause so the user will see the word Paused on the screen. So let's just run it. So here we are, let's do a little recording 1-2-3-4-5, and we'll stop. It says Recording Complete, now we'll play it back, 1-2-3-4-5.
And now let's exercise the Pause functionality, we'll stop it after 2. 1-2. Now, you can see the TextBlock here says Paused, and if we touch the Play button again, it continues on to play to the end, 3-4-5. So now we've been able to record audio, we've been able to give feedback on both the time and the amplitude of the audio that's being recorded, and now we've actually played that audio back. Now, that's a great app if all you want to do is record one thing and play it back, but we haven't saved anything yet, and so you can't play it back multiple times.
So let's go and Save that in Isolated Storage as a file, as a prelude to being able to create a list of recordings and look at them, which is the final application. So let's go save this data to Isolated Storage.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 102013 Viewers
61 Video lessons · 88729 Viewers
71 Video lessons · 72554 Viewers
56 Video lessons · 104200 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.