Link to home
Start Free TrialLog in
Avatar of SmileMagician
SmileMagician

asked on

GUI goes blank while an mp3 file is being played

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.getSampleRate(),
                                                                                  16,
                                                                                  baseFormat.getChannels(),
                                                                                  baseFormat.getChannels() * 2,
                                                                                  baseFormat.getSampleRate(),
                                                                                  false);
    din = AudioSystem.getAudioInputStream(decodedFormat, in);
    // Play now.
    rawplay(decodedFormat, din);
    in.close();
  } 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
    line.start();
    int nBytesRead = 0, nBytesWritten = 0;
    while (nBytesRead != -1)
    {
        nBytesRead = din.read(data, 0, data.length);
        if (nBytesRead != -1) nBytesWritten = line.write(data, 0, nBytesRead);
    }
    // Stop
    line.drain();
    line.stop();
    line.close();
    din.close();
stopBtn.setEnabled(false);
      playBtn.setEnabled(true);
  }
}

private SourceDataLine getLine(AudioFormat audioFormat) throws LineUnavailableException
{
  SourceDataLine res = null;
  DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
  res = (SourceDataLine) AudioSystem.getLine(info);
  res.open(audioFormat);
  return res;
}
Avatar of Mick Barry
Mick Barry
Flag of Australia image

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.
Avatar of SmileMagician
SmileMagician

ASKER

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!
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you! A very quick and concise answer.  =)