[MUSIC] In this video, we're going to talk about playing sounds in CodeSkulptor. Now playing sounds in CodeSkulptor is actually fairly straightforward. The biggest problem is actually, finding the sounds. So we're going to provide all the sounds that you need for this class. But as you make your own things or you try to embellish your game, you're going to need to find your own sounds, and trust me, that will be the hardest part, okay? I also would like to point out, that I got my tie back from that pesky owl. I don't know what he was doing with that thing. I want to demonstrate sounds in CodeSkulptor, by using a simple music player application. So let's look at it first. All right, so here it is. I can play sounds, by hitting the play button. [MUSIC] I can pause. I can start it again. Pause it again. Rewind, start over from the beginning, all right? And you can play more than one sound at a time. So I've got this sound going on and. >> [LAUGH] >> I can play the evil laugh. Now you can thank Joe for that crazy evil laugh, [LAUGH] I had nothing to do with that. All right, anyway, so the basic idea here is I've got three operations I can do, play, pause and rewind. And I can do that with as many sounds at a time as I would like. So let's look at what happens. If I scroll down here, you can see I've got the buttons play, pause and rewind. They refer to the handler's play, pause and rewind, with the obvious names. So here's the play handler. What does it do? Hm, music.play. pause handler. Hm, music.pause. Rewind handler. Hm, music.rewind. Well, what is music? Well, let's go find that. Okay, here we are. We are simply calling, simplegui.load_sound. That gets us our music. So in CodeSkulptor, we tried to make things very easy here with sounds. You simply load the sound and then you have three options. You can play, pause, or rewind the sound. And you can rewind and play the sound, as many times as you want. You can have as many sounds as you want, playing at the same time. You can also set the volume of a sound, all right? So while I'm playing here, I can turn the volume down. It gets quieter until 0, can't hear it. I can turn it up, make it louder. All right, so how do I set the volume? Now, that actually was also an interesting thing, right? I closed the window and the sound was still playing. I have to hit the reset button, if I want to stop the sounds after I'm done. Just like timers are not associated with a frame, they're actually associated with CodeSkulptor. Sounds are also associated with CodeSkulptor, not the frames. So you're going to have to hit the reset button, to stop the sounds. Now getting back to the volume, I've got two buttons here for volume down and volume up, okay? Here are the two handler functions, for volume down and volume up. You can set the volume, music.set_volume, to a number between 0 and 1. 0 is off, 1 is maximum volume. So you can see here, that we're actually keeping it as a number between 0 and 10 and then we're dividing by 10, when we do the set of the volume, all right? So that allows me to get different volumes, to raise and lower the volume. You should set the volume in the way that you like the sound. So CodeSkulptor, makes it pretty easy to deal with sounds. However, there are some issues. So if you look down here, we're actually using .ogg files, all right? .ogg files are ogg vorbis files. They work in both Chrome and Firefox. They don't work in Safari, however. Now there are other file formats that you can use, that will work in different browsers. But I have yet to find a particular file format that will successfully work correctly across all three browsers. What's the problem here? Well, each browser has its own set of codecs, that allow it to play its own set of file formats. So any music that your browser can play, CodeSkulptor can play. So you can test a different webpage, if you're able to play the music. If so, you will also be able to play it in CodeSkulptor. However, this makes for some problems, right? You can't be guaranteed that everybody else can play your songs. So, whenever you load some songs, you should put clear comments that say what browser you're using and what the file format is, so people can understand that. There's one additional problem. Firefox, in particular, for reasons that I don't quite understand, slows down when you start playing sounds. So, if you have a game that has lots of sounds in it, your game is going to start to slow down in Firefox. So for that reason, I highly recommend that at this point in the class. That if you've been using Firefox, you switch over to using either Chrome or Safari while you're doing games with sound. This will make your experience much more pleasant. So that's really all there is to sound. You can change the volume of the sound. You can play a sound, you can pause a sound, and you can rewind a sound. You can have as many sounds going on at once as you want. And you can replay sounds as many times as you want. And the real restriction here, is that you can only play sounds that your browser actually supports, okay? Now that you've got sound, you can add a lot of fun to your games.