GUI goes blank while an mp3 file is being played

Posted on 2005-04-16
Last Modified: 2010-03-31
NOTE: I accidentally asked this question in the wrong category first. So that question is 250 points on top of this 50 for this one.

I am building a little Java program that will play mp3s. With my current coding, the mp3s play just fine, the only thing is that while an mp3 is playing, the GUI goes completely blank until the mp3 is finished playing. Why would this happen and what do i do to fix it?  I have pasted the code to play the mp3s below.  Thank you!

public void testPlay(String filename)
System.out.println("testPlay started");
  try {
    File file = new File(filename);
    AudioInputStream in= AudioSystem.getAudioInputStream(file);
    AudioInputStream din = null;
    AudioFormat baseFormat = in.getFormat();
    AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
                                                                                  baseFormat.getChannels() * 2,
    din = AudioSystem.getAudioInputStream(decodedFormat, in);
    // Play now.
    rawplay(decodedFormat, din);
  } catch (Exception e)
        System.out.println("exception encountered!");

private void rawplay(AudioFormat targetFormat, AudioInputStream din) throws IOException,                                                                                                LineUnavailableException
  byte[] data = new byte[4096];
  SourceDataLine line = getLine(targetFormat);
  if (line != null)
    // Start
    int nBytesRead = 0, nBytesWritten = 0;
    while (nBytesRead != -1)
        nBytesRead =, 0, data.length);
        if (nBytesRead != -1) nBytesWritten = line.write(data, 0, nBytesRead);
    // Stop

private SourceDataLine getLine(AudioFormat audioFormat) throws LineUnavailableException
  SourceDataLine res = null;
  DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
  res = (SourceDataLine) AudioSystem.getLine(info);;
  return res;
Question by:SmileMagician
    LVL 92

    Expert Comment

    Swing is single threaded so if you block that thread your gui will block.
    To solve this start a seperate thread and play your audio on that thread.
    LVL 1

    Author Comment

    Oh I see, I thought it would probly be something simple like that. Do you happen to have a quick example of what starting a new thread would look like? is there any limit to the number of threads that can be running?

    Thank you!
    LVL 92

    Accepted Solution


    Basically rou need a class that implements Runnable and you start a thread using:

    Myrunnable worker = new MyRunnable();
    Thread mythread = new Thread(worker);

    That will result in the run() method of worker being called on a new thread.
    And you can start any number of threads (limited only by performance)
    LVL 1

    Author Comment

    Thank you! A very quick and concise answer.  =)
    LVL 92

    Expert Comment


    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now