So according to the second scenario excerpt, we're supposed to create a music Diversifier phonograph.
And basically what it's supposed to do is act as a playlist for the user to be able to play songs that
are English songs, Moroccan song and a Lithuanian song.
So we're going to use just one song for each of these languages.
So to begin, you're going to go to file and you're going to select new and then you're going to create
a Java project.
You will call this project Music Diversifier.
And once you have done that diversifier, you will leave your settings as they are, as usual, and
click Next, you'll be able to see your Java project and your source folder.
Now click finish.
The next thing you're going to do is once you expand your Java project, you're going to right click
on your source folder and you're going to select new and then package and you're going to call this
package music playlist if you would like, or you could just call it Phonograph Music, Phonograph.
I think I'm just going to use the same information as provided in this scenario and I'll click finish.
Now the package is created and now ready to create your class.
I'm going to right click on the package and select new and class, and I'm going to call this class
music playlist and I'm just going to try to differentiate it from the name of my Java project.
Since this is the only class that I'm going to be using to create this program, I'm going to make it
have this public static void main method.
So I'm going to check the box and click finish.
Now, once the class is created the same thing as we've done in the program, for the first scenario,
we need to import certain packages in classes.
So what we're going to do is we're going to start by importing Java utility package.
So import Java, utility, asterisk, and then a colon.
The next thing we're going to do is import the file class.
So import or the file package, import EO file.
And remember that this is to be able to create the file object.
And once we've done that, we're going to just put the colon and import Java.
Don't forget to put the java over here.
The next thing we're going to do is we're going to be creating.
Oh, by the way, please take away the dot.
So it's just import java, dot IO dot file and then we're going to now import our sound sampled package.
So import jar, box, dot, sound, dot sampled and then dot and then asterisks means importing everything.
You may also want to import the i o exception.
So that's going to be handling any errors that may occur with not being able to read the file path for
the audio.
So what you're going to do is import, you're going to do import and then you're going to do Java IO,
just like we've done in the previous program.
IO and then exception.
IO exception.
So in case there's any problems with reading the audio file and once you have all of this imported,
all your packages imported, you're now ready to begin.
So the first thing you want to do is to create a scanner class.
So you're going to be creating your scanner object.
Sorry, object of your scanner class.
I'm going to call this scanner.
I'm just going to call it scanner new and it's going to be equal to the scanner class and then, of
course, start system and then new scanner.
Sorry.
So new scanner.
Don't forget that I forgot the new keyword and then open parentheses and you're going to put system
in.
And of course, the colon, once you're done doing this, remember, what we have to do is we have to
create of course, we're going to welcome the user to playlists to the Music Diversifier phonograph.
So to do that, we're going to trend message.
So we're going to do System.out.println.
And we're going to say, welcome to the music.
Diversify our phonograph and we'll ask them, what would you like to play?
So we actually want to get the user input first before we start to play the music.
So we're going to say, Welcome to the music Diversifier.
So I'm just using a normal Len here.
I'm not using a formatter.
You can use a formatter if you want, just to format it in a specific way.
So I'm just using a simple Ellen So welcome to the Music Diversifier Phonograph program or phonograph,
basically.
And then I would ask the user, what would you like to listen to?
And then, and then we're going to give the user the option.
So front slash end option A you could do an option A if you prefer, or a number one, depending on
how you want to write the options.
So I'm going to say, what music would you like to listen to?
I'm going to add music right here.
What music would you like to listen to and what music would you like to listen to?
And that's a question.
And so front slash end was for my English and then another front slash, and that's going to be for
option B, which is going to be Moroccan music.
So Moroccan and then English music.
I probably would just put the musics in front of every one of these options.
And so here was going to be Moroccan music, and then I'm going to do a front slash and as well, and
this is going to be my option C and that's going to be for the Lithuanian music.
So Lithuanian music and then that's it.
So I have just three options.
If you want to add some more, you can add.
And then I'm just going to close this with a colon and then we now come here to declare a variable that's
going to be storing the user response.
So I'm going to create a variable and I'm going to call the string user.
You can call it user response, and that's equal to my scanner object that I created, which is scanner
on the score new dot next line, because it will be reading string texts, which is options A, B or
C, and then circular brackets and column.
Now, once that is done, I'm going to hit enter and I'm going to say if user that means if the user
chooses option A, which means English song or English music.
So I'm going to say if user equals a very similar to what we've done in the previous program for the
first scenario and then call on the curly brace.
So they choose option A, this is where we start to create the file object.
Then we create the audio stream object to be able to open the audio clip and then we give them the option
to play the music.
They will also have the option to stop or pause the music and then resume the music.
So it's slightly different from the program of the first scenario.
So here we're going to create the object of the file class.
So we're going to say file, and I'm going to call this file again.
So that's the name of the object of the file class, and it's going to be new and then file one more
time.
And then in brackets you have to put the name of the audio, which is going to be for the English music.
So according to what I have here, I already have my music downloaded.
So I'm going to be using for the English one I'm going to be using read.
So what you can do to do this is once you have your audio, you can just drag your audio in here and
place it in your Java project.
So music diversifier.
So place it in there and it will be you can just drag drop or you could copy paste it on your Java project
and once you release it, it will tell you if you want to copy your files, if you say okay, and it's
going to be copying the files as a way file and make sure your audio is away.
Don't forget, and I can repeat this for all of my other audio files, so I'm going to do them as I
go.
So the first one is read Wave.
And according to the scenario, we're supposed to give some information about the audio, about the
music.
So I'm just going to be saying this is the English music and it's from certain in American English music
or Lithuanian English music, just a very brief information about the music.
And so here for the first one, I'm going to put a call in.
So once you've created the object of your file class, you're going to put a column and the next thing
you're going to do is you're going to create an object of your audio input stream class.
If you remember from the previous program we created for the first scenario excerpt, So audio input
stream, and that's going to be again, the name of you could still use the same name of the object
audio stream or you could change it is equal to your audio system.
And that's just going to be get audio input and that's going to be getting the file object that you've
created here, which automatically comes in.
You will have to throw the exception.
Remember, throw the declaration, which is going to be handling the exception.
So click on throw declaration and that's going to handle the exception.
And the next thing you want to do now is to create an object of your clip class.
So clip or clip interface, and that's equal to audio system.
And then you're going to do DOT get clip.
And so it's going to be getting the audio clips so that it can play it.
And then you put a call in.
You will also get an error here because you have to throw another exception or declaration that's going
to be handling any problem.
That has to do with not being able to read the path of the audio file from where it's stored.
And then the next thing you're going to do once you're done doing that, is you're going to use the
method clip open so you can actually open the audio file so it can play.
So audio stream, that's the name of the object that you've created for the audio file.
And don't forget to put the call in.
Now, once you've done doing that, you're going to be asking the user to enter a certain value so it
can prompt to play the audio.
So I'm going to do persistent output print and I'm going to be asking the user to enter P to play.
So print Len and I'm going to say enter similarly to what I used before in the first program.
So entropy to play audio.
And so that's the message I have over here.
And I'll put a call in and once I've done doing that, I'll now create a variable declare variable to
receive the response from the user.
And so string response is equal to my scanner object, which is scanner new and then dot next line,
remember?
And then once you do that colon.
And so I would like to get the response in uppercase regardless of how they write the P, whether it's
small or lowercase capital.
So to do that, very similar to what we've done in our previous program, I'm going to write response
and response again and then dot to upper.
So to get it to uppercase, so dot to uppercase, you just select the option and then I'll put a call
in at the end.
So this gets the response in uppercase.
So this opens the audio clip, remember the audio stream, and then it basically asks the user to enter
P to play.
And once the user enters P to play, then now we're going to go and check what the users response is
because we said entropy to play.
So we're going to use the if conditional statement and we're going to say if the response of the user.
So if responds dot equals and then of course speech equals capital P since we're getting the response
in uppercase anyways, with this line of code, we're going to put the curly brace and we're going to
use the method clip dot start and we're going to start the clip playing.
And then of course, the next thing we want to do is to keep this going, like I've mentioned before.
So keep the.
You know, the audio playing as it goes.
And so the next thing we're going to do is to prompt the user and ask them if they would like to stop
the audio from playing or they would like to pause it.
So we are actually going to include a pause option here this time.
So I'm going to do a system that print Ellen, and I'm going to be asking the user and then saying,
Well, we're going to give the user options.
So I'm basically just going to say S is equal to stop.
So you could just say enter so that the user understands as to equal stop.
So basically equals stop the music and then p a is basically to pause the music or you could use something
else if you'd prefer.
So p a is equal to pulse, so depending on what the user will enter.
So we're going to go, oh don't forget to put the T please.
So print Ellen and now we're going to receive the user response.
So we're going to use the same thing that we have over here.
Just copy the response variable that you've declared over here.
Change it to uppercase and paste it down below here, and that's going to receive the user response
and change it to an uppercase.
And so now we're going to come down and do another nested if and say if the user responds.
So if response dot equals, so if the user responds to s, if the user would like to actually stop the
music because we're giving them two options of either stopping or pausing the music.
So if the user would like to stop the music, then we're going to use this method of clip stop so the
audio can stop playing.
And then of course, we're going to just tell them a tank.
You can display a thank you message right here and you could just tell them thank you.
And you know, once you're done, then you will put your call in.
What about if the user actually selects P to pause?
So we're going to do the same thing that we've done.
You can copy this block of code and paste it down below.
And if the response the user answers is PA, which means pause, pause the music.
And so that means we're going to be putting a different set of block inside of the if statements.
So if response is p, we're going to use this data type that we haven't used before called long.
And this data type is basically representing long digits of number, like long whole numbers like you
say, 235,000 or 2 million.
So long is usually just like an integer data type, but it's used to represent longer digits.
So we're going to use long and we're going to say clip and we're going to be writing clip.
We're going to declare a variable so that we can get the time position of where the user wants to user
hits or enters P for pause.
Then it's supposed to work.
We need a variable that's going to be storing the time position of where the audio is actually pause
so that we can if the user clicks resume, it can resume playing from that time position.
So we're creating a variable called time position and that's going to be storing the time position of
the audio from where the user stopped it or paused it.
So clip we're going to use clip which is the objects that we created of the clip class.
So clip gets and we're going to use get microsecond length so it can be able to get microsecond position,
sorry.
So it can be able to get the position of where the user actually pause the audio.
So long clip time position is equal to clip to get microsecond position.
So this will handle the pause.
And then once we've done that, then we're going to go back and we're going to do clip that stop because
it will get the position of where the music is paused and it will stop playing the music since it's
paused.
And then next thing we're going to do is we're going to give the user the option if they would like
to resume playing the music.
So I'm going to copy this block of code and I'm going to paste it down here and we're going to give
the option if they would like to stop or if they would like to reset, maybe.
So we're going to change this to our reset.
If they would like to reset from the beginning or if they would like to resume the music, or you could
just do resume so you could just tell them enter our to resume.
Or you could also do the option if they want to just stop it altogether like you've done before.
Or you could just say Enter are to resume.
Now.
This will prompt the user to enter something and if they decide to enter our for resuming, then we
are going to again use another nested if and we're going to say, okay, so we're using the same variable
which is response, same variable name.
So if response equals in this time equals to our to resume, that means they would write to resume from
where it stopped.
This is the reason why we wanted to get the microsecond position.
That's the exact time where the audio was stopped.
And so if they want to resume it, then we're going to continue by resetting it back all the way to
the beginning.
So here we're going to do something as if they want to resume.
So we're going to say to resume and we're going to go all the way back to clip and we're going to set
set.
We're going to use the method set microsecond position, but this time it's going to set it so it automatically
even does it for you.
It's setting it to the clip time position.
That was actually the variable used to store the the length of the audio from where it stopped.
So it's going to set it back to that length to continue playing.
And then we're going to continue playing it by saying clip up, start, and then we'll put the the circular
brackets in the colon.
And then.
Here.
We're going to tell the user if they would like to end the exit, just like we've done before so we
can copy this block of code again and we can just paste it down here and say, Enter E to exit.
So just as we've done in our previous program, so enter E to exit and this means to stop the whole
thing.
And we're just going to write a message saying system not print.
LN And we're going to tell them thank you.
And there is one more condition that we have to do is to finish this block off is what about if the
user actually enters as to stop.
Now you can do that block if you want or you may not do it if you don't want.
It depends on you.
So remember that it is all in the pause section.
So this is where if the user actually enters response, you could copy this part right here, this block
and just paste it down right under when this if statement ends.
So don't paste it inside and just change it to when the user actually enters as to stop.
So I mean to reset.
Sorry, this is actually stop.
Or if you want to make it a reset, that will also work.
So if you want to do like an r e to reset, I'm just going to change this to an E, So if you want to
prompt the user for them to reset it from the beginning.
So if the responses are e so reset, then it's just going to set the micro second position back to a
zero.
And so that means it's just going to take it back to zero from where it was in the beginning and it
will start all over and start the audio all over again.
And so that is that for the part of getting the music to play.
And this is for if the user decides to select option A and option A, which is basically to choose the
English music.
And so it starts by if they select option A, they get to play the music, then they could stop the
music if they want or they could pause it.
If they pause it, they could resume it, or they could basically reset it back again to the beginning.
So if you want to click here because you're going to do the second option, what about if the user chooses,
you know, the other option, which is going to be option B, and that's the Moroccan music.
So you're going to copy this entire block of code.
Just make sure you're copying it properly so you can click on the curly brace and where it ends.
You basically copy all the way from up bottom up, and once you copy that, you come down to where the
curly brace of the first.
If condition ends, We're not using a switch statement for this one.
So where it ends is right here and we're going to paste it down here so we can do the second option.
So you're going to do the exact same thing, but you're changing it to option B so you could do an LCF.
All right, So LCF means the second condition, remember?
So if the user chooses this option, else, if the user chooses this option, which is option B and
option B according to what you've given the user is the option where they can listen to Moroccan music.
So you go down and of course you have to change the audio name.
And according to the audio I have for Moroccan music, I'm going to use Manal, so I'm going to drag
this and I'll drop it down in my program project as well.
So just release it on your Java project and copy that audio.
So it's right there now.
So I'm going to just change the audio name to Manal Dot Wave and then of course everything else could
change because this is my second option.
So I would like to do this as file to audio stream to file to again clip to clip to audio stream two.
So I just prefer to change this.
The response can still be the same response.
So it's okay if you don't want to change the response, that's fine, but I'm just going to do this
as clip two.
So because I've changed the clip to in the File two and everything else that relates to the clip will
change.
The response will stay the same, the scanner will stay the same.
This is now related to clip two and you could change this to clip time position two so it doesn't clash
with the first time position.
And this is our clip to our clip two and the start clip time position two and then here as well, everything
of response will stay in scanner new.
And then this is going to be our clip to our clip two and then everything else is same.
So now that we've done the third music, we're going to copy, click on your condition, see where it
ends, and you're going to copy that whole block and you're going to paste it again for the third music,
and that's going to be for the Lithuanian music.
So you copy this block and then you paste it down here where it ends, basically.
So mine it basically ends here.
Just double check.
So you paste it as a third condition.
And so the third condition will be the elseif option B, option C, one thing we forgot to do was to
put an information about the artist according to the scenario so you could just print a message just
before you play.
You say print a play.
You could just which I'm going to be adding very briefly.
Let's finish this part first.
So if the user equals choices.
C So if they choose option C according to option C, in our first previous option for the Music Diversifier
phonograph, that's going to be for Lithuanian music.
So if they choose Lithuanian music and that's going to be our option.
C then I'm.
Going to go and I'm going to pace the Lutes wedding music, which is going to be Lithuania.
I'm going to drag it and copy it here.
On my Java project, I'll release it and again I have it right here, so I'll change the audio name
and that's going to be for Lithuania.
And just make sure you spell everything right as it should.
And this is going to be my file three now.
So this is for my third option and file three and this is going to be file three as well and just change
everything in terms of the clip and the file.
So you only need to change the file one.
So just change it to match your third option.
So this is my audio stream three and then I'll do the same again.
So clip is now, clip two is now, clip three.
So that's for my third clip my third audio.
And here is going to be clip three as well.
And here is going to be clip three.
And then here as well.
It's going to be clip three.
So just change everything.
Clip three here.
Don't make a mistake and make sure you're doing everything right and change everything to clip three.
So here is going to change the clip three as well.
And clip three You don't need to change the response or the scanner new because that's handling the
the position.
So I forgot to change this one to clip three.
So it matches.
That's why I was getting an arrow over here.
And then here too, you would have to change anything else.
So just double check.
Make sure that you've gotten everything into a clip three so that it doesn't give you any syntax or
any errors when you're playing the audio.
And the next thing I want to do very briefly, now that we've finished all of the three different musics
to play, we're going to go back and give some information about the the music itself.
So in the beginning here for the first one where it's playing option A and that's English music, I'm
just going to write.
System.out.println.
Len I'm just going to say that this music is, this is the English music.
This is an English music.
So this is an English music by called I think it's by Emily Sandy.
So that's the music called Read All About It.
So you could just add an information about the music.
So this is an English music by Emily Sun and this is the music is called Read All About It.
You could add some information there.
And for option B, I think I'm just going to put the same So system, not all dot print, so system,
dot, dot print.
So you can just put some information about the music and the artist if you know more.
And I'm just going to say this music, this is Moroccan music, and by so Moroccan music by Manal,
and then I'm just going to close this with quotes.
And then this one, I'm just going to say this is a Lithuanian music so system, not output print and
then.
Len So print.
Len Notice I'm doing that and I'm going to say this is a little winning music.
Och And once I've done that, I'll just put a column over here and that's pretty much it.
You could give some more information if you want about the music, but this is where it will end.
And now you can run this program to see how it works.
So we're going to run the program and we're going to see if our playlist works.
So we'll click on the run button.
And once it runs, it begins by asking us what play what music you would like to listen to.
So I'm going to say option A and it says Click Pizza Play.
So I'm going to click Pizza play.
You've got the words to change a nation.
And I'm going to put Ari to resume or are to resume.
But you're biting your tongue.
You've spent a lifetime stuck in silence, afraid you.
And that's it.
So it stops playing.
And now I'm going to replay this again for the second option.
So that's the Moroccan music.
So be and then plays.
It says this is a moroccan music by Manal.
So I'm going to hit play to play.
Early on in the major.
And then I'm going to hit resume again.
I need to get up to this -- neck.
And now I'm going to repeat this for the Lithuanian music.
So I'm going to do option C, and this is Lithuanian music and I'm going to hit play.
I'm going to hit pause.
So a pause.
And now I'm going to do resume.
And one more run just to show you that it does reset its when you run it.
So I'm going to run it again and I'm going to go with English one more time.
And I'm going to play English.
And then I'm going to reset it from here.
So reset is ready.
So it starts again.
All right, so that is our music Diversifier Phonograph.
I hope you've enjoyed programming this with me and creating this awesome project.
You can modify this to adding more music of your choice and just making it better all together.
So we're going to move towards the end of this lesson.
We've come to the end of lesson nine.
I hope you've enjoyed designing and developing these awesome projects with me and I hope you can modify
and extend them to the way you like.
I hope you've enjoyed this lesson all together.
Stay tuned for the next lesson and take care, everyone.