Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7955103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:38:04+00:00 2026-06-04T03:38:04+00:00

I am making a Java based game for which I want to add some

  • 0

I am making a Java based game for which I want to add some sound effects. I searched and found myself more confused. I know the coding differs from file format to format.
I just need some sounds – does not matter which format. So please suggest me the easiest file format. Code snippet will be extremely helpful.

What is the easiest format and way to provide sound effects?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T03:38:05+00:00Added an answer on June 4, 2026 at 3:38 am

    for short sounds, you should use WAV or AU, WAV being the most known format for small sounds. i just finished this small program, all you need to do is have a .wav sound.

    this program spawns a window with a button and every time you click that button the specified sound will play.

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.IOException;
    import java.net.URL;
    
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.Clip;
    import javax.sound.sampled.LineUnavailableException;
    import javax.sound.sampled.UnsupportedAudioFileException;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public class PlaySound extends JFrame{
    
        private Clip clip;
    
        public static void main(String [] args) {
    
            PlaySound app = new PlaySound();
    
        }
    
        public PlaySound() {
            JButton play = new JButton("Play");//here we make the button
            play.addActionListener(new ActionListener() {//here we tell what the button will do
            public void actionPerformed(ActionEvent e) {
                playTheSound();//when its clicked call this method
            }
        });
    
        this.add(play);
        this.pack();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
    
    private void SoundEffect(URL url) {
        try {
            // Set up an audio input stream piped from the sound file.
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(url);
            // Get a clip resource.
            clip = AudioSystem.getClip();
            // Open audio clip and load samples from the audio input stream.
            clip.open(audioInputStream);
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        }
    }
    
    // Play or Re-play the sound effect from the beginning, by rewinding.
    public void playTheSound() {
    
        URL url = getClass().getResource("click.wav");//You can change this to whatever other sound you have
        SoundEffect(url);//this method will load the sound
    
        if (clip.isRunning())
            clip.stop();   // Stop the player if it is still running
        clip.setFramePosition(0); // rewind to the beginning
        clip.start();     // Start playing
    
        }
    
    }
    

    you can always change “click.wav” for another sound, including .au files.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making a text based game in Java. I have a text field,
I'm making a minecraft-inspired game through Java LWJGL, which is heavy into development already.
I am making a program based on string processing in Java in which I
I'm making a command-line based tool in Java, and I was thinking I might
I'm making Braid in Java. If you rewind the time, the sound plays backward.
I am making a distributed java app for which I need both parts of
I'm currently writing a very basic Java game based on the idea of Theme
I want to get some thoughts on best practices around making .NET app configuration
I am making a text-based RPG for a personal project in Java. In the
I'm making a text based RPG to help me learn Java. So I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.