?
Solved

GUI goes blank while an mp3 file is being played

Posted on 2005-04-16
5
Medium Priority
?
162 Views
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.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;
}
0
Comment
Question by:SmileMagician
  • 3
  • 2
5 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13800172
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.
0
 
LVL 1

Author Comment

by:SmileMagician
ID: 13800194
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!
0
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 13800454
http://java.sun.com/docs/books/tutorial/essential/threads/index.html

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

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

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)
0
 
LVL 1

Author Comment

by:SmileMagician
ID: 13809462
Thank you! A very quick and concise answer.  =)
0
 
LVL 92

Expert Comment

by:objects
ID: 13810213
:)
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

809 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