How can I play audio in java with command line interface? - java

I have been searching for how to play audio in java ( textpad ). There are plenty of examples but they use a GUI. I am using a command line interface. How do I play audio and use key event e.g spacebar to pause the audio and press spacebar again to replay the audio?

I do not know which examples you have been looking at but I do not think the GUI has anything to do with IF you can play sounds.
The following code is a simple sound class you can use in order to play audio in Java, you can read more about it at this Documentation.
import javax.sound.sampled.*;
import java.io.IOException;
import java.net.URL;
public class Sound {
private URL url;
private Clip clip;
/**
* #param requestedSound The requested type of sound
*/
public Sound(String requestedSound) {
url = this.getClass().getResource(requestedSound);
if (url != null) {
try {
// Open an audio input stream.
// Get a sound clip resource.
// Open audio clip and load samples from the audio input stream.
AudioInputStream audioInput = AudioSystem.getAudioInputStream(url);
clip = AudioSystem.getClip();
clip.open(audioInput);
} catch (UnsupportedAudioFileException | LineUnavailableException | IOException e) {
e.printStackTrace();
}
}
}
/**
* Plays the sound
*/
public void play() {
clip.setFramePosition(0);
clip.start();
}
}
To use it you simply create a sound in your main file, with Sound mySound = new Sound("path_to_sound"); Where you replace path_to_sound with your path. I believe one of the supported formats is .wav. Then you can just play the sound whenever you want to with mySound.play();, and whenever you do it will be played from the beginning.
Regarding your implementation of using spacebar to play / replay the audio, I believe it is better if you try to work with the given code in order to understand how it works.

Related

Playing Sound From .jar File

I have looked at countless different StackOverflow answers as well as answers from other sites, but none of the solutions have fixed my problem. I cannot for the life of me get my .wav file to play.
Here is my code:
Sound class:
public class Sound {
/**
* Static file paths for each sound.
*/
public static String stepSound = "/resources/step.wav";
/**
* Audio input stream for this sound.
*/
private AudioInputStream audioInputStream;
/**
* Audio clip for this sound.
*/
private Clip clip;
/* -- Constructor -- */
/**
* Creates a new sound at the specified file path.
*
* #param path File path to sound file
*/
public Sound(String path) {
// Get the audio from the file
try {
// Convert the file path string to a URL
URL sound = getClass().getResource(path);
System.out.println(sound);
// Get audio input stream from the file
audioInputStream = AudioSystem.getAudioInputStream(sound);
// Get clip resource
clip = AudioSystem.getClip();
// Open clip from audio input stream
clip.open(audioInputStream);
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
e.printStackTrace();
}
}
/* -- Method -- */
/**
* Play the sound.
*/
public void play() {
// Stop clip if it's already running
if (clip.isRunning())
stop();
// Rewind clip to beginning
clip.setFramePosition(0);
// Play clip
clip.start();
}
/**
* Stop the sound.
*/
public void stop() {
clip.stop();
}
}
Constructor call that leads to error:
// Play step sound
new Sound(Sound.stepSound).play();
I know this isn't the first time a problem like this has been asked or answered on this website, but I've been trying other solutions for hours at this point and all I've found is pain and frustration. I can post more code if needed. Thanks in advance.
EDIT: I have unpacked the .jar file and confirmed that the file is indeed there. The problem is that the URL ends up being null, and so a NullPointerException is thrown.
EDIT #2: Added more code in case there's another problem.

run mp3 and .aac vlc audio in java

I would like to run audio file from java and i read many codes in SO but unbale to run my file perhaps!
Seems I have mentioned wrong path or using wrong lib .
Please assist me what's wrong in below code to run mp3 or VLC .aac format file
public void playSound() {
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("D:/clinic/clinic/mysound.mp3").getAbsoluteFile());
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
} catch(Exception ex) {
System.out.println("Error with playing sound.");
ex.printStackTrace();
}
}
If you use the this.getClass.getResource() method instead of the File(file) method, maybe it would work. Remember that the file that the audio is in has to be in the same package as the class that is running it. If this doesn't work, then try it with a .wav file(you can use a .mp3 to .wav converter).
public void run() {
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(this.getClass().getResource("mysound.mp3"));
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
clip.loop(Clip.LOOP_CONTINUOUSLY);
} catch (Exception ex) {
ex.printStackTrace();
}
}
I hope this helps.
AudioSystem does not support .mp3 files. (Only AIFC, AIFF, AU, SND, and WAVE)
If you want to use .mp3 files, try using MediaPlayer instead.
// Fake init of JFX Toolkit (Just do this once before you use MediaPlayer)
// Not needed in a JavaFX application as Application.launch() inits the toolkit
new JFXPanel();
Media media = new Media(new File("yourFile.mp3").toURI().toString());
MediaPlayer player = new MediaPlayer(media);
player.play();
The audioformats supported natively are nit that useful if you don't want to have huge audiofiles.
I ended using WAV files as it was what I could get to work, but it bothered me all the time.
Using jaad was trickier than I thought, but I got it working now: Java play AAC encoded audio

Audio not playing with JAR File

