I am trying to make a program that plays sound back to you. How I got the sound was I went to this link and I had it speak some words for about 9 seconds. While he was speaking, I was recording him with Audacity. It recorded him at 16-bit PCM, 48 khz and a stereo channel.
The code I use to play the sound is,
public void playSound(String Path) {
try {
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(Variables.class.getResourceAsStream("/com/project/resources/" + Path));
clip.open(inputStream);
clip.start();
} catch (Exception e) {
e.printStackTrace();
System.err.println(e.getMessage());
}
}
and the error that pops up is at this link.
I have stored the file in another package located at com.project.resources.
If you have any questions about this situation, let me know.
Related
I want to play a .wav file continuously in Java. I found some code, but I can't make it work.
String fileName = "res/sound/buz.wav";
File file = new File(fileName);
AudioInputStream ais;
try {
Clip clip = AudioSystem.getClip();
ais = AudioSystem.getAudioInputStream(file);
clip.open(ais);
clip.loop(Clip.LOOP_CONTINUOUSLY);
} catch (UnsupportedAudioFileException | IOException
| LineUnavailableException e) {
e.printStackTrace();
}
I get an Invalid Format Exception on clip.open(ais):
Exception in thread "main" 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 launcher.Launcher.main(Launcher.java:59)
I checked and file is created correctly and exists. So, what is the problem with my code?
If it matters, I'm working on Linux, but this should work on both Linux and Windows...
So I found a solution
try{
File file = new File (fileName);
AudioClip clip = Applet.newAudioClip(file.toURL());
clip.loop();
clip.stop();
}
catch(Exception e){
e.printStackTrace();
}
I can play a .wav file in a loop and stop it. That's what I wanted. And the code is very short.
My current project involves playing music, and I found a useful working code to play .wav sound files, but how can I speed the sound up, raise or lower the volume, and mute the sound. Is there commands or methods I have to use? Here is what I'm currently using.
import java.io.*;
import javax.sound.sampled.*;
try {
AudioInputStream stream;
AudioFormat format;
DataLine.Info info;
Clip clip;
File file1 = new File("C:\\Users\\Bryce\\Desktop\\DM Galaxy.wav");
stream = AudioSystem.getAudioInputStream(file1);
format = stream.getFormat();
info = new DataLine.Info(Clip.class, format);
clip = (Clip) AudioSystem.getLine(info);
clip.open(stream);
clip.start();
} catch (Exception e) {
e.printStackTrace();
}
This works great, but the only problem is, it plays it at full blast, and sounds very slow comapaired to if I just play the audio file. Any sugestions?
This question already has an answer here:
javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file when loading wav file
(1 answer)
Closed 4 years ago.
I'm trying to add sound to my java game...
I'm playing Sultans of swing at runtime:
static String WHOOSH = "res/WHOOSH.WAV";
static String SULTANS = "res/DireStraits_SultansOfSwing.wav";
music(SULTANS, true);
And this whoosh sound when the ball hits a paddle
music(WHOOSH, false);
public void music(String path, Boolean loop) {
try {
//will go into file folder and get music file (getResource)
AudioInputStream audio = AudioSystem.getAudioInputStream(GamePanel.class.getResource(path));
Clip clip = AudioSystem.getClip();
clip.open(audio);
clip.start();
if (loop) {
clip.loop(1000);
}
}
catch (Exception e) {
System.out.println("Check: " + path + "\n");
e.printStackTrace();
}
}
Problem:
The "Whoosh" always works, but Sultans of Swing does not. Sultans gives me this "Unsupported Audio File Exception" error, which oracle docs tells me
An UnsupportedAudioFileException is an exception indicating that an operation failed because a file did not contain valid data of a recognized file type and format.
Error:
Check: res/DireStraits_SultansOfSwing.wav
javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input URL at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
But you can see from these photos that they're both .wav files...
Why is it throwing that error? Is it a size issue?
Thanks!
When I've used wav files for a game, I've done something like this (I've updated it with your path):
public void endingSound() throws IOException{
ClassLoader cl = this.getClass().getClassLoader();
InputStream failSound = cl.getResourceAsStream("res/DireStraits_SultansOfSwing.wav");
if (failSound != null){
AudioStream as = new AudioStream(failSound);
AudioPlayer.player.start(as);
}
else{
System.err.println("cannot load ending sound");
}
}
In this way I assure you won't have any problems when you will export as jar. If is still doesn't work try to rename or replace that file; it may be corrupted as #MadProgrammer said.
I am creating a client server program where the client sends a string and as soon as server receives the string it plays a .wav file. Instead of playing the .wav file the server shows this error:
javax.sound.sampled.LineUnavailableException: line with format PCM_SIGNED 48000.0 Hz, 24 bit, stereo, 6 bytes/frame, little-endian not supported.
Here's the code:
try {
AudioInputStream audio = AudioSystem.getAudioInputStream(new File("N.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);
}
Your relative pathing is probably not where you expect it to be. Try using an absolute path for N.wav and see if it works. (i.e. /home/navjosh/Sounds/N.wav or C:\Users\navjosh\Documents\N.wav etc.
Just like the title. I'm wondering if it's possible to play a song in the background, and if so, how does the code go for that? I've googled endlessly to no avail. Any help would be appreciated.
Try using the Clip class:
http://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/Clip.html
I've used this code for many projects, and it always works
try {
// Open an audio input stream.
URL url = this.getClass().getClassLoader().getResource("path/fileName");
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
// Get a sound clip resource.
clip = AudioSystem.getClip();
// Open audio clip and load samples from the audio input stream.
clip.open(audioIn);
}catch(Exception e){
System.out.println(e);
}