Hello and welcome to another episode in the Godot Basics tutorial series. In this episode, we will be taking a look at the animation player for audio. Now, on top of doing interpellation animation through the animation player, we are also able to use the animation player to play audio. One thing to note is that to play audio through the animation player, you will need to use either the audio stream player node or the audio stream player to denote they're both basically the same thing with one difference, and that is audio stream player will play audio evenly, whereas audio stream player 2D plays audio depending on the notes position in relation to the main camera you are using in 2D.
So setting up music in your animation players quite simple. First, you need to set the length of your time for your animation to be either the same length as your song or greater. Next, you have the option to loop your music so you can set the loop property to true. This is optional, but keep in mind that the loop property by default is set to force. After that you can add a track type or rather you need to add the track type. But keep in mind, your track type must be the enum value type audio.
Next, you need to load your music resource into a variable, preferably. Next, you need to set the path to your audio stream player node because again, to play your music, you're going to need either audio stream player note or audio stream player to denote. And then lastly, you need to insert the audio track key with your track index value, which again holds your track type, which is type audio. Now, the one method you should focus on understanding when it comes to audio tracks in the animation player is called the audio track insert key method.
It takes in five arguments. Three of those are mandatory. The last two are optional, but by default they don't do anything. So let's go over them. So the first argument is your track index, which again will have the track type called type audio. The second argument is your time. Basically, what position in your track would you like your audio resource to start at? The third argument is your resource file, which is just your music file that you would like this audio track to play.
The fourth argument is your starting off set value, which is a full value, which just represents how many seconds from the start would you like to cut? And then the last argument is your ending offset, which just means how many seconds from the end would you like to cut by default? Both the fourth and fifth argument value is set to zero, which means you do not want to edit out anything from your audio file. Let's go ahead and take a look at some code. So if we look at our scene tab, you're going to see our audio stream player note, which is what we need in order to play our audio file.
And of course, we need our animation player node, which has a script. Keep in mind that this script is what we will use to play our animation player resource. Now, if we take a look at our animation resource class, which is called Resource Music script, you'll see that we extend from the animation resource class. We give it the name animation resource so we can create an instantiated object with the new method. As you can see here, we have a function called Get Music Resource, which returns back in animation resource data type.
And just like the previous episode, we will be returning back using the self keyword again, self keyword being or animation, because we are extending from the animation resource class. The first step is to set our length to be equal to or greater than our music file. In this case, that will be thirty nine seconds. Then we can choose to set the loop, which just means whether or not we want our music to loop. And in this case we set that value to true. So our music will now loop. Next we have next we have the track index variable which we will assign its value using the add track method.
And keep in mind, we need to pass in a track type. In this case it's type audio because we want to deal with audio. And then next we have a variable called Music Stream Object, which is just our music file. In this case, we use the preload method and it takes in a string value, which is just our firepower. And I am just passing it a V file called simple melody. One thing to note is that good dough doesn't handle MP three well. So if you want to use music, just keep in mind you will need to use a W a V file.
Next, we use the track set path method. The first argument will be our trick index and our second argument will be the node. Health, which, again, is just a string value, and in this case, you can see we're accessing the route followed by the route to node, which is called No Tutty, and then it's child notecard audio stream player. And then last but not least, we use the audio track insert key method. The first argument is our track index. Again, the second argument is at what position in our truck do we want to set our music at? And in this case, we would like to start it basically immediately.
And then the third mandatory argument is our music file and our music file has been loaded into the variable called Music Stream Object. And in the animation player in the ready virtual method, we use the add animation method. We give our animation name the value resource music. And the second argument we pass in is a variable called Get Music Resource, which is just our animation resource. And then with the play method, we can go ahead and play the music.
As you can see.
And that was basically the music I hope you were able to hear that I rooted my computer's audio so I cannot hear it, but on your side, you should be able to hear it. It's nothing special, just the simple melody. But through code, we can, in fact, play music. Now, let's go ahead and look at a way in order to create the same thing without code. So, again, if you click the animation player button, you're going to see towards the bottom are animation. Unitab opens up and in here we can create our animation. In this case, if you click the animation button press and you give it a name, in this case, we're going to give it the name music.
Press the button over here at the bottom, you'll see that we have our animation set called music right here. Now, the first thing to do is set the length. So again, in the top right portion, you're going to see the clock. And next to it is our time. In this case, the music files. Thirty nine seconds. So I'm going to set the value there. This button right here next to your seconds is the button that allows you to loop your animation. Since we want this music to loop, we're going to go ahead and click it. You're going to see that it relates to blue, which means it's active.
So our music will now loop after the full thirty nine seconds as played in the top left corner, you're going to see the track button. You can go ahead, click that and are six different track types are available. We'll go ahead and press the audio playback track. First, you need to pick your audio stream player note or audio stream player to denote in this case, we pick the audio stream player press, OK, and you can see here that it's there and all you really have to do is drag and drop here w a V file. And here we have a file called Simple Melody.
You just go ahead, click drag it. Going to have to eyeball that. And if you don't feel like eyeballing it, if you go to the top right in your inspector tab, you're going to see time. And over here you can set the time more specifically. And of course, you'll see three other options. The first is our stream value. In this case, we have our audio file as our stream data type. We have our start off set and we have our end offset. And that's basically it when you want to create your own audio stream using the animation player node. Now, if we go ahead and press play, you're going to play music only because we still haven't changed what we're playing in through the animation player script file because we're playing from the get music resource variable, which again has been labeled with the resource music string name.
And that's what we're playing. So we need to change that. And so in this case, we call it music and lucky for us, it's given it out as an option. And now if we press play.
You're going to hear the music. Well, that's all I have for you in this episode. Thank you so much for joining me. Thank you for clicking the like button and thank you for clicking the subscribe button. If you have any questions or comments, please feel free to leave them in the comments section down below. Don't forget to download the GitHub file and play around with the project, and I look forward to seeing you in the next episode. Have an amazing day!