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

  • Home
  • SEARCH
  • 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 6001283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:48:46+00:00 2026-05-23T00:48:46+00:00

I have this music class for a game I’m making in an Introduction Course

  • 0

I have this music class for a game I’m making in an Introduction Course in high school. You can see the source where I got the code from.

I originally was using a Clip, but found out that had pretty small buffer sizes, and couldn’t play long songs well (at least not on our Macs at school). The new code works well, from what I understand it is getting “chunks” of the song, playing them, then getting more chunks. Problem is, when the music changes the songs will sometimes overlap, and when I exit the game, a terrible sound plays (at least on the Macs) probably caused because the stream of data is being cut before the game closes.

Is there anyway to fix this other than delaying the program a few seconds each time?
(Note: I don’t want to use external .jar’s or libraries, I’d like to keep it to strictly the JRE)

package mahaffeyfinalproject;

import java.io.File;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;


public class Music implements Runnable{

//SOURCE: http://www.ntu.edu.sg/home/ehchua/programming/java/J8c_PlayingSound.html
//Note: I've modified it slightly

private String location;
private boolean play;

public void Music(){

}

public void playMusic(String loc) {
    location = loc;
    play = true;
    try{
        Thread t = new Thread(this);
        t.start();
    }catch(Exception e){
        System.err.println("Could not start music thread");
    }
}

public void run(){
    SourceDataLine soundLine = null;
    int BUFFER_SIZE = 64*1024;

    try {
        File soundFile = new File(location);
        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(soundFile);
        AudioFormat audioFormat = audioInputStream.getFormat();
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
        soundLine = (SourceDataLine) AudioSystem.getLine(info);
        soundLine.open(audioFormat);
        soundLine.start();
        int nBytesRead = 0;
        byte[] sampledData = new byte[BUFFER_SIZE];

        while (nBytesRead != -1 && play == true) {
            nBytesRead = audioInputStream.read(sampledData, 0, sampledData.length);
            if (nBytesRead >= 0) {
               soundLine.write(sampledData, 0, nBytesRead);
            }
        }
    } catch (Exception e) {
        System.err.println("Could not start music!");
    }

    soundLine.drain();
    soundLine.close();

}

public void stop(){
    play = false;
}


}
  • 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-23T00:48:47+00:00Added an answer on May 23, 2026 at 12:48 am

    I’m not sure about the horrible sound you are getting on close, but I think I have an idea of how to solve the overlap problem you mentioned. It looks like your usage of the Sound API is fine, but I suspect you may be having a concurrency issue. I noticed that you start off a new thread for each sound you play. This is a good idea, but you need to be careful if you don’t want your songs to mix. My theory is that your code looks something like this:

    Music songA = new Music();
    Music songB = new Music();
    
    //Start playing song A
    songA.playMusic("path");
    
    //Some stuff happens with your game...
    
    //Some time later you want to change to song B
    songA.stop();
    songB.playMusic("other path");
    

    At first blush this looks fine, after all you were careful to stop songA before starting songB, right? Unfortunately, when it comes to concurrency few things are as simple as we might like them to be. Let’s look at a small bit of your code, specifically the while loop responsible for feeding data into the audio stream…

    //Inside run(), this is the interesting bit at the end
    
    while(nBytesRead != -1 && play == true){
        //Feed the audio stream while there is still data 
    }
    

    stop() sets play to false, causing the while loop to exit, after finishing the current iteration. If I’m not mistaken, each iteration of your while loop is sending 64k of data into the audio stream. 64k is a fair amount of audio data, and depending on the audio properties will go on for some time. What you really want to do is signal the thread to quit (i.e. set play to false), and then wait for the thread to finish. I believe this will fix the overlap problem, and if you’re not killing your threads properly this issue may also be causing the god-awful racket on quit. I humbly suggest the following change to your Music class (with no guarantee of correctness)…

    public class Music implements Runnable{
        private Thread t;    //Make the thread object a class field
    
        //Just about everything is the same as it was before...
    
        public void stop(){
            play = false;
            try{
                t.join()
            }catch(InterruptedException e){
                //Don't do anything, it doesn't matter
            }
       }
    }
    

    Hope this helps, and for the love of all that is good and pure don’t use my code as-is, it came off the top of my head in about 2 minutes. I recommend The Java Tutorials’ series on threading.

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

Sidebar

Related Questions

I have seen this code in ShoppingCart class in Music Store MVC3 tutorial: http://www.asp.net/mvc/tutorials/mvc-music-store
As you can see, I have this PlayLesson_01 activity which displays images and audio
I have this wonderful music library app: beets. When I run beet ls somequery
I'm building a music events website and want to have a 'share this event'
I have embeded a music in a webpage using this code: <embed src=upload/gnossiennes.mp3 autostart=true
I have this form: <%= form_tag posts_path, :method => :get, :class => search_nav do
I have this structure: 1) main activity: public class mainActivity extends Activity { @Override
Okey, this is my problem. I have one service class where Ive managed to
I have written a simple class to play audio files in a simple game.
I have a Class like this: class ClassA { public long classAID {get; set;}

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.