I created a project that plays audio within the netbeans IDE. Those audio files were placed in the Classes folder.
Although when I created it as a JAR file, it was unable to locate the audio files. I even copy and pasted the files inside the new dist folder.
Here is a snippet of code:
private void playSound39()
{
try
{
/**Sound player code from:
http://alvinalexander.com/java/java-audio-example-java-au-play-sound
*/
// the input stream portion of this recipe comes from a javaworld.com article.
InputStream inputStream = getClass().getResourceAsStream("./beep39.wav");
AudioStream audioStream = new AudioStream(inputStream);
AudioPlayer.player.start(audioStream);
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null,"Audio file not found!");
}
}
If you want to embedd the audio file in your program it's must be placed inside the src folder in a package.
For example I'll demonstrate a code I use to set icons to buttons (should work for audio files as well) :
While creating the JFrame I wrote :
jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Icon/PatientBig.png")));
I have in my project a package called GUI with a subpackage called Icons where my icons exist and they all are in src folder.
When you using getClass().getResource function , I prefer to use an absolute path.
After seeing your respone I have noticed that you keep using . in the begining of the class path, I copied the snippet you published and removed the . from the begining of the path and placed my audio file bark.wav in the src folder in the default package and it worked
public class test {
private void playSound39() {
try {
/**
* Sound player code from:
* http://alvinalexander.com/java/java-audio-example-java-au-play-sound
*/
// the input stream portion of this recipe comes from a javaworld.com article.
InputStream inputStream = getClass().getResourceAsStream("/bark.wav");
AudioStream audioStream = new AudioStream(inputStream);
AudioPlayer.player.start(audioStream);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Audio file not found!");
}
}
public static void main(String[] args){
new test().playSound39();
}
}
Then I placed the audio file inside a package called test1 and modified the path in getResourceAsStream function and again it worked:
public class test {
private void playSound39() {
try {
/**
* Sound player code from:
* http://alvinalexander.com/java/java-audio-example-java-au-play-sound
*/
// the input stream portion of this recipe comes from a javaworld.com article.
InputStream inputStream = getClass().getResourceAsStream("/test1/bark.wav");
AudioStream audioStream = new AudioStream(inputStream);
AudioPlayer.player.start(audioStream);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Audio file not found!");
}
}
public static void main(String[] args){
new test().playSound39();
}
}
The Most important thing is to remove . from the path
try this
InputStream in = getClass().getResourceAsStream("/beep39.wav");
I think you need to bypass use of the InputStream. When running the getAudioInputStream method, using InputStream as a parameter triggers markability and resetability tests on the audio file. Audio files usually fail these tests. If you create your AudioInputStream with a URL or File parameter, these tests are circumvented. I prefer URL as it seems more robust and can "see into" jars.
URL url = getClass().getResource("./beep39.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
Then, in a while loop, you would execute a read method on the AudioInputStream and send the data to a SourceDataLine.
The Java Tutorials covers this in their audio trail. This link jumps into the middle of the tutorials.
AFAIK, there is no "AudioPlayer" in the Java 7 SDK.

Computer can't find 'AudioStream' when wanting to play background music for an app

So what I have tried to do is make my application play music in the background with a .wav music file.
I have this code but AudioStream can't be found under
sun.audio.*;
If any of you have worked with Eclipse IDE, how would you be able to find AudioStream to import it...
Here's my code which this uses. It's under the Sound class which doesn't implement or extend anything.
private AudioStream as;
private String lastSoundPath;
private void setStream(String soundPath){
this.lastSoundPath = soundPath;
try {
InputStream in = new FileInputStream(soundPath);
this.as = new AudioStream(in);
} catch (Exception e) {
e.printStackTrace();
}
}
Here's the error I get when trying to play Bangarang (Random I know...)
java.io.IOException: could not create audio stream from input stream
at sun.audio.AudioStream.<init>(AudioStream.java:82)
at vapour.studios.destiny.client.Sound.setStream(Sound.java:17)
at vapour.studios.destiny.client.Sound.<init>(Sound.java:24)
at vapour.studios.destiny.Destiny.main(Destiny.java:23)
Thanks in advance.
You need to use javaSE 1.7 as execution environment in project properties.
it worked for me on Mac OS 10.8

How can I intercept the audio stream on an android device?

Let's suppose that we have the following scenario: something is playing on an android device (an mp3 par example, but it could be anything that use the audio part of an android device). From an application (android application :) ), I would like to intercept the audio stream to analyze it, to record it, etc. From this application (let's say "the analyzer") I don't want to start an mp3 or something, all I want is to have access to the audio stream of android.
Any advice is appreciated, it could a Java or C++ solution.
http://developer.android.com/reference/android/media/MediaRecorder.html
public class AudioRecorder {
final MediaRecorder recorder = new MediaRecorder();
final String path;
/**
* Creates a new audio recording at the given path (relative to root of SD
* card).
*/
public AudioRecorder(String path) {
this.path = sanitizePath(path);
}
private String sanitizePath(String path) {
if (!path.startsWith("/")) {
path = "/" + path;
}
if (!path.contains(".")) {
path += ".3gp";
}
return Environment.getExternalStorageDirectory().getAbsolutePath()
+ path;
}
/**
* Starts a new recording.
*/
public void start() throws IOException {
String state = android.os.Environment.getExternalStorageState();
if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
throw new IOException("SD Card is not mounted. It is " + state
+ ".");
}
// make sure the directory we plan to store the recording in exists
File directory = new File(path).getParentFile();
if (!directory.exists() && !directory.mkdirs()) {
throw new IOException("Path to file could not be created.");
}
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);
recorder.prepare();
recorder.start();
}
/**
* Stops a recording that has been previously started.
*/
public void stop() throws IOException {
recorder.stop();
recorder.release();
}
}
Consider using the AudioPlaybackCapture API that was introduced in Android 10 if you want to get the audio stream for a particular app.

Categories