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("testPl ay started");
try {
File file = new File(filename);
AudioInputStream in= AudioSystem.getAudioInputS tream(file );
AudioInputStream din = null;
AudioFormat baseFormat = in.getFormat();
AudioFormat decodedFormat = new AudioFormat(AudioFormat.En coding.PCM _SIGNED,
baseFormat.getSampleRate() ,
16,
baseFormat.getChannels(),
baseFormat.getChannels() * 2,
baseFormat.getSampleRate() ,
false);
din = AudioSystem.getAudioInputS tream(deco dedFormat, in);
// Play now.
rawplay(decodedFormat, din);
in.close();
} catch (Exception e)
{
System.out.println("except ion 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(SourceDataLi ne.class, audioFormat);
res = (SourceDataLine) AudioSystem.getLine(info);
res.open(audioFormat);
return res;
}
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("testPl
try {
File file = new File(filename);
AudioInputStream in= AudioSystem.getAudioInputS
AudioInputStream din = null;
AudioFormat baseFormat = in.getFormat();
AudioFormat decodedFormat = new AudioFormat(AudioFormat.En
baseFormat.getSampleRate()
16,
baseFormat.getChannels(),
baseFormat.getChannels() * 2,
baseFormat.getSampleRate()
false);
din = AudioSystem.getAudioInputS
// Play now.
rawplay(decodedFormat, din);
in.close();
} catch (Exception e)
{
System.out.println("except
}
}
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(SourceDataLi
res = (SourceDataLine) AudioSystem.getLine(info);
res.open(audioFormat);
return res;
}
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!
Thank you!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you! A very quick and concise answer. =)
:)
To solve this start a seperate thread and play your audio on that thread.