How to Play Audio Files (Sound Effect) - Java Extra 3

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

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

  • @lichenlink465
    @lichenlink465 4 года назад +16

    For anyone who cannot get the sound effect to play, I had the same problem and it's because only certain sound files will work. The file MUST be 16bit to work! Try converting your sound file to the correct bit rate and see if it works then (I used audacity).
    Sound files that I've found work:
    -> 16 bit .aiff
    -> 16 bit .wav
    Files types which WILL NOT WORK (causes an error message in the console window):
    Mp2
    Mp3
    Ogg
    Flac
    Wma
    A 32-bit .wav file will not give an error message in the console window, but it will not play regardless. I have no idea why...

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

      Yes, I also confirmed that 32bit wav files don't work. I'm sorry that I failed to notice/mention that when I made the video and thank you for providing these additional info!

    • @j.c8183
      @j.c8183 4 года назад +3

      Hello everyone, if you want to Convert from 32 bit to 16bit :
      - Download Audacity,
      - Import your soundtrack,
      - Go Tracks > mix > mix stereo down to mono
      - Click on the track title and select Set Sample Format -> 16-bit
      - Convert to 22KHz > At the bottom of the window there is a little button named Project Rate (Hz) - Make sure this is 22 kHz or less.
      -Export your soundtrack > Select File -> Export -> Export as WAV
      I hope it will work for you :)

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

      @@j.c8183 thanks a lot bro

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

      @@RyiSnow hi, i did all the conversions on followed the steps but i get these errors:
      Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
      at Game$SoundEffect.play(Game.java:244)
      at Game$TitleScreenHandler.actionPerformed(Game.java:251)
      at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
      at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
      at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
      at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
      at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
      at java.awt.Component.processMouseEvent(Unknown Source)
      at javax.swing.JComponent.processMouseEvent(Unknown Source)
      at java.awt.Component.processEvent(Unknown Source)
      at java.awt.Container.processEvent(Unknown Source)
      at java.awt.Component.dispatchEventImpl(Unknown Source)
      at java.awt.Container.dispatchEventImpl(Unknown Source)
      at java.awt.Component.dispatchEvent(Unknown Source)
      at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
      at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
      at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
      at java.awt.Container.dispatchEventImpl(Unknown Source)
      at java.awt.Window.dispatchEventImpl(Unknown Source)
      at java.awt.Component.dispatchEvent(Unknown Source)
      at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
      at java.awt.EventQueue.access$500(Unknown Source)
      at java.awt.EventQueue$3.run(Unknown Source)
      at java.awt.EventQueue$3.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
      at java.awt.EventQueue$4.run(Unknown Source)
      at java.awt.EventQueue$4.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
      at java.awt.EventQueue.dispatchEvent(Unknown Source)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.run(Unknown Source)

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

      @@j.c8183 thx!

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

    excellent work as always! I'm really excited to start your Wizardry-clone tutorial. I love the original Wizardry and have always wanted to make a dungeon crawler like that. Best Java game tutorials by far!

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

      Cool, good luck on your development!

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

    Thank you RyiSnow, really helpful :)

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

      You're welcome :D

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

    I followed all the code but I get this error once I actually click on the button:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "javax.sound.sampled.Clip.setFramePosition(int)" because "this.clip" is null
    The sound sample I used was a 16-bit wav file and I made sure to connect it with the folder location in the code

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

      3 years later lol when you load the sound file make sure you say getClass.getClassLoader.getResourceAsStream

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

    Thank you for uploading this, I'm using it for my Java final project

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

      You're welcome!

  • @ApexPreds1
    @ApexPreds1 7 лет назад +1

    why does my "public class ButtonHandler implements ActionListener" needs unimplemented method or its saying that needs abstract? I do unimplemented method and its not working error "AWT-EventQueue-0".

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

    For me the button only appeared if window.setVisible(true) was the last line. It is not like that for you, but it still appears for you...

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

    OMG finally!! Thank you so much!

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

    Great video!! Very well explained and helpful!

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

    Ryi Snow can you add sound effects to game characters like in some games when they jump make a sound .

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

      You can do that. You just need to set the sound effect file and call the method whenever you want to play the jump sound. Please check my How to Make a 2D Game in Java tutorial where I explain the process in detail!

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

    Can I ask why the frame is not showing out although I followed the code as shown?

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

      May I see your code?

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

      @@RyiSnow yea sure, I will email it to u, thanks.

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

    Hi RyiSnow, I've been enjoying your game programming tutorials; I've finished the text adventure videos, plus the multi-class version, and now I'm going through the extras. I've followed them all so far and gotten each to work just fine. Unfortunately, I cannot for the life of me to get the sound to play for this tutorial. I've checked my code and it all looks good; I even copied the code you posted and just changed the .wav file name to match what I have - but it still doesn't work. I've tried several different sound files and played with the audio/volume settings on my computer, but the sound just won't play when I click the button. Do you have any ideas on why this might be? Any suggestions on how to fix it? Your help would be much appreciated

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

      Hi, thank you for the comment. May I see your code? Then I will check it in my environment!

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

      @@RyiSnow h
      Here you go:
      import java.awt.Color;
      import java.awt.Container;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.io.File;
      import javax.sound.sampled.AudioInputStream;
      import javax.sound.sampled.AudioSystem;
      import javax.sound.sampled.Clip;
      import javax.swing.JButton;
      import javax.swing.JFrame;
      import javax.swing.JPanel;
      public class Sound {
      JFrame window;
      Container con;
      JPanel buttonPanel;
      JButton soundButton;
      String snapSound;
      ButtonHandler bHandler = new ButtonHandler();
      SoundEffect se = new SoundEffect();
      public static void main(String[] args) {
      new Sound();
      }
      public Sound() {
      window = new JFrame();
      window.setSize(800, 600);
      window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      window.getContentPane().setBackground(Color.black);
      window.setLayout(null);
      con = window.getContentPane();
      buttonPanel = new JPanel();
      buttonPanel.setBounds(300, 300, 200, 100);
      buttonPanel.setBackground(Color.black);
      con.add(buttonPanel);
      soundButton = new JButton("Sound Effect");
      soundButton.setFocusPainted(false);
      soundButton.addActionListener(bHandler);
      soundButton.setActionCommand("soundB");
      buttonPanel.add(soundButton);
      window.setVisible(true);
      snapSound = ".//res//LatexSnap05.wav";
      }
      public class SoundEffect {
      Clip clip;
      public void setFile(String soundFileName){
      try{
      File file = new File(soundFileName);
      AudioInputStream sound = AudioSystem.getAudioInputStream(file);
      clip = AudioSystem.getClip();
      clip.open(sound);
      }
      catch(Exception e){
      }
      }
      public void play(){
      clip.setFramePosition(0);
      clip.start();
      }
      }
      public class ButtonHandler implements ActionListener{
      public void actionPerformed(ActionEvent event){
      se.setFile(snapSound);
      se.play();
      }
      }
      }

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

      @@RyiSnow I figured out what the problem was - I was using a 32bit wav file not a 16bit - it's working now

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

      Oh I see. I didn't know that. Thanks for letting me know. Also, I'm sorry for the late reply. I just noticed that you have posted your code (sometimes you don't get notification when someone responds in already established thread). Anyway, I just tried to run the code with a 32bit wav and indeed, it didn't make sound as you said. I think I will add a warning comment.

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

    Thanks! Im learning java and this really helps for my poroyect.

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

    I have a question, why when I export the program the sound dosen't work ?
    edit: I found the part 9, and I'm going to learn why, sorry :]

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

    Hello, I am following your guide but at 6:59 on the video, where I had to create a sound button, I did exactly what you did but no button appeared. what do I do?

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

      Could you show me your code? Then maybe I can find out what the problem is.

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

      could I send you an image via gmail? as I want to show you what the end Window looks like, plus the code.

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

      If I could see the code, I can reproduce the issue in my environment. So just code is fine!

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

      but right now my code is:
      package sound.test;
      import java.awt.Color;
      import java.awt.Container;
      import javax.swing.JButton;
      import javax.swing.JFrame;
      import javax.swing.JPanel;
      /**
      *
      * @author STRIX
      */
      public class Sound {
      JFrame window;
      Container con;
      JPanel buttonPanel;
      JButton soundButton;
      /**
      * @param args the command line arguments
      */
      public static void main(String[] args) {
      new Sound();
      }
      public Sound() {
      window = new JFrame();
      window.setSize(800,600);
      window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      window.getContentPane().setBackground(Color.BLACK);
      window.setLayout(null);
      con = window.getContentPane();
      window.setVisible(true);
      buttonPanel = new JPanel();
      buttonPanel.setBounds(300, 300, 200, 100);
      buttonPanel.setBackground(Color.BLUE);
      con.add(buttonPanel);
      soundButton = new JButton("Sound Effect");
      soundButton.setFocusPainted(false);
      buttonPanel.add(soundButton);
      }
      }

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

      If you resize the window, your button shows up. Sometimes this happens and I'm not sure why.
      That said to avoid this, please move
      window.setVisible(true);
      to the end of Sound method (after buttonPanel.add(soundButton);).
      Then soundButton should show up.

  • @occultandmysticism4796
    @occultandmysticism4796 7 лет назад

    Could you make a tutorial for making a text adventure GUI? I've been trying to make something like Adventure or Zork, and I want to capture that old school black background green text command prompt type aesthetic, but I'm not sure how to handle the user input. Your videos are great, thanks so much!

    • @RyiSnow
      @RyiSnow  7 лет назад +1

      Hi. You can handle user input with JTextField. That's also a good function for a game so maybe I will make a tutorial for it(can't say when though). Anyways the game you're trying to make sounds pretty interesting. I'd love to play when it's done :)

    • @RyiSnow
      @RyiSnow  7 лет назад

      I uploaded a tutorial video about handling user input today. Probably you've already figured it out but please check it if you feel like it :)

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

    For some reason my sound is not appearing.

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

      Are you using a .wav file?
      If yes:
      It's probably 32bit file. It needs to be a 16bit .wav file to work.
      If no:
      Find either a .wav file or a .aiff file - both need to be 16bit - and try that. Those are the only two file formats I've found work.

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

    I used your code and there are some problems when I did it on my MacBook pro

  • @kris030
    @kris030 7 лет назад

    Eclipse says there is no such class! Can someone help me?

    • @RyiSnow
      @RyiSnow  7 лет назад

      Which class you mean? Could you provide more details?

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

    Not working. If possible share your codes here please and thanks in advance

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

      Just added a link in the description so please check that. Make sure to create a res folder and place a file correctly.