Java audio 🔊

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

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

  • @BroCodez
    @BroCodez  4 года назад +85

    import java.io.File;
    import java.io.IOException;
    import java.util.Scanner;
    import javax.sound.sampled.*;
    public class Main {
    public static void main(String[] args) throws UnsupportedAudioFileException, IOException, LineUnavailableException{

    Scanner scanner = new Scanner(System.in);

    File file = new File("Level_Up.wav");
    AudioInputStream audioStream = AudioSystem.getAudioInputStream(file);
    Clip clip = AudioSystem.getClip();
    clip.open(audioStream);

    String response = "";

    while(!response.equals("Q")) {
    System.out.println("P = play, S = Stop, R = Reset, Q = Quit");
    System.out.print("Enter your choice: ");

    response = scanner.next();
    response = response.toUpperCase();

    switch(response) {
    case ("P"): clip.start();
    break;
    case ("S"): clip.stop();
    break;
    case ("R"): clip.setMicrosecondPosition(0);
    break;
    case ("Q"): clip.close();
    break;
    default: System.out.println("Not a valid response");
    }
    }
    System.out.println("Byeeee!");
    }
    }

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

      When you realize you didn't have to watch the whole video. Still helpful tho. THXXXXXXXXX

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

      Bro how are you so smart? How do you know these things? Just how???

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

      @@Code_Machine read the documentation? or watch youtube tutorial if u are lazy like me 🤣

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

      i had a question an i instanciate an object based on another objects properties such as
      if I had created hair and a getter
      Color color = hair.getColor();

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

      @@cayman_islands Your file must be .wav extension not mp3.

  • @kirankarki4217
    @kirankarki4217 2 года назад +57

    This is the first time I have ever played audio through programming. The joy it gave me was very immense. Thank you very much for putting out this tutorial.

  • @jonathanmantello3974
    @jonathanmantello3974 3 года назад +11

    Thank you for the simple and clear explanation! I really want to go deeper into this topic, and have done a bit of research on Java's Line Mixer and stuff. I am a musician and audio engineer and I want to start building some music creation tools, or mp3 player type projects for my portfolio.

  • @nikybomber496
    @nikybomber496 4 года назад +23

    you are the only one who made me understand how to put music in java

  • @wh1t3st4r5
    @wh1t3st4r5 3 года назад +56

    Quick tip for y'all. Java can't handle common high quality files (48000.0 Hz, 32 bit, etc), if you encounter something like this: line with format PCM_FLOAT 48000.0 Hz, 32 bit, stereo, 8 bytes/frame not supported. You'll have to downgrade your file, using the web converter and tuning it to ~20000 does the trick ;D

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

      Thx buddy

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

      I made it 16000 Hz 8 bits and it still won’t work

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

      @@Noman23444 odd, We will have to read some docs to know what after the limitations exactly then

    • @raffaelefogu4553
      @raffaelefogu4553 9 месяцев назад +2

      Even 2 years later, I love you

    • @4r1k87
      @4r1k87 9 месяцев назад +2

      Ok thanks bro

  • @janil9357
    @janil9357 4 года назад +6

    great video, appreciate the hard works you've done

  • @DrinkableWater
    @DrinkableWater 4 года назад +46

    Maby I use this for adding jumpscares to programms that have nothing to do with horror... XD

    • @BroCodez
      @BroCodez  4 года назад +7

      YES! That would be great 😂

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

      @@BroCodez could not get audio input stream from input file , that what exception says

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

      @@BroCodez in my intellij its not working

  • @oguzhantopaloglu9442
    @oguzhantopaloglu9442 4 года назад +8

    NO WAY THIS WAS EXACTLY WHAT I NEEDED OMG YOU ARE THE BEST

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

      I think you might have given me the idea to create this video lol

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

      @@BroCodez 😂

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

      @@BroCodez you are the best bro! Could you maybe make a video on CardLayout class too?

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

    Wao man, it's what i was looking for ... 3 months ago, I don't know why I didn't find this amazing video

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

    Thxxxxxxxxx mannnnnn you're the best
    Love from India 🇮🇳🇮🇳🇮🇳🇮🇳🇮🇳

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

    when watching your vids I constantly see ads from the university I tried to get into but failed... Bro it's kinda depressing but won't stop me from learning from your supersupersuper tutorials ;DDD

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

      You can get into programming without a programming degree! Just keep your effort as close to 100% as possible and never worry that you might fail, because it's toxic to worry about things that are out of your control. What you have is each day! Lose yourself to the music

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

      @@Simis999 thank you :) Finally, I did it. I'm on my dream course of study but you're absolutely right - it's all about hard work and doing your best every single day

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

    THAN YOU VERY MUCH BRO!!!!!! I CAN NOW MAKE MY CODE PLAY THE BIRD

  • @meedhanshgupta
    @meedhanshgupta Год назад

    Bro, you are the best...
    Thanks for making such free videos

  • @dianamilenaarchilacordoba4632
    @dianamilenaarchilacordoba4632 3 месяца назад

    great video!!! I'm infinitely grateful for your dedication and big heart to share this knowledge with the world. Thank you soo much

  • @hasibulshaikh9396
    @hasibulshaikh9396 Год назад

    Thanks for helping Dude. You have explained it so nicely

  • @xevonate
    @xevonate 4 месяца назад

    Thank you bro, with 3:25 you saved me

  • @arjundas7078
    @arjundas7078 4 месяца назад

    No words can express this feeling.

  • @oguzhantopaloglu9442
    @oguzhantopaloglu9442 4 года назад +23

    Bro I have some questions:
    1- How can we play a sound that will automaticly loop when it ends until we stop it with clip.stop() ?
    2- How can we set the volume of the sound?
    3- Can we set different volumes to different sounds while playing all of them at once? For example we have some sound effects and music in some app and we want to play music at %70 volume and sound effects at %50 volume. (We want to play them at the same time, the music will be playing in the background and when we play the sound effect, the music will not stop)

    • @BroCodez
      @BroCodez  4 года назад +34

      1. clip.loop(clip.LOOP_CONTINUOUSLY);
      2 + 3. You can set the individual sound levels for each clip using this set of code. I would recommend placing this code in a method to call when you need it:
      FloatControl volume = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
      // set the percent (between 0.0 and 1.0)
      double percent = 0.5;
      float dB = (float) (Math.log(percent) / Math.log(10.0) * 20.0);
      volume.setValue(dB);

    • @oguzhantopaloglu9442
      @oguzhantopaloglu9442 4 года назад +8

      @@BroCodez thank you so much bro you are the best!

  • @AbduqodirOdiljonov-o4x
    @AbduqodirOdiljonov-o4x 7 месяцев назад

    Thanks Bro, after watching your videos i get more motivation to code

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

    Best coding channel!

  • @hamzaak1781
    @hamzaak1781 Год назад

    I just subscribed after the second video from If statement to Audio. Itbreallybmakes sense when they say, if you really know the base of Java, everything else will follow easily.
    Thanks a lot.i am going for SDET,so Java is the main programming language as the backbone... thanks

  • @Motivational_speaker_saikat
    @Motivational_speaker_saikat 5 месяцев назад

    I love this channel. I am very much thankful to bro code for coding content. I teach everyday new new thing.
    Thank you
    Bro code❤❤❤

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

    dude thats the coolest tutorial so far, well im new to your channel so

  • @furiouspuzza7080
    @furiouspuzza7080 4 года назад +4

    Best of all... no ads 😎🎧

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

      Oh, there's ads lol
      I try not to place too many in because I know it gets annoying

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

      @Bro Code no I mean no ads when you listen to music in eclipse 🤣

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

      @Bro Code can you teach us how to make a playlist? 😂

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

    Thank you for such a wonderful video.

  • @federicomaccioni3446
    @federicomaccioni3446 7 месяцев назад

    Yu're a f*cking g, i've been searching for this for like two years

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

    Never heard of that. Good to know. Thanks

  • @theuns-robertpretorius8331
    @theuns-robertpretorius8331 2 года назад

    every video this guy makes deserves a like

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

    helped me out alot man thanks

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

      thanks for watching epic

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

    really thankful for this tutorial

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

    You are the best bro!❤

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

    I like your Taste in music

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

      i know right some interesting music

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

      It's a real banger

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

      @@BroCodez where do I find it tho

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

    Finally I can listen to epic sax guy in my java application.

  • @danny.3036
    @danny.3036 3 года назад

    Thanks, Bro! ☕ You're awesome!

  • @Marius-re3xj
    @Marius-re3xj 3 года назад +2

    Great video! What keyboard are you using? It sounds satisfying :)

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

    good video to practice multiple concept, Ty

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

    Thank you so much! I'm also using Eclipse so some other videos on this topic that I watched didn't help.

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

    the filepath is not found.... can't understand i put the correct String..

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

    Ah, thank you! after series of check on stack overflow. finally worked! voila

  • @ThomasRobertsOffficial
    @ThomasRobertsOffficial 8 месяцев назад

    I think the simplest way to suspend your program is just "while(boolean var) { }" or in this case "while(clip.isRunning()) { }". Both of these will trap the thread and allow music to continue playing until the expression resolves to false.

  • @Overseer190
    @Overseer190 2 года назад +2

    Exception in thread "main" javax.sound.sampled.LineUnavailableException: line with format PCM_SIGNED 32000.0 Hz, 24 bit, mono, 3 bytes/frame, *little-endian not supported*
    What does this mean?

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

      Exception in thread "main" java.io.FileNotFoundException: nocturnal.wav (The system cannot find the file specified)

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

    Thanks Bro for keeping it simple 😊

  • @Blackilykat
    @Blackilykat 3 года назад +8

    I actually preferred using if statements for this one because i used p for both play and pause. i also made a fast forward and a rewind system :)
    File file = new File("src/sound.wav");
    AudioInputStream audioStream = AudioSystem.getAudioInputStream(file);
    Clip clip = AudioSystem.getClip();
    clip.open(audioStream);
    System.out.println("P = play/pause, R = reset, RR = rewind 5s, FF = fast forward 5s, Q = quit");
    String response = scanner.next();
    response = response.toLowerCase();
    boolean isPlaying = false;
    while(!response.equals("q")) {
    System.out.println("P = play/pause, R = reset, RR = rewind 5s, FF = fast forward 5s, Q = quit");
    if(response.equals("p")) {
    if(!isPlaying) {
    clip.start();
    isPlaying = true;
    } else {
    clip.stop();
    isPlaying = false;
    }
    } else if (response.equals("r")) {
    clip.setMicrosecondPosition(0);
    } else if (response.equals("rr")) {
    clip.setMicrosecondPosition(clip.getMicrosecondPosition() - 5000000);
    // NOTE: 1000000 is 1 second
    } else if (response.equals("ff")) {
    clip.setMicrosecondPosition(clip.getMicrosecondPosition() + 5000000);
    } else {
    System.out.println("invalid input");
    }
    response = scanner.next().toLowerCase();
    }

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

    Thank You very much Sirrrrr😍

  • @noah77
    @noah77 4 года назад +4

    Hi Bro! I am back, hope you remember me :D just a little tip here, when you teach us Java in Eclipse, press ctrl + m. It will make it full screen and we would be able to see the code properly. I would certainly like it :D

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

      ha! of course I remember you Moonlight!
      Sweet, didn't know about that crtl + m trick! I'll have to try it

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

      @@BroCodez awesome! :D

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

      or don't use eclipse and use intelliJ instead

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

    I have a exception please help me:
    Exception in thread "main" javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file
    at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1189)
    at playmusic.Main.main(Main.java:15)
    C:\Users\ferid\AppData\Local\NetBeans\Cache\8.2\executor-snippets
    un.xml:53: Java returned: 1
    BUILD FAILED (total time: 1 second)

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

    😍😍😍😍😍

  • @abdelrahman702
    @abdelrahman702 8 месяцев назад

    Thank you from Egypt

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

    Glad I'm subbed

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

    huge thank you!

  • @Hexieee
    @Hexieee Год назад

    Is there a way to put this on my UI? and btw you always do a good job. Good thing I listened to my friends because they introduced me to your channel

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

    piece of cake broh!

  • @MK-zf6or
    @MK-zf6or 3 года назад

    bro is the best bro from all my bros and I haven't even met bro

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

    thank you very good vid!

  • @eugenezuev7349
    @eugenezuev7349 8 месяцев назад

    gorgeous!

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

    How would you make a program that plays a variable pitch tone? I want to change the sound's pitch based on the distance between two points on the screen

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

    Hey, thanks for the great tutorial! I have a question, after calling the sound method I created, my program stops executing the following proceeding lines. For example, I want to play a sound when I run my start game function, then have the game logic still be executed, but where ever I call the sound, my game just comes to a complete halt, how do I fix this?

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

    Great! Thank you Bro!

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

    Hi, i'm a beginner with java and I followed your snake game tutorial as well, and wanted to add a sound effect when an apple is eaten. For some reason I can't get it to work. Here is my code:
    public void playSound() throws LineUnavailableException, IOException, UnsupportedAudioFileException {

    File file = new File("appleeaten.wav");
    AudioInputStream audioStream = AudioSystem.getAudioInputStream(file);
    Clip clip = AudioSystem.getClip();
    clip.open(audioStream);
    clip.start();

    }

    public void checkApple() {

    if((x[0] == appleX) && (y[0]) == appleY) {
    bodyParts++;
    applesEaten++;
    try {
    playSound();
    } catch (LineUnavailableException | IOException | UnsupportedAudioFileException e) {
    e.printStackTrace();
    }
    newApple();
    }

    }
    there are no errors in the code but no sound actually plays when I test it. Do you know what I did wrong?

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

    great vid bro

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

      thank you as always Dbest

  • @mariapriya5311
    @mariapriya5311 Год назад

    Thanks a lot Sirr!!!

  • @tasneemayham974
    @tasneemayham974 Год назад +1

    Hey, bro!!
    I've got a question:
    I've been trying to make a JButton that when clicked will produce an audio. I created 3 classes. Within the LaunchAudio Class, in the ActionPerformed method, I created an instance of the class audio where wrote the audio code. I tried to make the button static and make a while loop that says e.getSource() == button, clip.start(); and a sysout. But it doesn't work. There are no errors in the code. But there is no sound when I click the button. the print lines are there tho. May you help me out with this?
    And thx for all your videos BRO!!!! Real HERO!

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

    Thank​ bro for video

  • @曾毓哲-b1t
    @曾毓哲-b1t Год назад

    Thank you very much

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

    A new way to rick roll people!!!

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

    Ya man you are so smart bro

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

    Thank you so much sir.

  • @oskaroskarowski1472
    @oskaroskarowski1472 Год назад +1

    anybody got: "The package javax.sound.sampled is not accessible" ??

  • @rudypeter9720
    @rudypeter9720 10 месяцев назад

    Thank you so much

  • @johnbeal6592
    @johnbeal6592 Год назад

    How can i add on this and make it open multiple files and be able to play lot more songs and some other features too

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

    thank you bro!

  • @superTramp572
    @superTramp572 Год назад

    I stucked pretty fast at the first import, netbeans says "import javax.sound.sampled.*; not visible. Package javax.sound.sampled is declared in module java.desktop, but module "my main class" does not read it.
    Has anyone got similar error?

  • @leonferreira4287
    @leonferreira4287 Год назад

    Thank you for share your knowledge!!!!!! Leon T S Ferreira

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

    Amazing, thanks

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

    Am very glad. i was finally able to complet it after my second attempt. thank you very much.
    but i want to ask, is it possible to add more sounds and to delete sound aswell?

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

    super awesome

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

    But i think this time round am going to be expert in programming with code bro

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

    Great

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

    Great video Broo THANKS🤘🏿....Are you still going to do more videos on audio? How do eye have more than one song? and how to create a desktop of my mp3 player?

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

      maybe, it depends on the demand

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

    Excellent! But how about mp3 ?

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

    can i automtically start the sound when i run the class? without having any input?

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

    brooooooooo 😳😳

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

    Useful. But most of the video is about generic command line processing not so much about audio.

  • @pa-305
    @pa-305 3 года назад

    best broooo

  • @stay-felany3267
    @stay-felany3267 3 года назад

    ah this helped thanks

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

    I didn't understand the first part of the code:
    File file = new File("Level_Up.wav");
    AudioInputStream audioStream = AudioSystem.getAudioInputStream(file);
    Clip clip = AudioSystem.getClip();
    clip.open(audioStream);

  • @augischadiegils.5109
    @augischadiegils.5109 4 года назад

    Very nice

  • @audiomac
    @audiomac 10 месяцев назад

    I am confused. How do I install the package?! I can't find any instructions on how to install it!

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

    I got Exception in thread "main" javax.sound.sampled.LineUnavailableException: line with format PCM_SIGNED 48000.0 Hz, 24 bit, mono, 3 bytes/frame, little-endian not supported. how to fix?

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

    Nice video, But why the File work only with wav Script?
    What about the others?

  • @speklex779
    @speklex779 Год назад

    dope video

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

    nice tuts

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

    Friendly reminder to everyone: The file shouldn't contain spaces or it won't work

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

    Can you tell how to increase and decrease the volume?

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

    Hi Bro! Like im making a game and want to have a background music, that once the music stops, the next music in an array starts. But like you said bout the gui or Scanner bit, so when youre Playing a game you'd have to pause the game, click ok or scanner to continue, is there any other way.

  • @shibanned
    @shibanned 8 месяцев назад

    why do i have an error on javax.sound.sample.*; it says the package is not accessible

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

    in my eclipse dont find the file whyy ??
    i had followed all the passage

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

    Hello, Bro! I can already code in Javascript an I am currently learning Java. One thing that is still confusing me is this:
    AudioInputStream audioStream = AudioSystem.getInputStream(file);
    why do I have to write AudioInputStream before the name of the object/variable I am creating? what is stored in the audioStream variable is the returned value from the AudioSystem static method "getInputStream" right?
    in javascript the syntax would look something like this
    const audioStream = audioSystem.getInputStream() (if the method audioSystem.getInputStream actually existed in javascript)

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

      did you figure this out?? I am having the same doubt.

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

      @@it04ameekanazreen59 hello and yes haah

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

      @@it04ameekanazreen59 in Java, its mandatory to define the tipe of the variable. boolean, int. String. But not only that, every instanxe of a class, be it custom or predefined, should also have its type stated before the name of the variable

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

      So if I created a class called GameClass
      Class GameClass{
      public string gameString(){
      return "youre playing a game",}
      public AnotherRandomClass createAnInstanceOfThatRandomClass (){
      return new AnotgerRandomClass();
      }
      }

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

      I would have to do the following
      GameClass gameClassObj = new GameClass()

  • @GOODBOY-vt1cf
    @GOODBOY-vt1cf 3 года назад +1

    8:03

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

    This is the music tape from " The Forest", am I right? Sure this sounds familiar 🙄🙄🙄