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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:07:39+00:00 2026-05-23T03:07:39+00:00

I am tyring to write some Java code that basically just plays a short

  • 0

I am tyring to write some Java code that basically just plays a short .wav file – with ‘short’ I mean a fraction of a second. (The file I use is at /usr/share/sounds/generic.wav for those of you using Ubuntu.)

The problem is, I can’t seem to figure out how to play that sample reliably, i.e., in all my attempts, I can get my program to play the sound in 4 out of 5 times or so, but never 100%.

This is what has worked best so far as a stand-alone program:

File soundFile = new File("/usr/share/sounds/generic.wav");
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(soundFile);
clip.open(inputStream);
clip.start(); 

(Note that the code doesn’t even call clip.stop()) But even with that one, if I run it a couple of times in a row, sooner or later there will be a run without any sound being played, but no Exceptions either.

Variations I’ve tried:

  1. Loading the audio file into a byte array and passing that to clip.open

  2. Attaching a LineListener to the clip to wait for STOP events

plus a couple of random try-outs, but so far I haven’t managed to create code that works every time.

I’m also aware of the following bug: https://bugs.java.com/bugdatabase/view_bug?bug_id=4434125 but I’m using Java 1.6 and the report claims that things should be fine with Java 1.5 or later.

Any ideas? Is it PulseAudio?

  • 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-05-23T03:07:40+00:00Added an answer on May 23, 2026 at 3:07 am

    I suspect now that the reason my test program failed was a timing issue. Either I attempted playing the short sound before the samples were fully loaded, or the program terminated too quickly. The reason for this suspicion is that if I change the above code slightly like so:

    File soundFile = new File("/usr/share/sounds/generic.wav");
    Clip clip = AudioSystem.getClip();
    AudioInputStream inputStream = AudioSystem.getAudioInputStream(soundFile);
    clip.open(inputStream);
    
    while (System.in.read() == '\n') {
        clip.stop();
        clip.setFramePosition(0);
        clip.start();
    }
    

    then the short sound gets played correctly every time I hit enter.

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

Sidebar

Related Questions

I've been trying to write some very fast Java code that has to do
What's up y'all, I am trying to write some code in Java that will
I'm trying to write some LINQ To SQL code that would generate SQL like
I'm trying to write some C# code that calls a method from an unmanaged
I'm trying to write some code to work with an htdigest password file. The
I was trying to write some code that would check if an item has
So, im trying to write some code that utilizes Nvidia's CUDA architecture. I noticed
I have a mobile platform that I am trying to write some communications code
I was trying to write some code that looked like this: public List<IObject> getObject(){
I'm trying to write some Java code for a set of enum classes. Each

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.