So now that we've seen how a simple package such as the English words package works, and how it gets
downloaded and incorporated into our project and automatically updated as well every time we run packages
get, we're now ready to go ahead and incorporate a audio file playing package.
So let's go ahead and just search for it.
Let's search for play audio file.
Now sometimes you might have to play around with the search terms a little bit in order to get to the
package that you need.
But if we scroll through this list, we can see that we probably don't want a video player.
We probably do want something that plays audio files simultaneously.
And there's also this one which plays audio files.
There's something that allows us to play audio in the background while the screen is off, which sounds
less like what we want.
And so we're kind of choosing between audio players and audio player.
Now notice because the authors are naming these names,
they might often sound very similar.
And this one's only different by an s.
So let's take a look at the scores. Audio players has a score of 96 whereas audio player only has 93.
So let's pick this one with the higher score.
Now you've already seen how we can incorporate these packages into our project.
I'm going to set this as a challenge for you.
Go ahead and incorporate the audio players' plug in into your Xylophone project going through those three
steps that we did in the last lesson.
Now,
it's important that when you're programming, don't feel like you have to memorize anything.
It's important that you understand what's going on.
But there's always the docs and Google whenever you get stuck, whenever you can't remember how to do
something. Google and docs, they're your best friends as a developer.
Don't ever let anyone make you feel bad for looking up things in Google or Stack Overflow or in the
docs such as how to add a package.
There are so many places that'll tell you how to do this.
As we mentioned before, when you're using packages the first place to go when you're installing it, is
of course the installing tab.
And usually here it will have the instructions for you to install the package.
So let's follow their steps.
So first things first.
Let's go ahead and copy this line of code and add it to our dependencies.
So that's inside our pubspec.yaml and we're going to delete the English word package now because
we don't need it anymore. And we're going to paste in our new package that we need.
So I'm going to go ahead and run packages get as the second step, and that will remove our English
words package that we had from previously.
And it should include now our audio players package. So let's take a look inside our Dart packages once more.
The English words is gone but the audio players is now in.
So we're ready to go ahead and import this new package.
We can go ahead and simply copy this line of code and replace where we imported our English words with
this new import instruction to say that in this file, main.dart, we want to use this audioplayers
.dart file that we incorporated just now. So now that we've got our package in our project, we're ready
to actually use it to play some sound.
Let's take a look at the read me to see how we can do this.
Now it's important to read through most of the documentation before you get started because you'll discover
that there's a section called Playing audio which seems like the right thing to use.
But if you scroll down a little bit more,
it tells you that for local assets, which is what we have, files that are packaged together with our project,
you have to use the AudioCache class.
So let's scroll down and find it.
Here it is, AudioCache.
And this plays local assets
and here's the full documentation.
So here it tells us, this is how we play an audio using AudioCache.
First we have to switch out the import from importing audio players to importing AudioCache. So let's
copy that and let's replace this line of code.
And what that does is it tells our project to use this particular file that plays audio instead of this
file that plays audio from the Internet,
so remote files
right?
But what we want is to be a to play it from our assets folder.
This is the one that we need. Now,
the next thing it tells us to do is to create a new player using the code that we just imported. So we're
going to do that in here.
I'm going to delete all of this and instead of having some text, I'm going to have a flat button. And
this flat button will have a onPressed that gets triggered when it gets pressed, and the child is just
going to be some text that says 'Click me.' Now, what should happen when this button gets pressed?
Well, we want to play a sound from our assets folder.
So we have to go and build our player first.
Now this code is a little bit outdated and there's a new way that we would write this in Dart 2.
In all of this course, we've been working with the latest syntax or the latest way of writing Dart code,
and that's Dart 2.0.
So in Dart 2, the way that we would create audio player is we would write something like final. And we'll
give it a name, we'll call it player.
We're going to set it to equal the player that comes from audio cache, so we'll say audio cache.
And finally we're going to finish off with a set of parentheses.
So that line of code is all we need to do to create our new player that plays cached audio, so audio
that's in our local assets.
So now that when our button gets pressed we create a new player and then we just have to tell it what
to play.
And we specify the name of the file inside the parentheses of this play function.
So let's go ahead and type that out.
Let's say player.play and the file name
Let's play note1.wav. Now, notice that previously when we were using images and we were specifying
a file that we want to display, we would add in the name of the folder as well.
So in this case, we would be writing something like assets/note1.wav. But
very helpfully, the audio players package actually already assumes that your file is inside a folder
called assets, and you don't have to write the full file path. All you need to do is to give it the name
of the sound file that's inside the assets folder.
So it's very important that your file structure looks like this. And this is why all of our sounds are
inside a folder that's called assets.
And if we changed the name of this folder, then it won't work.
But on the plus side, we can now write much less and it looks a lot better when we're saying our audio
player should play this particular sound.
So that's the theory of it.
It's time to actually put it into practice and run it and see if it works.
Now even though we're used to hitting save or using the hot reload,
in my experience when you're working with audio players and you've just set it up to start playing, the
first thing to do is actually to stop your app if it's currently running and restart it from cold. And
this allows it to update things in the background and actually make sure it gets hold of the right files
for you. Once that's done, you should see our flat button in the center of the screen that says the words
'click me'.
And when you click on it, you might get a pop up that asks you for permission for Runner, which refers
to your iOS app.
Now if you're running your app on an Android emulator, then this shouldn't be a problem.
Similarly if you're running it on a physical device such as your iPhone or your Android phone, then you
won't see this pop up either.
But make sure when it asks you for any permissions just go ahead and click ok and allow it.
And now every time you click on the button, we hear note 1 being played.
There's usually a lot of output in the console and it's telling you all sorts of things like what it's
trying to do.
Now all of this text actually comes from the audio players package that we incorporated.
But as long as you don't see any red or any errors, then don't worry about all of these messages too
much. Success right?
We managed to get a sound to play with very few lines of code.
We didn't have to do very much for this to start working. And that's all thanks to incorporating these
open source Flutter packages and powering up our app to be able to do so much with so few lines of code.
Now in the next lesson,
we're going to try and see how we can play all of our sounds by hooking them up to different buttons.
So for all of that and more, I'll see you in the next lesson.