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...
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!
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 :)
@@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)
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!
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
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".
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!
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
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.
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?
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.
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!
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 :)
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.
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...
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!
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 :)
@@j.c8183 thanks a lot bro
@@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)
@@j.c8183 thx!
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!
Cool, good luck on your development!
Thank you RyiSnow, really helpful :)
You're welcome :D
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
3 years later lol when you load the sound file make sure you say getClass.getClassLoader.getResourceAsStream
Thank you for uploading this, I'm using it for my Java final project
You're welcome!
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".
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...
OMG finally!! Thank you so much!
Great video!! Very well explained and helpful!
Ryi Snow can you add sound effects to game characters like in some games when they jump make a sound .
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!
Can I ask why the frame is not showing out although I followed the code as shown?
May I see your code?
@@RyiSnow yea sure, I will email it to u, thanks.
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
Hi, thank you for the comment. May I see your code? Then I will check it in my environment!
@@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();
}
}
}
@@RyiSnow I figured out what the problem was - I was using a 32bit wav file not a 16bit - it's working now
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.
Thanks! Im learning java and this really helps for my poroyect.
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 :]
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?
Could you show me your code? Then maybe I can find out what the problem is.
could I send you an image via gmail? as I want to show you what the end Window looks like, plus the code.
If I could see the code, I can reproduce the issue in my environment. So just code is fine!
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);
}
}
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.
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!
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 :)
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 :)
For some reason my sound is not appearing.
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.
I used your code and there are some problems when I did it on my MacBook pro
Eclipse says there is no such class! Can someone help me?
Which class you mean? Could you provide more details?
Not working. If possible share your codes here please and thanks in advance
Just added a link in the description so please check that. Make sure to create a res folder and place a file correctly.