".getAudioInputStream(file)" give the error "cannot resolve symbol" (IDE Intellij, java 8)
I try the solution in File > Invalidate Chaces / Restart ... but it doesn't work
package com.Main;
import javax.sound.sampled.Clip;
import java.io.File;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
public class SoundEffect {
private String filepath;
private Clip clip;
public SoundEffect(String filepath) {
this.filepath = filepath;
try {
File file = new File(filepath);
AudioInputStream sound = new AudioSystem.getAudioInputStream(file);
clip = AudioSystem.getClip();
clip.open(sound);
}
catch (Exception e) { e.printStackTrace(); }
}
public void play() {
clip.start();
}
}
AudioInputStream sound = new AudioSystem.getAudioInputStream(file);
You don't want to create an instance of an object. You want to invoke a static method of a class.
You don't need the "new".
The code should be:
//AudioInputStream sound = new AudioSystem.getAudioInputStream(file);
AudioInputStream sound = AudioSystem.getAudioInputStream(file);
Related
I tried to make a runnable JAR, but for some reason I couldn't get my game to play. I did some research and ran it through my command prompt to try to find the error and I got this below. So obviously I know the issue I just need to fix it. I have the audio file in my res folder which is in my src. So if it is already in the program I can't figure out why I would get this error. Thoughts?
Exception in thread "main" java.lang.NullPointerException
at java.base/java.util.Objects.requireNonNull(Unknown Source)
at java.desktop/javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at builder.AudioPlayer.playMenuSound(AudioPlayer.java:20)
at builder.Game.<init>(Game.java:56)
at builder.Game.main(Game.java:61)
package builder;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
public class AudioPlayer {
private static Clip play;
public static void playMenuSound()
{
try {
//AudioInputStream menuSound = AudioSystem.getAudioInputStream(new File("src/res/introSong.wav")); //Take in audio from res folder
AudioInputStream menuSound = AudioSystem.getAudioInputStream(AudioPlayer.class.getClassLoader().getResourceAsStream("introSong.wav"));
play = AudioSystem.getClip(); //
play.open(menuSound); //Play the sound
FloatControl volume = (FloatControl) play.getControl(FloatControl.Type.MASTER_GAIN); //Get control of volume
volume.setValue(1.0f); //0.0 - 1.0 volume
play.loop(Clip.LOOP_CONTINUOUSLY); //Loop once clip is over
}catch (LineUnavailableException | IOException | UnsupportedAudioFileException e){
e.printStackTrace();
}
}
public static void playGameSound()
{
try {
//AudioInputStream gameSound = AudioSystem.getAudioInputStream(new File("src/res/inGame.wav")); //Take in audio from res folder
AudioInputStream gameSound = AudioSystem.getAudioInputStream(AudioPlayer.class.getClassLoader().getResourceAsStream("inGame.wav"));
play = AudioSystem.getClip(); //
play.open(gameSound); //Play the sound
FloatControl volume = (FloatControl) play.getControl(FloatControl.Type.MASTER_GAIN); //Get control of volume
volume.setValue(0.5f); //0.0 - 1.0 volume
play.loop(Clip.LOOP_CONTINUOUSLY); //Loop once clip is over
}catch (LineUnavailableException | IOException | UnsupportedAudioFileException e){
e.printStackTrace();
}
}
public static void stopMusic()
{
play.close(); //Stop music
}
}
The problem in your code is where you're trying to instantiate your file with a new reference, here:
AudioInputStream gameSound = AudioSystem.getAudioInputStream(new File("src/res/inGame.wav"));
Instead of doing that, you need to get your file as a resource using a ClassLoader, since it is located inside the resource (res) folder.
Here is how your code should look like:
AudioInputStream gameSound = AudioSystem.getAudioInputStream(AudioPlayer.class.getClassLoader().getResourceAsStream("inGame.wav"));
Same should be done in with your introSong.wav in the playMenuSound() method. The code should look something like
AudioInputStream gameSound = AudioSystem.getAudioInputStream(AudioPlayer.class.getClassLoader().getResourceAsStream("introSong.wav"));
I really hope this solves your problem.
I have had success with the following form for setting up Clips. Perhaps it will work for you.
URL url = this.getClass().getResource("audio/" + filename);
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
DataLine.Info info = new DataLine.Info(Clip.class, ais.getFormat());
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(ais);
The method getResource returns a URL. The method getResourceAsStream returns an InputStream. A URL works as a way to address a file within a jar.
In this example, the file in which the code resides is a parent to the folder "/audio" and the audio resource is in the "/audio" folder.
The following form works for me if you desire to call the loader as a static method.
URL url = AudioPlayer.class.getResource("audio/" + filename);
package soundTest;
import java.applet.*;
import java.net.*;
import javax.swing.*;
import javax.sound.sampled.*;
import java.io.*;
import java.util.*;
public class SoundTest {
public static void main(String[] args) throws Exception {
try {
AudioClip clip1 = Applet.newAudioClip(new URL(new File("E0.wav").getAbsolutePath()));
clip1.play();
} catch (MalformedURLException murle) {
System.out.println(murle);
}
URL url = new URL(
"http://www.mediafire.com/listen/tok9j9s1hnogj1y/downloads/E0.wav");
Clip clip = AudioSystem.getClip();
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
clip.open(ais);
URL url2 = new URL(
"http://www.villagegeek.com/downloads/webwavs/Austin_Powers_death.wav");
Clip clip2 = AudioSystem.getClip();
AudioInputStream ais2 = AudioSystem.
getAudioInputStream(url2);
clip2.open(ais2);
clip.loop(1);
clip2.loop(Clip.LOOP_CONTINUOUSLY);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(null, "Close to exit!");
}
});
}
}
I can't figure out how to play a wav file from my computer (not from a URL) in java. I'm sure that I have it placed in the right area, the SRC (I also placed it in practically every other space just in case...).
The first attempt is from http://www.cs.cmu.edu/~illah/CLASSDOCS/javasound.pdf
It gives the me the catch statement.
The second attempt was putting my recorded .wav file on mediafire. However, that didn't work. "Exception in thread "main" javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input URL"
The third example works fine, but, unlike my file, it's a file from online. When you click on that one, it brings you to just an audio player, while the mediafire link brings you to a page with other stuff and some application that plays the file.
First Attempt
AudioClip clip1 = Applet.newAudioClip(new URL(new File("E0.wav").getAbsolutePath()));
This is not how you construct a URL to a File. Instead, you should use File#getURI#getURL
AudioClip clip1 = Applet.newAudioClip(new File("/full/path/to/audio.wav").toURI().toURL());
Second Attempt
mediafire is returning a html response, not the audio file...You can test it with...
URL url = new URL("http://www.mediafire.com/listen/tok9j9s1hnogj1y/downloads/E0.wav");
try (InputStream is = url.openStream()) {
int in = -1;
while ((in = is.read()) != -1) {
System.out.print((char)in);
}
} catch (IOException exp) {
exp.printStackTrace();
}
Third Attempt
You open the clip, but never start it...
URL url2 = new URL("http://www.villagegeek.com/downloads/webwavs/Austin_Powers_death.wav");
Clip clip2 = AudioSystem.getClip();
AudioInputStream ais2 = AudioSystem.getAudioInputStream(url2);
clip2.open(ais2);
clip2.start();
The audio file that I am using is found here: http://www.orangefreesounds.com/loud-alarm-clock-sound/
This is what my file structure looks like in my Eclipse IDE:
The audio file plays perfectly fine when I run it in my IDE, but not when I export it as a JAR file. I have already checked and found that the audio file is inside the JAR file.
I am using the terminal command java -jar Sandbox.jar & to run the JAR file. The program seems to be able to find the file (since it is not throwing an IOException), but does not seem to be able to perform playback.
Why is this problem happening and how can I fix it?
Weird Update
Okay, so actually, the JAR file is able to play the audio file when run in cmd or PowerShell on Windows 8.1, but not in the terminal of Ubuntu 14.04 for some reason. This whole time, I have been trying to run the JAR file in Ubuntu 14.04.
Weird Update #2
I have confirmed the issue of the JAR files only working on a Windows 8.1 system. Both of the code snippets in this question DO NOT WORK, while both of MadProgrammer's solutions work.
Minimal, Complete, and Verifiable example (does NOT work on Windows or Ubuntu)
import java.io.IOException;
import java.net.URL;
import javax.sound.sampled.*;
public class Sandbox
{
public static void main(String[] args) throws UnsupportedAudioFileException, IOException, LineUnavailableException
{
URL url = Sandbox.class.getResource("/sound-effects/alarmSoundClip.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
AudioFormat af = ais.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, af);
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(ais);
clip.start();
}
}
Attempted Solution #1 (does NOT work on Windows or Ubuntu)
One attempted solution (as suggested by Andrew Thompson) was to write this.getClass().getResource( ... ) instead of Sandbox.class.getResource( ... ):
import java.io.IOException;
import java.net.URL;
import javax.sound.sampled.*;
public class Sandbox
{
public static void main(String[] args) throws UnsupportedAudioFileException, IOException, LineUnavailableException
{
new Sandbox();
}
public Sandbox() throws UnsupportedAudioFileException, IOException, LineUnavailableException
{
URL url = this.getClass().getResource("/sound-effects/alarmSoundClip.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
AudioFormat af = ais.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, af);
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(ais);
clip.start();
}
}
Adding clip.drain() after clip.start() seems to have worked okay for me (IDE and command line both with and without &)
import java.io.IOException;
import java.net.URL;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
public class Sandbox {
public static void main(String[] args) {
try {
URL url = Sandbox.class.getResource("/sound-effects/Loud-alarm-clock-sound.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
AudioFormat af = ais.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, af);
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(ais);
clip.start();
System.out.println("Drain...");
clip.drain();
System.out.println("...Drained");
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException exp) {
exp.printStackTrace();
}
}
}
Now, having said that, I have found drain a little unreliable in the past, especially when there are multiple sounds playing in which case I tend to use a LineListener
For example...
import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
public class Sandbox {
protected static final Object LOCK = new Object();
public static void main(String[] args) {
try {
URL url = Sandbox.class.getResource("/sound-effects/Loud-alarm-clock-sound.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
AudioFormat af = ais.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, af);
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(ais);
clip.addLineListener(new LineListener() {
#Override
public void update(LineEvent event) {
System.out.println(event.getType());
if (event.getType() == LineEvent.Type.STOP) {
synchronized (LOCK) {
LOCK.notify();
}
}
}
});
clip.start();
synchronized (LOCK) {
LOCK.wait();
}
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException | InterruptedException exp) {
exp.printStackTrace();
}
}
}
I am trying to change the output audio levels of the program, preferably in decibels. I need to change the audio levels of the entire program and record the change in the level. The language is Java. Is there any easy way to do this? The sounds I am using to play the sounds is below:
import java.io.InputStream;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
public class Sound
{
String sounds;
public Sound(String file)
{
sounds = file;
playSound(sounds);
}//end contructor
public void playSound(String soundLoc)
{
try
{
InputStream inputStream = getClass().getResourceAsStream(soundLoc);
AudioStream audioStream = new AudioStream(inputStream);
AudioPlayer.player.start(audioStream);
}//end try
catch (Exception e)
{
}//end catch
}//end playSound method
}//end class Sound
You can use MASTER_GAIN_CONTROL using the Java sound API
you need to import this.... import javax.sound.sampled.*;
get your clip using a Clip object and then,
public void playSound(String soundLoc)
{
try
{
InputStream inputStream = getClass().getResourceAsStream(soundLoc);
AudioStream audioStream = new AudioStream(inputStream);
AudioPlayer.player.start(audioStream);
Clip myclip = AudioSystem.getClip();
myclip.open(audioStream);
FloatControl audioControl = (FloatControl) myclip.getControl(FloatControl.Type.MASTER_GAIN);
audioControl.setValue(-5.0f); //decrease volume 5 decibels
clip.start();
}//end try
catch (Exception e)
{
}//end catch
}//end playSound method
package soundTest;
import java.applet.*;
import java.net.*;
import javax.swing.*;
import javax.sound.sampled.*;
import java.io.*;
import java.util.*;
public class SoundTest {
public static void main(String[] args) throws Exception {
try {
AudioClip clip1 = Applet.newAudioClip(new URL(new File("E0.wav").getAbsolutePath()));
clip1.play();
} catch (MalformedURLException murle) {
System.out.println(murle);
}
URL url = new URL(
"http://www.mediafire.com/listen/tok9j9s1hnogj1y/downloads/E0.wav");
Clip clip = AudioSystem.getClip();
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
clip.open(ais);
URL url2 = new URL(
"http://www.villagegeek.com/downloads/webwavs/Austin_Powers_death.wav");
Clip clip2 = AudioSystem.getClip();
AudioInputStream ais2 = AudioSystem.
getAudioInputStream(url2);
clip2.open(ais2);
clip.loop(1);
clip2.loop(Clip.LOOP_CONTINUOUSLY);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(null, "Close to exit!");
}
});
}
}
I can't figure out how to play a wav file from my computer (not from a URL) in java. I'm sure that I have it placed in the right area, the SRC (I also placed it in practically every other space just in case...).
The first attempt is from http://www.cs.cmu.edu/~illah/CLASSDOCS/javasound.pdf
It gives the me the catch statement.
The second attempt was putting my recorded .wav file on mediafire. However, that didn't work. "Exception in thread "main" javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input URL"
The third example works fine, but, unlike my file, it's a file from online. When you click on that one, it brings you to just an audio player, while the mediafire link brings you to a page with other stuff and some application that plays the file.
First Attempt
AudioClip clip1 = Applet.newAudioClip(new URL(new File("E0.wav").getAbsolutePath()));
This is not how you construct a URL to a File. Instead, you should use File#getURI#getURL
AudioClip clip1 = Applet.newAudioClip(new File("/full/path/to/audio.wav").toURI().toURL());
Second Attempt
mediafire is returning a html response, not the audio file...You can test it with...
URL url = new URL("http://www.mediafire.com/listen/tok9j9s1hnogj1y/downloads/E0.wav");
try (InputStream is = url.openStream()) {
int in = -1;
while ((in = is.read()) != -1) {
System.out.print((char)in);
}
} catch (IOException exp) {
exp.printStackTrace();
}
Third Attempt
You open the clip, but never start it...
URL url2 = new URL("http://www.villagegeek.com/downloads/webwavs/Austin_Powers_death.wav");
Clip clip2 = AudioSystem.getClip();
AudioInputStream ais2 = AudioSystem.getAudioInputStream(url2);
clip2.open(ais2);
clip2.start();