I couldn't deal with playing sound in Java for my project at university until I watched this video. Thank you very much! From University of Engineering and Technology, Vietnam :D
Thanks Max for coming through this is the only tutorial on youtube that actually works. Really appreciate you taking your time to make this. If you don't mind could you explain a little bit more in depth about the separate thread the clip creates and other ways to work around it besides the joption for future program uses.
Thanks a lot for your kind words, they make doing these tutorials worthwhile. As for details on how Java clip works and how to prevent a thread from ending, I would check out the top answer for this thread: stackoverflow.com/questions/6870816/why-this-code-doesnt-play-the-sound-file They explain how it works beautifully and give a practical way to keep a thread alive.
Honestly, i want to seriously thank you, i have been trying to found a way to put audio into my program in videos of my own language, and several others in english, but they would just not work because they used older versions of netbeans or libraries that my version doesn't have, this is very well explained and actually works, i was just looking how to do the loop, but im very gratefully, i even founded how to stop it , i have no more ways to thank you, but wherever you are thanks, thanks, thanks. I had like three days into this problem
thank you so much for the vid. it is helping my comp sci group program a danny devito magic eight ball. you are a real one. hugs and kisses, your biggest fans
I tried to create a method that would stop the audio clip, but not working. I just changed the clip.start() to clip.stop() on the other method. Am I missing something? Thanks for the video by the way :)
Same problem. I have main class and then class from where I calling playSound method via object, but then I need to stop that track, so I can play another one, without two of them being overlapped.
Hey mate, could you describe how can we get the same stream from an URL? I am trying the same steps with "URL music = new URL(String link), but i get UnsupportedFile exception?
Well, this might be a little late for you, but for anyone else who is also curious: I used the absolute path of my file (in my case, I put "G:/steve/eclipse-workspace/Simple Music Player/src/musicPlayer/PogoSticks.wav" which is the exact pathway of my audio file for the filePath. I'm sure you can use your class path to find it, but I'm too lazy to figure that out, and using the absolute path is a simple copy and paste from my file explorer address bar.
Hey, great video! Is there anyway to continue to play audio without input? I have a game i'm making at it would be great if I could have some background music to the game, but I don't wish to have any inputs after the clip.start() method. Is this possible? Thanks, Shadow (my other alias)
Greetings, I recently uploaded a tutorial on how to make a music player which may be of use to you: ruclips.net/video/OX3CFHLV9ws/видео.html As for what you want to do, I would look at this stack overflow post: stackoverflow.com/questions/34887816/how-do-i-implement-background-music-into-my-java-program
I actually figured it out, I was playing the sound inside the game method instead of the JFrame method, this meant that it only played once and all the other code ran!
I'm using BlueJ 5.0.0 because of school and my problme is that if I export it as a .jar it doesn't find the file anymore. I have it in the project folder and inside BlueJ it runs perfectly fine but after the export it doesn't recognize the file in the .jar archive and searches inside the folder of the .jar
Hello, I am working in Eclipse IDE, and it does not work for me very well, Console: "javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source) at Java.settings.playMusic(settings.java:58) at Java.music.main(music.java:11)"
Hi! I am doing a web Application with Login,Register and want to enable the client to play selected Song from the UI like a online Music app. How can i do it by getting the song from database and send it through a servlet to html page to play it with all play,pause,stop,next and so on.Please help me with this. Suggest any way to attain this
@@maxodidily when I use a bigger sound of duration of 3:30 minutes for example like( Linkin-park In the End.wav) this method does not work ...it works with 0:40 to 1:00 mins duration musics only 😃😃
Hey Max ty for your work. I have a problem. This code works fine, it plays the music however when i export this codes as a runnable jar file, it doesnt play the music. Please help. FIXED it.
Hello. I am using this method of playing music for my school project and I ran into a problem. It works fine when I run it from netbeans, but when I go to the dist folder of the project and run it from there the audio doesn't play. Is there any way to fix this? I already did so much on this project and I don't wat to throw it all away... :(
for me for some reason I had to move the audio file directly into the project folder, anytime i had it in another folder inside of it (such as a res folder and then audio and then the audio file, all inside the project folder) it couldn't find it.
@@jobliar937 I did that and it didn't work, what I think I ended up doing is making res a *package* in the src folder... but I'm not sure as I don't work on that project anymore.
@@varchive8136 If you really want I can send you a link to an entirely different way of running sound files that uses libraries ruclips.net/video/HRaJXVuZjRM/видео.html
HI Max do you know any possible way i can get it to play at the same time the game is playing ( im trying to add the music as background music for a game )
You can just repeat the code again (with different names for your AudioInputStream, clip, file object and filepath). Since this method creates separate threads for each sound, you can play multiple sounds at the same time.
javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source) at Player.PlayMusic(Player.java:18) at LoopMusic.main(LoopMusic.java:8) what am i doing wrong?
I followed his tutorial and worked for my audio file. I think the problem has to do with your path. Have you double checked the path, and made sure you're using double // instead of single / within the path. You must do double due to the slash acting as a meta character, doing double slash cancels the first slash's meta character meaning.
For some reason, I can't find the music path. I've tried renaming the wav files, I've double checked the file location etc. It's just not working for me :(
I don't know if this thread is still active, but I am using the same method as shown in the video, using a .wav file as well but still getting the following error. javax.sound.sampled.UnsupportedAudioFileException: File of unsupported format at java.desktop/javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1066) If someone could offer solutions to fix this, that would be greatly appreciated.
I couldn't deal with playing sound in Java for my project at university until I watched this video. Thank you very much! From University of Engineering and Technology, Vietnam :D
Really happy to hear this tutorial helped! Wishing you all the best in getting your degree.
The combination of memes and programming is absolute genius.
Thanks Max for coming through this is the only tutorial on youtube that actually works. Really appreciate you taking your time to make this. If you don't mind could you explain a little bit more in depth about the separate thread the clip creates and other ways to work around it besides the joption for future program uses.
Thanks a lot for your kind words, they make doing these tutorials worthwhile. As for details on how Java clip works and how to prevent a thread from ending, I would check out the top answer for this thread: stackoverflow.com/questions/6870816/why-this-code-doesnt-play-the-sound-file They explain how it works beautifully and give a practical way to keep a thread alive.
Sorry for the lack of uploads over the past month or so, I shall be addressing it in a video and consistent uploads should occur ceteris paribus.
what happened
Hi,
I get an error at
Clip clip = new AudioSystem.getClip();
------------------------------------
Why?
This actually worked, it’s the first time I could get sound to work in a program. Thank you.
Honestly, i want to seriously thank you, i have been trying to found a way to put audio into my program in videos of my own language, and several others in english, but they would just not work because they used older versions of netbeans or libraries that my version doesn't have, this is very well explained and actually works, i was just looking how to do the loop, but im very gratefully, i even founded how to stop it , i have no more ways to thank you, but wherever you are thanks, thanks, thanks. I had like three days into this problem
Your thanks is more than enough! Very happy to hear this tutorial helped.
now this is epic
this really helped me a lot with my game in swing!
SUPERRRR!!!! GRACIASSSS POR COMPARTIR SUS CONOCIMIENTOS!!! SALUDOS DESDE PERÚ, ÉXITOS EN TODO!!
at least you respond to issues a few people get
Great video. Teaches me how to do things that my teacher has not taught me yet.
Thanks a lot for your kind words, they really make my day. I'm also glad my tutorials have allowed you to accelerate past your peers.
thank you so much for the vid. it is helping my comp sci group program a danny devito magic eight ball. you are a real one. hugs and kisses, your biggest fans
Thanks a lot; used part of your code to loop sound for an alarm system.
Great, now ive got music in my app. Thanks!
Cool Video but how can I stop music without a JOptionPane???
This was a great tutorial. Thank you.
Thanks so much! Amazing tutorial!
Thank you for the video. LEGEND!
Can you somehow make a seperate method to stop playing music?
How would that look like if I give a button a click sound. So it plays a 1sec Long Sound if u click it.
This video covers how to play a sound when a button is clicked: ruclips.net/video/OX3CFHLV9ws/видео.html
Incredible, on a side note, how would we go about getting those WAVs from a resource folder we've added to the our project's build path?
Is there a way to replace the message window with a JFrame and JButtons?
I tried to create a method that would stop the audio clip, but not working. I just changed the clip.start() to clip.stop() on the other method. Am I missing something? Thanks for the video by the way :)
Same problem. I have main class and then class from where I calling playSound method via object, but then I need to stop that track, so I can play another one, without two of them being overlapped.
I get this error when u run it the first time "non-static variable this cannot be referenced from a static context"
Sorry for the slow reply. This is simple to fix. If you are still need help, I'll be happy to look at your code.
Fantastic video very clean code and easy to follow thanks!
IS IT IMPORTANT TO CLOSE IT WHEN YOU DONE? (like in C)
Is there a way to also lower or inscrease the volume?
Max you 're amazingg
Hey mate, could you describe how can we get the same stream from an URL?
I am trying the same steps with "URL music = new URL(String link), but i get UnsupportedFile exception?
Nice voice over pete reference.
Is there a way to have a different class that can be called that stops the music instead of JOptionPane?
i didn't use a different class but i used Thread.sleep(amount) if that helps you?
error: Can't find file :((
Well, this might be a little late for you, but for anyone else who is also curious: I used the absolute path of my file (in my case, I put "G:/steve/eclipse-workspace/Simple Music Player/src/musicPlayer/PogoSticks.wav" which is the exact pathway of my audio file for the filePath. I'm sure you can use your class path to find it, but I'm too lazy to figure that out, and using the absolute path is a simple copy and paste from my file explorer address bar.
@@thearcticwinter5703 thank you so much ❤️
The audio doesn't kill himself after I press the ok button and clip.stop() just doesn't work, like it isn't wrote. Any ideas?
Is it possible to store/save musics in a list or a table?
Thanks it works ! =) Can we set the volume easily with this or not ?
It works! Thanks a lot! very helpful!!
it works perfectly, thanks man
You are my hero
Hey, great video!
Is there anyway to continue to play audio without input? I have a game i'm making at it would be great if I could have some background music to the game, but I don't wish to have any inputs after the clip.start() method. Is this possible?
Thanks,
Shadow (my other alias)
Greetings, I recently uploaded a tutorial on how to make a music player which may be of use to you: ruclips.net/video/OX3CFHLV9ws/видео.html As for what you want to do, I would look at this stack overflow post: stackoverflow.com/questions/34887816/how-do-i-implement-background-music-into-my-java-program
@@maxodidily Jeez! That was quick and helpful! Thanks so much! For that alone I am going to sub!
I actually figured it out, I was playing the sound inside the game method instead of the JFrame method, this meant that it only played once and all the other code ran!
Thanks a lot and glad you figured it out!
High five bru! More videos! God bless.
how would I change volume?
Does the Thread kill itself if the mainprogramm doesnt stop?
Is there any way of interrupting the daemon without having to show a JOptionPane?
Try these lines:
clip.open(ais);
clip.loop(-1);
Thread.sleep(amountToSleep);
I'm using BlueJ 5.0.0 because of school and my problme is that if I export it as a .jar it doesn't find the file anymore. I have it in the project folder and inside BlueJ it runs perfectly fine but after the export it doesn't recognize the file in the .jar archive and searches inside the folder of the .jar
How to fix inputs(3:12)? If i right click it shows cut, copy, paste option.
Are you using NetBeans?
Sir now i have downloaded.
Sir please tell me how can i stop the music without showing the dialog that says press ok to stop the music?
This video covers how to do just that: ruclips.net/video/OX3CFHLV9ws/видео.html
Que buen tutorial, muchas gracias.
Muchas gracias!
How would you mute instead of pause?
is there a way to do it without an option pane?
this method only works on windows as far as i know, do you know another way to playback audio files on linux?
HI! thanks for your tutoriel
Hey how can you fastforward and rewind with this?
A tricker topic but can be something I cover in a future tutorial after I recover from my illness.
I use jgrasp for my java coding because I run on a mac. Can anybody tell me what main methods, JavaFX Apps, applets, or midilets I may be missing?
For the meme and the working code, I have bestowed upon you:
A like and a sub.
How to play more than one after each other
Thanks mate
Hi able to help me with my javafx project? im adding music/audio but keep getting error
I don't think javax works anymore.
Thank u so much!!! You really helped me
Hello, I am working in Eclipse IDE, and it does not work for me very well,
Console:
"javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at Java.settings.playMusic(settings.java:58)
at Java.music.main(music.java:11)"
What's your code?
i dont know but getClip is not working for me
program code and music file please
Hi! I am doing a web Application with Login,Register and want to enable the client to play selected Song from the UI like a online Music app. How can i do it by getting the song from database and send it through a servlet to html page to play it with all play,pause,stop,next and so on.Please help me with this. Suggest any way to attain this
Very helpful, thanks
I'm glad it helped!
It's awesome but why it cannot run the long songs or music ? Thanks
Can you describe your issue in more detail please?
@@maxodidily when I use a bigger sound of duration of 3:30 minutes for example like( Linkin-park In the End.wav) this method does not work ...it works with 0:40 to 1:00 mins duration musics only 😃😃
@@pavlg3944 I'm sorry you are having this issue, could you send your code via hastebin please?
Why have i to use JOpionPane to play the music?
You don't, I just needed something to stop the application from closing.
New Sub Ty bro you're the best , May 2020 :3
smooth af.
Hey Max ty for your work. I have a problem. This code works fine, it plays the music however when i export this codes as a runnable jar file, it doesnt play the music. Please help. FIXED it.
AudioInputStream inputStream = AudioSystem.getAudioInputStream(getClass().getResource("sound.wav"));
Hello. I am using this method of playing music for my school project and I ran into a problem. It works fine when I run it from netbeans, but when I go to the dist folder of the project and run it from there the audio doesn't play. Is there any way to fix this? I already did so much on this project and I don't wat to throw it all away... :(
Where are your sound files located relative to your project's jar file?
@@maxodidily The jar file is in the dist folder and the sound files are in the project folder if this is what you ment.
Thank you so much!!!
Hey I hope you are still active,
Im doing the steps exactly and its saying file not found instead of playing
for me for some reason I had to move the audio file directly into the project folder, anytime i had it in another folder inside of it (such as a res folder and then audio and then the audio file, all inside the project folder) it couldn't find it.
@@varchive8136 you could just do res/filename if its in a res folder
@@jobliar937 I did that and it didn't work, what I think I ended up doing is making res a *package* in the src folder... but I'm not sure as I don't work on that project anymore.
@@varchive8136 If you really want I can send you a link to an entirely different way of running sound files that uses libraries
ruclips.net/video/HRaJXVuZjRM/видео.html
HI Max do you know any possible way i can get it to play at the same time the game is playing ( im trying to add the music as background music for a game )
same here..but idk any way
You can just repeat the code again (with different names for your AudioInputStream, clip, file object and filepath). Since this method creates separate threads for each sound, you can play multiple sounds at the same time.
Max O'Didily cheers figured it out today !
I'm glad you figured it out, what did you do?
Thank you😭
How do i play two sound files?
javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at Player.PlayMusic(Player.java:18)
at LoopMusic.main(LoopMusic.java:8)
what am i doing wrong?
im pretty new but im guessing u using a unsupported audio format ....try make ur file in 16bit Stereo Wav
thanx man ..
*still doesnt work please help. Program runs smoothly but no audio*
try {
File musicPath = new File("C:\\Users\\MyPC\\Desktop\\gaming music\\Kingdom Hearts");
if(musicPath.exists()) {
AudioInputStream audioInput = AudioSystem.getAudioInputStream(musicPath);
Clip clip = AudioSystem.getClip();
clip.open(audioInput);
clip.start();
JOptionPane.showMessageDialog(null,"press ok to stop playing");
}else {
System.out.println("Cant find file");
}
}catch(Exception e) {
}
}
use extension too. if your audio file name is Kingdom hearts then change it ti kingdomhearts.wav(wav format only)
this wont play 24bit wav files. Just 8 bit ant 16 bit
Mine doesn't play any audio but runs fine.
May I see your code?
@@maxodidily Here's a screenshot of code and file location: prntscr.com/nvlvup
@@maxodidily Update: Just nothing in general plays now, but it runs: prntscr.com/nvmldt
thank you so much
I did everything that you did but I'm getting the error expected at
musicObject.playMusic(filepath);
please help ;-;
May I see your code please?
@@maxodidily It's okay, I managed to make it work. Thanks for the helpful video, managed to pass my finals because of this.
File couldn't be found, I did the same thing as you
I followed his tutorial and worked for my audio file. I think the problem has to do with your path.
Have you double checked the path, and made sure you're using double // instead of single / within the path. You must do double due to the slash acting as a meta character, doing double slash cancels the first slash's meta character meaning.
where can i get this audio file
I can't remember where I obtained this file, I would suggest looking at compilation videos of Fiverr gigs Pete has done.
Warning, this didn't work for me if I was using a .mp4 file. I had to use audacity and change it to a .wav
i did everything you did, but it doesn't play
May I see your code?
11/10
Thank youu
For some reason, I can't find the music path. I've tried renaming the wav files, I've double checked the file location etc. It's just not working for me :(
I have made a similar video with javafx.. the code is on github, maybe it can help you :)
do this for c#
Then theres me who cant even make a scratch game without some errors
A master has encountered many errors.
Can you make a github link for this?
if i could upvote twice i would. same with subbing
Not working.
gonna need more info if u wanted help from someone
there is a god
Ok sure, Here is my credit/ Debit card info. But first. You will need to complete a human verification survey.
Thx ;)
I don't know if this thread is still active, but I am using the same method as shown in the video, using a .wav file as well but still getting the following error.
javax.sound.sampled.UnsupportedAudioFileException: File of unsupported format
at java.desktop/javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1066)
If someone could offer solutions to fix this, that would be greatly appreciated.
What's the name of the audio file you are trying to play?
BGM.wav
@@vilfredogiordano4544 wav isnt just wav u probably forcably changed it to wave but it still stores other data
lmfao that took me off guard.
Ah voice over pete