How to Play, Loop, Pause and Stop Music in Java

Поделиться
HTML-код
  • Опубликовано: 19 янв 2025

Комментарии • 155

  • @ngahacong8101
    @ngahacong8101 2 года назад +1

    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

    • @maxodidily
      @maxodidily  2 года назад +1

      Really happy to hear this tutorial helped! Wishing you all the best in getting your degree.

  • @S0L4RE
    @S0L4RE 5 лет назад +18

    The combination of memes and programming is absolute genius.

  • @dariusjohnson8849
    @dariusjohnson8849 6 лет назад +14

    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.

    • @maxodidily
      @maxodidily  6 лет назад +1

      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.

  • @maxodidily
    @maxodidily  6 лет назад +16

    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.

    • @theawsomeguy8814
      @theawsomeguy8814 6 лет назад

      what happened

    • @infinity6305
      @infinity6305 4 года назад

      Hi,
      I get an error at
      Clip clip = new AudioSystem.getClip();
      ------------------------------------
      Why?

  • @nevinndonwi3274
    @nevinndonwi3274 3 года назад +1

    This actually worked, it’s the first time I could get sound to work in a program. Thank you.

  • @tengubnahi5788
    @tengubnahi5788 2 года назад

    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

    • @maxodidily
      @maxodidily  2 года назад

      Your thanks is more than enough! Very happy to hear this tutorial helped.

  • @ukcommunistparty6045
    @ukcommunistparty6045 6 лет назад +37

    now this is epic

  • @bridiro
    @bridiro 2 года назад +1

    this really helped me a lot with my game in swing!

  • @isaacroman3040
    @isaacroman3040 5 лет назад +4

    SUPERRRR!!!! GRACIASSSS POR COMPARTIR SUS CONOCIMIENTOS!!! SALUDOS DESDE PERÚ, ÉXITOS EN TODO!!

  • @bitchlasagne9394
    @bitchlasagne9394 6 лет назад +3

    at least you respond to issues a few people get

  • @sohompal7357
    @sohompal7357 6 лет назад +2

    Great video. Teaches me how to do things that my teacher has not taught me yet.

    • @maxodidily
      @maxodidily  6 лет назад

      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.

  • @terracottapots6989
    @terracottapots6989 2 года назад

    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

  • @annandkoongie4513
    @annandkoongie4513 3 года назад

    Thanks a lot; used part of your code to loop sound for an alarm system.

  • @szejker5011
    @szejker5011 4 года назад

    Great, now ive got music in my app. Thanks!

  • @bouncebootleg8412
    @bouncebootleg8412 5 лет назад +3

    Cool Video but how can I stop music without a JOptionPane???

  • @panchal6360
    @panchal6360 2 года назад

    This was a great tutorial. Thank you.

  • @badger411
    @badger411 2 года назад

    Thanks so much! Amazing tutorial!

  • @zecashe9407
    @zecashe9407 3 года назад

    Thank you for the video. LEGEND!

  • @lukli425
    @lukli425 6 месяцев назад

    Can you somehow make a seperate method to stop playing music?

  • @_bt1831
    @_bt1831 4 года назад +1

    How would that look like if I give a button a click sound. So it plays a 1sec Long Sound if u click it.

    • @maxodidily
      @maxodidily  4 года назад +1

      This video covers how to play a sound when a button is clicked: ruclips.net/video/OX3CFHLV9ws/видео.html

  • @ethnicsovereignty2369
    @ethnicsovereignty2369 3 года назад +2

    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?

  • @Big_Funi_Official
    @Big_Funi_Official 5 лет назад +1

    Is there a way to replace the message window with a JFrame and JButtons?

  • @Alpha-ev4fj
    @Alpha-ev4fj 5 лет назад +2

    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 :)

    • @brunosemerek9880
      @brunosemerek9880 5 лет назад +2

      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.

  • @CarNage2x
    @CarNage2x 2 года назад

    I get this error when u run it the first time "non-static variable this cannot be referenced from a static context"

    • @maxodidily
      @maxodidily  2 года назад

      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.

  • @harry9743
    @harry9743 3 года назад

    Fantastic video very clean code and easy to follow thanks!

  • @mixtapemania6769
    @mixtapemania6769 5 лет назад +1

    IS IT IMPORTANT TO CLOSE IT WHEN YOU DONE? (like in C)

  • @major1352
    @major1352 4 года назад +1

    Is there a way to also lower or inscrease the volume?

  • @totoxsk3320
    @totoxsk3320 3 года назад

    Max you 're amazingg

  • @martinmiloshev241
    @martinmiloshev241 3 года назад +1

    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?

  • @spencernold7121
    @spencernold7121 3 года назад

    Nice voice over pete reference.

  • @forlornfox
    @forlornfox 4 года назад +1

    Is there a way to have a different class that can be called that stops the music instead of JOptionPane?

    • @claraheadstrong2143
      @claraheadstrong2143 4 года назад

      i didn't use a different class but i used Thread.sleep(amount) if that helps you?

  • @thaohanh2676
    @thaohanh2676 4 года назад +3

    error: Can't find file :((

    • @thearcticwinter5703
      @thearcticwinter5703 3 года назад +3

      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.

    • @thaohanh2676
      @thaohanh2676 3 года назад +2

      @@thearcticwinter5703 thank you so much ❤️

  • @acalexanderchannel9572
    @acalexanderchannel9572 3 года назад

    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?

  • @anti-depression7429
    @anti-depression7429 5 лет назад +1

    Is it possible to store/save musics in a list or a table?

  • @leobrunot8993
    @leobrunot8993 5 лет назад +1

    Thanks it works ! =) Can we set the volume easily with this or not ?

  • @lisaw3829
    @lisaw3829 5 лет назад

    It works! Thanks a lot! very helpful!!

  • @microman99
    @microman99 5 лет назад

    it works perfectly, thanks man

  • @jashgovender3683
    @jashgovender3683 3 года назад

    You are my hero

  • @minecraftmaniacawesome3857
    @minecraftmaniacawesome3857 4 года назад +1

    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)

    • @maxodidily
      @maxodidily  4 года назад +1

      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

    • @minecraftmaniacawesome3857
      @minecraftmaniacawesome3857 4 года назад

      @@maxodidily Jeez! That was quick and helpful! Thanks so much! For that alone I am going to sub!

    • @minecraftmaniacawesome3857
      @minecraftmaniacawesome3857 4 года назад

      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!

    • @maxodidily
      @maxodidily  4 года назад

      Thanks a lot and glad you figured it out!

  • @kriziacarissepangan1590
    @kriziacarissepangan1590 4 года назад

    High five bru! More videos! God bless.

  • @kyuxi
    @kyuxi 6 лет назад +1

    how would I change volume?

  • @prismarin8009
    @prismarin8009 4 года назад

    Does the Thread kill itself if the mainprogramm doesnt stop?

  • @herpderpitypurple73
    @herpderpitypurple73 6 лет назад +1

    Is there any way of interrupting the daemon without having to show a JOptionPane?

    • @maxodidily
      @maxodidily  5 лет назад

      Try these lines:
      clip.open(ais);
      clip.loop(-1);
      Thread.sleep(amountToSleep);

  • @ChaoticTheGamer
    @ChaoticTheGamer 4 года назад

    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

  • @rishabhpanwar7539
    @rishabhpanwar7539 4 года назад

    How to fix inputs(3:12)? If i right click it shows cut, copy, paste option.

    • @maxodidily
      @maxodidily  4 года назад +1

      Are you using NetBeans?

    • @rishabhpanwar7539
      @rishabhpanwar7539 4 года назад

      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?

    • @maxodidily
      @maxodidily  4 года назад

      This video covers how to do just that: ruclips.net/video/OX3CFHLV9ws/видео.html

  • @florgalindo7833
    @florgalindo7833 2 года назад

    Que buen tutorial, muchas gracias.

  • @callumhay928
    @callumhay928 4 года назад

    How would you mute instead of pause?

  • @reddistone
    @reddistone 4 года назад

    is there a way to do it without an option pane?

  • @skyler948
    @skyler948 3 года назад

    this method only works on windows as far as i know, do you know another way to playback audio files on linux?

  • @anti-depression7429
    @anti-depression7429 5 лет назад

    HI! thanks for your tutoriel

  • @sohompal7357
    @sohompal7357 6 лет назад +1

    Hey how can you fastforward and rewind with this?

    • @maxodidily
      @maxodidily  6 лет назад +1

      A tricker topic but can be something I cover in a future tutorial after I recover from my illness.

  • @austinfaber2247
    @austinfaber2247 4 года назад

    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?

  • @noicthebrave
    @noicthebrave 3 года назад

    For the meme and the working code, I have bestowed upon you:
    A like and a sub.

  • @mekhailmeiring8592
    @mekhailmeiring8592 2 года назад

    How to play more than one after each other

  • @HitTank47
    @HitTank47 3 года назад +1

    Thanks mate

  • @sweekeung
    @sweekeung 4 года назад

    Hi able to help me with my javafx project? im adding music/audio but keep getting error

  • @heisenberg2077
    @heisenberg2077 5 лет назад

    Thank u so much!!! You really helped me

  • @kraszkreszlist316
    @kraszkreszlist316 4 года назад

    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)"

  • @TT-gp2os
    @TT-gp2os 3 года назад

    i dont know but getClip is not working for me

  • @harinirepala
    @harinirepala 2 года назад

    program code and music file please

  • @Dineshkumar-jw4mu
    @Dineshkumar-jw4mu 5 лет назад

    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

  • @amirsyafiq5761
    @amirsyafiq5761 6 лет назад

    Very helpful, thanks

  • @pavlg3944
    @pavlg3944 6 лет назад

    It's awesome but why it cannot run the long songs or music ? Thanks

    • @maxodidily
      @maxodidily  6 лет назад +1

      Can you describe your issue in more detail please?

    • @pavlg3944
      @pavlg3944 6 лет назад

      @@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 😃😃

    • @maxodidily
      @maxodidily  6 лет назад

      @@pavlg3944 I'm sorry you are having this issue, could you send your code via hastebin please?

  • @lucasflorentino5893
    @lucasflorentino5893 4 года назад

    Why have i to use JOpionPane to play the music?

    • @maxodidily
      @maxodidily  4 года назад +1

      You don't, I just needed something to stop the application from closing.

  • @nelsonortega9340
    @nelsonortega9340 4 года назад +1

    New Sub Ty bro you're the best , May 2020 :3

  • @wholeegg
    @wholeegg 4 года назад

    smooth af.

  • @BoBo-bq2os
    @BoBo-bq2os 4 года назад

    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.

    • @BoBo-bq2os
      @BoBo-bq2os 4 года назад

      AudioInputStream inputStream = AudioSystem.getAudioInputStream(getClass().getResource("sound.wav"));

  • @mittiyas242
    @mittiyas242 4 года назад

    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... :(

    • @maxodidily
      @maxodidily  4 года назад

      Where are your sound files located relative to your project's jar file?

    • @mittiyas242
      @mittiyas242 4 года назад

      @@maxodidily The jar file is in the dist folder and the sound files are in the project folder if this is what you ment.

  • @fm07teaching
    @fm07teaching 5 лет назад

    Thank you so much!!!

  • @DiccHeadProductionz
    @DiccHeadProductionz 4 года назад +1

    Hey I hope you are still active,
    Im doing the steps exactly and its saying file not found instead of playing

    • @varchive8136
      @varchive8136 4 года назад +1

      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
      @jobliar937 4 года назад

      @@varchive8136 you could just do res/filename if its in a res folder

    • @varchive8136
      @varchive8136 4 года назад

      @@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.

    • @jobliar937
      @jobliar937 4 года назад

      @@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

  • @joeyladd8841
    @joeyladd8841 6 лет назад

    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 )

    • @NickTsougass
      @NickTsougass 6 лет назад

      same here..but idk any way

    • @maxodidily
      @maxodidily  6 лет назад

      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.

    • @joeyladd8841
      @joeyladd8841 6 лет назад

      Max O'Didily cheers figured it out today !

    • @maxodidily
      @maxodidily  6 лет назад

      I'm glad you figured it out, what did you do?

  • @Victor-xt6ll
    @Victor-xt6ll 4 года назад +1

    Thank you😭

  • @cjgs1000
    @cjgs1000 5 лет назад

    How do i play two sound files?

  • @IkeVoodoo
    @IkeVoodoo 5 лет назад +1

    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?

    • @eumieumi8766
      @eumieumi8766 5 лет назад

      im pretty new but im guessing u using a unsupported audio format ....try make ur file in 16bit Stereo Wav

  • @sidharthmishra4158
    @sidharthmishra4158 2 года назад

    thanx man ..

  • @fightinggold7674
    @fightinggold7674 5 лет назад

    *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) {

    }




    }

    • @wholeegg
      @wholeegg 4 года назад +2

      use extension too. if your audio file name is Kingdom hearts then change it ti kingdomhearts.wav(wav format only)

  • @cjgs1000
    @cjgs1000 5 лет назад

    this wont play 24bit wav files. Just 8 bit ant 16 bit

  • @blufilmsesa
    @blufilmsesa 5 лет назад

    Mine doesn't play any audio but runs fine.

    • @maxodidily
      @maxodidily  5 лет назад

      May I see your code?

    • @blufilmsesa
      @blufilmsesa 5 лет назад

      @@maxodidily Here's a screenshot of code and file location: prntscr.com/nvlvup

    • @blufilmsesa
      @blufilmsesa 5 лет назад

      @@maxodidily Update: Just nothing in general plays now, but it runs: prntscr.com/nvmldt

  • @burakyol3686
    @burakyol3686 4 года назад

    thank you so much

  • @gabytesrido3962
    @gabytesrido3962 5 лет назад

    I did everything that you did but I'm getting the error expected at
    musicObject.playMusic(filepath);
    please help ;-;

    • @maxodidily
      @maxodidily  5 лет назад +1

      May I see your code please?

    • @gabytesrido3962
      @gabytesrido3962 5 лет назад +1

      @@maxodidily It's okay, I managed to make it work. Thanks for the helpful video, managed to pass my finals because of this.

  • @ludzo2347
    @ludzo2347 5 лет назад

    File couldn't be found, I did the same thing as you

    • @crazyconan28
      @crazyconan28 5 лет назад

      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.

  • @yakult_is_a_cult
    @yakult_is_a_cult 4 года назад

    where can i get this audio file

    • @maxodidily
      @maxodidily  4 года назад

      I can't remember where I obtained this file, I would suggest looking at compilation videos of Fiverr gigs Pete has done.

  • @malachisanderson1284
    @malachisanderson1284 4 года назад

    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

  • @ailtondevesse4738
    @ailtondevesse4738 5 лет назад

    i did everything you did, but it doesn't play

  • @DrewnaAdamek
    @DrewnaAdamek 5 лет назад +1

    11/10

  • @Sara-dz3hc
    @Sara-dz3hc 8 месяцев назад

    Thank youu

  • @Mitch_Yang
    @Mitch_Yang 5 лет назад

    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 :(

    • @gasparcoding
      @gasparcoding 5 лет назад

      I have made a similar video with javafx.. the code is on github, maybe it can help you :)

  • @theawsomeguy8814
    @theawsomeguy8814 5 лет назад

    do this for c#

  • @TheNoobGuyGamingAndMore
    @TheNoobGuyGamingAndMore 6 лет назад +1

    Then theres me who cant even make a scratch game without some errors

    • @maxodidily
      @maxodidily  6 лет назад +4

      A master has encountered many errors.

  • @mrED123
    @mrED123 5 лет назад

    Can you make a github link for this?

  • @discordgoat4228
    @discordgoat4228 3 года назад

    if i could upvote twice i would. same with subbing

  • @chrishuppe2734
    @chrishuppe2734 4 года назад

    Not working.

  • @philipino99
    @philipino99 6 лет назад +1

    there is a god

  • @paulkwan4296
    @paulkwan4296 4 года назад +1

    Ok sure, Here is my credit/ Debit card info. But first. You will need to complete a human verification survey.

  • @mrED123
    @mrED123 5 лет назад

    Thx ;)

  • @vilfredogiordano4544
    @vilfredogiordano4544 4 года назад

    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.

    • @maxodidily
      @maxodidily  4 года назад

      What's the name of the audio file you are trying to play?

    • @vilfredogiordano4544
      @vilfredogiordano4544 4 года назад

      BGM.wav

    • @jakobrollwage7048
      @jakobrollwage7048 3 года назад

      @@vilfredogiordano4544 wav isnt just wav u probably forcably changed it to wave but it still stores other data

  • @sedeanimugamez5418
    @sedeanimugamez5418 5 лет назад

    lmfao that took me off guard.

  • @fuzedcable5612
    @fuzedcable5612 4 года назад

    Ah voice over pete