Open Java Clip File Before Actually Playing it? - java

I have a button that plays this code when pressed:
public void music() throws UnsupportedAudioFileException, IOException {
try {
URL url = this.getClass().getResource("/audio/InGame01.mid");
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
But, not only is the sound delayed (I'm assuming its because it has to read the sound file, and then play it), the button also becomes frozen until it finishes reading the sound clip. Is there a simple way to open the audio clip before the button is actually pressed, so that it already has it loaded? (clip.open(audioIn) is what I'm talking about.)
This is where the method is called(Button Listener):
try {
music();
} catch (UnsupportedAudioFileException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}

Related

Java Clips Lag on First Play

I have sound effects for a game. Upon the game frame opening, the first sound lags behind. After that sound plays, no more lag is experienced.
Here's my clip player:
public enum SoundEffect
{
WALL("ping_pong_8bit_plop"),
PADDLE("ping_pong_8bit_beeep"),
POINT("ping_pong_8bit_peeeeeep");
public static enum Volume
{
MUTE, UNMUTE
}
public static Volume volume = Volume.MUTE;
private Clip clip;
SoundEffect (String file)
{
try
{
AudioInputStream inputStream = AudioSystem.getAudioInputStream(this.getClass().getResource(file+".wav"));
AudioFormat format = inputStream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
clip = (Clip)AudioSystem.getLine(info);
clip.open(inputStream);
}
catch (UnsupportedAudioFileException uae)
{
uae.printStackTrace();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
catch (LineUnavailableException lue)
{
lue.printStackTrace();
}
}
public void play()
{
if (volume != Volume.MUTE)
{
if (clip.isRunning())
clip.stop();
clip.flush();
clip.setFramePosition(0);
clip.start();
}
}
static void init()
{
values();
}
}
So when I call SoundEffect.WALL.play() for example, it plays fine overall, but the very first time it plays there is a huge lag spike. What can I do to solve this, preferably still using Clips?
I had the same bug, the first play of a clip was about 1-2 sec delayed, no matter when the play()-method is called. What I did is I played a clip in the main() method which is a half seccond long and contains nothing (no sound). This means that the play() method of the actual sound isn't called the first time, and for me, it works. Hope it helped.
Use a separate Thread
Thread music = new Thread(new Runnable() {
#Override public void run() { your code }
};
music.start();

Playing an audio file in java

My goal is to make a program that displays a pop up window with a single button called play. I am using an inner class and action listener. The button does in fact play the audio clip. Now what I would like to do is make it so every time I hit the play button it plays it from the beginning and I do not hear more than one instance of the clip being played in it's completion. Here is the code I have for the inner class only:
private class PlaySound implements ActionListener {
public void actionPerformed(ActionEvent e) {
String filename = "/Users/philipgouldman/Desktop/iPhoneRingtones/Dummy_Yeah.wav";
try {
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(new File(filename)));
clip.setFramePosition(0);
clip.start();
} catch (Exception exc) {
exc.printStackTrace(System.out);
}
}
}
I would appreciate any tips or suggestions to achieve this goal. I read on the Clip Interface javadoc that if the user wants the action to restart to simply invoke stop following setFramePosition(0). I can't say I quite understand what that means. Can someone guide me in the right direction?
Move your Clip Object outside of this handle so that its state can be investigated later.
e.g.
private class PlaySound implements ActionListener {
private Clip clip = null;
public void actionPerformed(ActionEvent e) {
String filename = "/Users/philipgouldman/Desktop/iPhoneRingtones/Dummy_Yeah.wav";
try {
if (clip == null) {
clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(new File(filename)));
}
clip.setFramePosition(0);
clip.start();
} catch (Exception exc) {
exc.printStackTrace(System.out);
}
}
}

While loop not working In audio player

I'm using a sound player and I'm trying to create a while loop that knows when to terminate the JFrame it's playing in. The while loop should loop while the clip (recording) is playing and end when the clip is done. But here's the deal: The while loop only works when it prints something out, which I DON'T want it to do?
import java.io.*;
import java.net.URL;
import javax.sound.sampled.*;
import javax.swing.*;
public class SoundClipTest extends JFrame {
public SoundClipTest() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
// Open an audio input stream.
File soundFile = new File("Testing/recording (2).wav");
AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);
//URL url = new URL("http://maximumdonline.com/miscwavs/pacman.wav");
//AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
// Get a sound clip resource.
Clip clip = AudioSystem.getClip();
// Open audio clip and load samples from the audio input stream.
clip.open(audioIn);
clip.start();
this.setVisible(true);
this.setVisible(false);
boolean run = true;
//problem While Loop
while(clip.isActive()){
//Only works when I print something out during the loop:
//System.out.println(run);
}
//Test While Loop has ended.
run = false;
System.out.println(run);
//Close JFrame
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
Note: Most of this sound code came from
https://www.ntu.edu.sg/home/ehchua/programming/java/J8c_PlayingSound.html
It works. I've tested it, but my while loop is crying!
The correct way to approach this task is to add a LineListener to the Clip. A Clip implements javax.sound.sampled.Line interface, so we can use Line.addLineListener(LineListener)..
Adds a listener to this line. Whenever the line's status changes, the listener's update() method is called with a LineEvent object that describes the change.
Try modifying your code to
while (clip.isActive()) {
if(!clip.isRunning())
clip.stop();
}
This did the trick for me

Stopping my playing music in Java

I am writing a programm and I added some background music to it.
However I would like to be able to stop the music by the click of a button.
I know how to add buttons and stuff however I don't know how to stop the music, I have tried a lot of things but just can't get it to work.
This is my code:
new Thread(new Runnable() {
public void run() {
try {
Clip Music = AudioSystem.getClip();
AudioInputStream BMG = AudioSystem.getAudioInputStream(
Main.class.getResourceAsStream("/Music/BackgroundMusic.wav"));
Music.open(BMG);
Music.start();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}).start();
If you could help that would be awesome!
Thank you in advance,
JS
Clip Music=null;
new Thread(new Runnable() {
public void run() {
try {
Clip Music = AudioSystem.getClip();
AudioInputStream BMG = AudioSystem.getAudioInputStream(
Main.class.getResourceAsStream("/Music/BackgroundMusic.wav"));
Music.open(BMG);
Music.start();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}).start();
//this goes inside the actionPerformed event of the button
if(Music!=null)Music.stop();
And as m0skit0 said you better start your variables with a lowercase letter,don't be a c# smug.

Error with playing sounds

I have faced an error with playing sounds on button click in java. I have a field of buttons and if any button is pressed application plays a sound. But application throws me an exception:
javax.sound.sampled.LineUnavailableException: unable to obtain a line
Method look like this and is called when button was pressed:
public void playSound() {
try {
File file = new File("Sounds/sound.wav");
AudioInputStream hitStream = AudioSystem.getAudioInputStream(file);
AudioFormat format = hitStream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(hitStream);
clip.start();
} catch (Exception e) {
e.printStackTrace();
}
}
Sound play at the beginning then it crashes. I googled the exception, found that the clip should be closed after playing sound. I thought I should add lines after clip.start()
if (!clip.isRunning()) {
clip.stop();
clip.close();
}
But then the sounds don't play and whole application start to lag. What is the correct solution for this error?

Categories

Resources