This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
You can add sounds very easily to a JavaFX application. Let's add some without being annoying.
Learn more
- Scrum Basics course on Treehouse
- Sound Snap
- File URLs
- Incorporate Media
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
As we saw in that user story,
0:00
we want to notify the user
when the break is finished.
0:02
I think we should also
commend their effort.
0:05
They achieved 25 minutes of focus time.
0:07
They deserve a round of applause.
0:10
Let's go figure out how to do that.
0:12
So before we get started,
we better go take a look at the board.
0:14
It's a bit of a traffic jam, right?
0:18
[LAUGH] Okay, so
we can move this one over, right?
0:20
This one about detailing what
was going on with the Pomodoro.
0:22
We can move this one over to done,
cuz we did that.
0:26
We mocked out the save.
0:28
And hypothetically,
we've completed this story, right,
0:30
about notifying them about the change?
0:35
Now, I want to do a little more than
what was specified by the request.
0:37
That making sound part probably should
be its own ticket, though, right?
0:41
And this is kind of out of the project
scope that we had agreed upon.
0:45
If you imagine this sort of project
planning through user stories,
0:49
you can probably also imagine
that this sort of scope creep, or
0:52
additional changes outside of the plan,
happens all the time.
0:55
Now sometimes it happens
outside the project.
0:58
People ask you, can you also make
the app do this thing, please?
1:00
And sometimes it happens internally,
right?
1:05
We developers see something that
would make something way better.
1:07
Now, the way to handle to handle this
is to discuss, both with your team and
1:11
the stakeholders of the project.
1:15
It's possible that the thing that we're
going to add is way lower priority
1:17
than what the client wants to build.
1:21
And that the added work is gonna make us
miss our deadline that we agreed upon when
1:23
starting this work.
1:26
Anyways, the best thing
to do is to communicate.
1:28
As we're in a simulation here, I've talked
it over with the powers that be and
1:32
everyone's on board with
adding the applause sound.
1:36
Now our pretend stakeholders
think it's a great feature.
1:39
Plus, wait till you see how
easy it is to add noises.
1:42
Now, I talked to our pretend
development team and
1:45
I told them that we could probably
knock this out in five minutes, right?
1:47
And now I just need to
figure out how to do it.
1:50
Shh.
1:53
Okay, so I'm gonna use my favorite
search engine to look about how to
1:54
add sounds to a JavaFX 8 application.
2:00
Perfect, so there's a bunch
of stuff that's showing up.
2:07
There's some game tutorial stuff.
2:09
You'll see some old things that came
up even though I have JavaFX 8.
2:10
So let's be careful of that.
2:12
Here's one that says audio clip.
2:14
Let's open that up.
2:15
Okay, so this is the Java doc for
an audio clip and
2:17
it represents a segment of audio that
can be played with minimal latency.
2:19
Sounds great.
2:22
Playback we have here is fire and forget.
2:24
It loads in memory and it's for
just really quick ones.
2:26
It's not efficient for long clips,
especially if they're compressed.
2:29
So let's just go ahead and
we will do that.
2:33
And if you look here,
it shows all you need to do is call it.
2:36
And it's giving it an outside URL.
2:38
Which is okay.
2:41
But, we kind of want ours to work
without the internet, right?
2:42
We want to get it to run offline.
2:46
I definitely want to be able to focus
when I'm somewhere without the internet.
2:48
You know, like on an airplane
that doesn't have wi-fi.
2:51
Those still exist, right?
2:55
Okay.
2:57
So, you get a handle on it,
and you call play.
2:57
So here at Treehouse we use
a tool call Sound Snap.
3:00
And I went and I found an applause mp3 and
I downloaded it and
3:04
I've added it to the teacher's note.
3:07
So why don't you download it now,
and I'm gonna jump over and
3:09
in my resources directory I'm gonna
make a new directory for sounds.
3:13
Great.
3:21
And I'm going to open
up my download folder.
3:22
All right so here's my downloads and
I've downloaded the applause mp3.
3:25
I'm going to drag that
into my sound folder here.
3:28
And of course it's going to talk about it,
says do you want to move it?
3:31
Yes I do.
3:34
Says it doesn't belong,
I want to do it anyway.
3:36
All right, okay.
3:40
So let's create one of these new audio
clips that we just learned about.
3:42
Let's do it in the constructor of home.
3:45
In the constructor here
we have this public home.
3:49
Let's go ahead and
we'll just say mApplause = new AudioClip.
3:52
So we wanna use
the javafx.scene.media.AudioClip,
4:01
is the one that we want,
let's choose that.
4:04
And again we're gonna use that pattern.
4:06
We're gonna use the getClass of
the object and we're gonna chain, and
4:08
we're gonna say, getResource.
4:11
We're gonna specify the resource that we
want and again it's in the root there.
4:14
So we're gonna say /sounds/applause.mp3.
4:18
Now, it's expecting a URL.
4:23
Remember when we saw that HTTP one.
4:24
If we do this at the end here,
4:26
if we say toexternalform, what that
will do is it will generate a file URL.
4:28
You've seen them before where it says
file: and then the file all specified out.
4:32
What that does is that.
4:37
And now we have a working URL
that the audio clip can hit.
4:38
We have not declared this yet.
4:41
Let's go ahead and
say create field, right?
4:43
And it will pop up the private field
audio clip here and it's nApplause.
4:46
Perfect.
4:51
Okay.
4:51
And then in the onFinished handler, now
we could make a separate method for it.
4:52
I don't know if we need to just yet.
4:55
Let's hold off on that.
4:57
Let's go into this onFinished,
right here, when this is all done.
4:59
Let's go ahead and let's play it.
5:03
So, we'll just call nApplause, and
all we need to do is call play.
5:04
Awesome.
5:09
So, if we look back over in our
AttemptKind, I believe that we left focus.
5:10
We're only at three seconds, right?
5:15
So, we've got to remember
to flip that back.
5:16
So, focus is at three seconds.
5:18
So, let's go ahead, and let's run this.
5:19
Awesome, and we're gonna restart.
5:23
And when it finished, the applause is.
5:25
>> [APPLAUSE]
>> [LAUGH] And we did it, blam.
5:28
Just made it and
5:33
I think we did it in the five minutes that
we suggested that it would take, right?
5:34
So this is one of those times
where you're probably going whoa,
5:37
I wonder what else this thing can do?
5:39
Well check the teacher's
notes for more awesome.
5:41
Awesome, super easy right?
5:44
Now just because it's easy doesn't
mean you need to go overboard.
5:46
Now I've some of these timers implement
the clicking on each second like, [SOUND].
5:50
Let's not do that, right.
5:56
But if you did, remember to make
the [SOUND] only load once.
5:57
It seems like we have all the states
defined that our stories would lead us to
6:01
believe are required.
6:05
What do you say we go ahead and
wire it all up?
6:06
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up