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 8612915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:44:40+00:00 2026-06-12T04:44:40+00:00

I am trying to add sound in program. I have no experience how to

  • 0

I am trying to add sound in program. I have no experience how to add sound in program.
I am trying this but this code giving me exception “null”. I don’t know what argument I have to pass in playSound function. So help me please.

import java.io.InputStream;
import sun.audio.*; //import the sun.audio package
import java.io.*;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

public class Main {

    public Main() {

    }

    public static void main(String[] args) {
        System.out.println("hello there");
        playSound("Hi there");

    }

    public static synchronized void playSound(final String url) {
        new Thread(new Runnable() { // the wrapper thread is unnecessary, unless
                                    // it blocks on the Clip finishing, see
                                    // comments
                    public void run() {
                        try {
                            Clip clip = AudioSystem.getClip();
                            AudioInputStream inputStream = AudioSystem
                                    .getAudioInputStream(Main.class
                                            .getResourceAsStream("pacman_chomp.wav"
                                                    + url));
                            clip.open(inputStream);
                            clip.start();
                        } catch (Exception e) {
                            System.err.println(e.getMessage());
                        }
                    }
                }).start();
    }

}
  • 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-12T04:44:42+00:00Added an answer on June 12, 2026 at 4:44 am

    Since you said your playSound method didn’t work, here’s a sound class I coded.

    import java.io.IOException;
    import java.io.InputStream;
    
    import javax.sound.sampled.AudioFormat;
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.DataLine;
    import javax.sound.sampled.LineUnavailableException;
    import javax.sound.sampled.SourceDataLine;
    import javax.sound.sampled.UnsupportedAudioFileException;
    
    public class AePlayWave extends Thread {
    
        protected static final boolean DEBUG = false;
    
        protected InputStream inputStream;
    
        public AePlayWave(InputStream inputStream) {
            this.inputStream = inputStream;
            if (DEBUG) System.out.println("AePlayWave constructor");
        }
    
        @Override
        public void run() {
            if (DEBUG) System.out.println("AePlayWave running");
    
            AudioInputStream audioInputStream = verifyInputStream();
            if (audioInputStream == null) {
                return;
            }
    
            AudioFormat format = audioInputStream.getFormat();
            SourceDataLine audioLine = openInputStream(format);
    
            if (DEBUG) System.out.println(audioLine.getLineInfo());
    
            if (audioLine != null) {
                audioLine.start();
                playInputStream(audioInputStream, audioLine);
            }
        }
    
        protected AudioInputStream verifyInputStream() {
            if (DEBUG) System.out.println("AePlayWave verifyInputStream");
            AudioInputStream audioInputStream = null;
            try {
                audioInputStream = AudioSystem.getAudioInputStream(inputStream);
            } catch (UnsupportedAudioFileException e) {
                e.printStackTrace();
                return null;
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
            return audioInputStream;
        }
    
        protected SourceDataLine openInputStream(AudioFormat format) {
            if (DEBUG) System.out.println("AePlayWave openInputStream");
            DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
            SourceDataLine audioLine = null;
            if (DEBUG) System.out.println("AePlayWave openInputStream try");
            try {
                audioLine = (SourceDataLine) AudioSystem.getLine(info);
                if (DEBUG) System.out.println("AePlayWave openInputStream getLine");
                audioLine.open(format);
                if (DEBUG) System.out.println("AePlayWave openInputStream open");
            } catch (LineUnavailableException e) {
                e.printStackTrace();
                return null;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
            return audioLine;
        }
    
        protected void playInputStream(AudioInputStream audioInputStream,
                SourceDataLine audioLine) {
            if (DEBUG) System.out.println("AePlayWave playInputStream");
            int externalBufferSize = (int) audioInputStream.getFrameLength() * 4;
            if (DEBUG) System.out.println("AePlayWave playInputStream externalBufferSize: " 
                    + externalBufferSize);
            int nBytesRead = 0;
            byte[] abData = new byte[externalBufferSize];
    
            try {
                while (nBytesRead != -1) {
                    nBytesRead = audioInputStream.read(abData, 0, abData.length);
                    if (nBytesRead >= 0) {
                        if (DEBUG) System.out.println("Bytes read: " + nBytesRead);
                        audioLine.write(abData, 0, nBytesRead);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
                return;
            } finally {
                audioLine.drain();
                audioLine.close();
            }
        }
    }
    

    And here’s how it’s called:

    InputStream inputStream = getClass().getResourceAsStream("alarm-clock-1.wav");
    AePlayWave playWave = new AePlayWave(inputStream);
    playWave.run();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might sound silly but I have been trying to execute a NUnit test
Now this might sound simple, but I'm a bit mixed up. I am trying
I know this will sound odd but stay with me on this. I am
this may sound kind of convoluted but basically i want to have a form
I'm trying to adapt a code in order to add a sound capture feature
I have a simple c++ program I am trying to debug, but gdb cannot
I am trying add picture boxes dynamically. My code is as PictureBox picture =
Im new to asp.net mvc. I'm trying add new model class but it got
Trying to add a blank sample app for a rails tutorial to GitHub, but
Trying to add a class object into a List using reflection, but when invoking

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.