How do i pause a clip? Java - java

I have a music player that works perfectly fine but i want to add a play/pause button. I have set the button up and all that but i don't know the code to actually pause the clip.
Here is my code:
try{
File f = new File("songs/mysong.wav");
Clip clip = AudioSystem.getClip();
AudioInputStream ais = AudioSystem.getAudioInputStream(f);
clip.open(ais);
playing = true;
if(MusicPlayer.pause)
{
clip.stop(); // <- Doesnt stop the song
}
clip.loop(Clip.LOOP_CONTINUOUSLY);
}catch(Exception exception){System.out.println("Failed To Play The WAV File!");}
Thanks in advance!

Before stopping the clip, assign a long variable to take the value of the current time of the clip.
For example:
long clipTime;
clipTime= clip.getMicrosecondPostion();
clip.stop();
//When you want to resume the clip from the last position
clip.setMicrosecondPosition(clipTime);
clip.start();

You need to call clip.start(); after clip.open(ais) then clip.stop() will work.

Related

How to append multiple files to Audio InputStream

I have an audio player, I have found how to add a single file to the audio inputstream, but I have an arraylist of files I want to add. How should I do that?
public class AudioPlayer {
Long currentFrame;
Clip clip;
// current status of clip
String status;
AudioInputStream musicInputStream;
// constructor to initialize streams and clip
public AudioPlayer(Schedule schedule,List<File> files)
throws UnsupportedAudioFileException,
IOException, LineUnavailableException
{
// create AudioInputStream object
musicInputStream =
AudioSystem.getAudioInputStream(files.get(0).getAbsoluteFile());
// create clip reference
clip = AudioSystem.getClip();
// open audioInputStream to the clip
clip.open(musicInputStream);
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
}
I'm sure this answer comes too late to be helpful to you, but I am searching for the answer to a similar question. The solution provided here suggests that you could create a loop to join two AudioInputStream objects together until you have a single final AudioInputStream:
Join two WAV files from Java?

Java wav sounds NullPointer

I found this code on the Internet for playing a .wav file
public static synchronized void playSound(final String url) {
new Thread(new Runnable() {
public void run() {
try {
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(
ClientMain.class.getResourceAsStream("sounds/" + url));
clip.open(inputStream);
clip.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
When I call getAudioInputStream() it gives me a NullPointerException.
Here is the error:
java.lang.NullPointerException
at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(SoftMidiAudioFileReader.java:130)
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1111)
at it.whispers.rain.ClientMain$10.run(ClientMain.java:415)
at java.lang.Thread.run(Thread.java:744)
java.net.SocketException: Socket is closed
at java.net.Socket.getOutputStream(Socket.java:916)
at it.whispers.rain.ClientMain.send(ClientMain.java:400)
at it.whispers.rain.ClientMain.Disconnect(ClientMain.java:373)
at it.whispers.rain.ClientMain.run(ClientMain.java:319)
at java.lang.Thread.run(Thread.java:744)
EDIT:
This is what i give when the .wav file is loaded:
java.lang.IllegalArgumentException: Invalid format
at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.createStream(PulseAudioDataLine.java:142)
at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.open(PulseAudioDataLine.java:99)
at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.open(PulseAudioDataLine.java:283)
at org.classpath.icedtea.pulseaudio.PulseAudioClip.open(PulseAudioClip.java:402)
at org.classpath.icedtea.pulseaudio.PulseAudioClip.open(PulseAudioClip.java:453)
at it.whispers.rain.ClientMain$10.run(ClientMain.java:418)
at java.lang.Thread.run(Thread.java:744)
When you generate a .jar, you can embed resource files (files that can be read out by the java runtime environment).
This line reads out files that are embedded in the .jar:
ClientMain.class.getResourceAsStream("sounds/" + url));
Since you probably haven't added a .wav file in the sounds directory of the .jar file. The method cannot fetch that file and returns null. The file is (in most cases) loaded relatively from the class file (thus ClientMain.class).
You can simply modify the line by opening a File from the file system, or embed a wave file.
See this for more details.
EDIT: A second error is the format error. Perhaps you can solve this by fetching the format:
AudioFormat format = inputStream.getFormat();
and then:
DataLine.Info info = new DataLine.Info(Clip.class, format);
Clip clip = (Clip)AudioSystem.getLine(info);//you should postpone the creation of the clip
So the full code (in the Thread):
AudioInputStream inputStream = AudioSystem.getAudioInputStream(ClientMain.class.getResourceAsStream("sounds/" + url));
DataLine.Info info = new DataLine.Info(Clip.class, format);
Clip clip = (Clip)AudioSystem.getLine(info);
clip.open(inputStream);
clip.start();
Possibly you created a Clip that has an encoding/bitrate/... that does not correspond to the actual .wav file.

I'm trying to add sound and i've searched this site but here error comes file no found why?

KeyListener s;
try {
AudioInputStream audio = AudioSystem.getAudioInputStream(new File("x.wav"));
Clip clip = AudioSystem.getClip();
clip.open(audio);
clip.start();
}
catch(UnsupportedAudioFileException uae) {
System.out.println(uae);
}
catch(IOException ioe) {
System.out.println(ioe);
}
catch(LineUnavailableException lua) {
System.out.println(lua);
}// TODO add your handling code here:
I have a file With name x.wav do i need to enter path or save it in its directory
Your file path only gives the file name, not the location. When your code is executing, the program knows that it is looking for x.wav but has no idea where to find it.
Make sure that you include the entire path name in the file object, like C:\path\to\audio\file\x.wav.
If you do not want to give the entire path name in the code, you would have to use System.getProperty() to get a relative directory for your path. http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html lists some of the arguments that you can pass into the getProperty() method.

How to remove LOOP_CONTINUOUSLY in Javasound? [code inside]

I have saw this code somewhere in this area. I want to play the .wav file once but not continuously. How can I do that? I try removing the LOOP_CONTINUOUSLY line but it does not work.
URL url = new URL("http://pscode.org/media/leftright.wav");
Clip clip = AudioSystem.getClip();
// getAudioInputStream() also accepts a File or InputStream
AudioInputStream ais = AudioSystem.
getAudioInputStream( url );
clip.open(ais);
clip.loop(Clip.LOOP_CONTINUOUSLY);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(null, "Close to exit!");
}
});
And if possible. Can I play mp3 files as well. Because when I try to replace it as mp3, my code crashes. Also, I want to get the file from my computer. Not on the internet. Can anyone help me?
Use clip.start() instead of clip.loop(Clip.LOOP_CONTINUOUSLY)
Use File or getResourceAsStream() instead of URL and feed it to getAudioInputStream the same way
See this question for mp3 files

Java: Playing sound when message revived (IP chat program)

I know there are a lot of playing sound things. I am building a IP chat program. I am very new to programming (a nursing major if you must know). I am using eclipse. I am trying to have it play a sound when a message is received. I don't know how to make a class that will call the file and then play it. Thanks!
This is what i have right now (yes I know it is commented out):
public void playsound(final String input) {
final java.util.Date date= new java.util.Date();
//String stringFile = "x.wav";
//File wavfile = new File("notification.wav");
//AudioInputStream audioInput = AudioSystem.getAudioInputStream(wavfile);
//AudioFormat format = audioInput.getFormat();
//DataLine.Info info = new DataLine.Info(Clip.class, format);
//clip = AudioSystem.getClip();
}
Replacing clip = AudioSystem.getClip(); with
Clip clip = AudioSystem.getClip();
clip.open(audioInput);
clip.start();
removing String stringFile = "x.wav";
and uncommenting the rest should be sufficient.
There's a tutorial on playing files from a file inside a JAR here, which might be more useful than specifying a file in the file system: http://www3.ntu.edu.sg/home/ehchua/programming/java/J8c_PlayingSound.html

Categories

Resources