Using Java, I'm trying to record sound from the default microphone and show the current volume and mute status (as set at OS level, not interested in checking bytes if possible). So far, I can get the TargetDataLine and record to it using the following code:
TargetDataLine line = (TargetDataLine) AudioSystem.getLine(new DataLine.Info(TargetDataLine.class, formato));
This works great on Windows, line being the default microphone selected using the OS.
Right now, to get volume/mute controls I have the following code:
Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo();
for (Mixer.Info mixerInfo : mixerInfos) {
Mixer mixer = AudioSystem.getMixer(mixerInfo);
if (mixer.getMaxLines(Port.Info.MICROPHONE) > 0) {
System.out.println("-------------");
System.out.println(mixerInfo);
Port line = (Port) mixer.getLine(Port.Info.MICROPHONE);
line.open();
Line.Info[] targets = mixer.getTargetLineInfo();
Control[] controls = line.getControls();
for (Control control : controls) {
if (control instanceof CompoundControl) {
Control[] subControls = ((CompoundControl) control).getMemberControls();
for (Control subControl : subControls) {
System.out.println(subControl);
}
} else {
System.out.println(control);
}
}
System.out.println("-----------");
}
}
There are 2 microphones in my setup and I haven't found a way of knowing which is the default one (right now it is the second one in the array).
Tried Line port = AudioSystem.getLine(Port.Info.MICROPHONE); and it is returning the first one available (and it is not the default in this instance).
So, any way to get the default microphone port that relates to the TargetDataLine obtained?
Thanks!
To get of all microphones installed in your system you can list them as follow :
package testing;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Line;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.TargetDataLine;
public class Main {
public static void main(String[] args) {
//Enumerates all available microphones
Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo();
for (Mixer.Info info: mixerInfos){
Mixer m = AudioSystem.getMixer(info);
Line.Info[] lineInfos = m.getTargetLineInfo();
if(lineInfos.length>=1 && lineInfos[0].getLineClass().equals(TargetDataLine.class)){//Only prints out info is it is a Microphone
System.out.println("Line Name: " + info.getName());//The name of the AudioDevice
System.out.println("Line Description: " + info.getDescription());//The type of audio device
for (Line.Info lineInfo:lineInfos){
System.out.println ("\t"+"---"+lineInfo);
Line line;
try {
line = m.getLine(lineInfo);
} catch (LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
System.out.println("\t-----"+line);
}
}
}
}
}
Starting from that you can identify the default one...
For instance in my MacBook the default one has this name "Built-in Microphone"
Related
I want to ask the repetitive question of how to record the audio send to the speakers. But I want some insights to the previously answered.
I went to this page: Capturing speaker output in Java
I saw this code posted by a developer:
import javax.sound.sampled.*;
import java.io.*;
public class JavaSoundRecorder {
// record duration, in milliseconds
static final long RECORD_TIME = 10000; // 1 minute
// path of the wav file
File wavFile = new File("E:/RecordAudio.wav");
// format of audio file
AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;
// the line from which audio data is captured
TargetDataLine line;
/**
* Defines an audio format
*/
AudioFormat getAudioFormat() {
float sampleRate = 16000;
int sampleSizeInBits = 8;
int channels = 1;
boolean signed = true;
boolean bigEndian = true;
AudioFormat format = new AudioFormat(sampleRate, sampleSizeInBits,
channels, signed, bigEndian);
return format;
}
/**
* Captures the sound and record into a WAV file
*/
void start() {
try {
AudioFormat format = getAudioFormat();
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
// checks if system supports the data line
if (!AudioSystem.isLineSupported(info)) {
System.out.println("Line not supported");
System.exit(0);
}
line = (TargetDataLine) AudioSystem.getLine(info);
line.open(format);
line.start(); // start capturing
System.out.println("Start capturing...");
AudioInputStream ais = new AudioInputStream(line);
System.out.println("Start recording...");
// start recording
AudioSystem.write(ais, fileType, wavFile);
} catch (LineUnavailableException ex) {
ex.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
/**
* Closes the target data line to finish capturing and recording
*/
void finish() {
line.stop();
line.close();
System.out.println("Finished");
}
/**
* Entry to run the program
*/
public static void main(String[] args) {
final JavaSoundRecorder recorder = new JavaSoundRecorder();
// creates a new thread that waits for a specified
// of time before stopping
Thread stopper = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(RECORD_TIME);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
recorder.finish();
}
});
stopper.start();
// start recording
recorder.start();
}
}
Now I have some questions I want to ask.
This code runs OK on my windows OS but it doesn't work on my Ubuntu on the same machine(dual boot). In Ubuntu it records silence and I tried to get all mixers but can't get it working
I want to get the output going to the speakers and I am getting the output of the speakers. The sound of the vicinity with a very little sound of what I actually want.
Please answer my queries of the above 2 questions.
What I want? I want the clear audio that is currently being played and fetched to the speakers of my laptop. I don't want the audio that is already emitted and then re-recorded because that is bad. Also I need a reason as of why my Ubuntu is not supporting this code.(This is vague info but I am using BlueJ in windows to run this and NetBeans on Ubuntu(without sudo)).
I saw some YouTube videos to understand the theory:
https://www.youtube.com/watch?v=GVtl19L9GxU
https://www.youtube.com/watch?v=PTs01qr9RlY
I read 1 and a half page documentation of oracle here: https://docs.oracle.com/javase/tutorial/sound/accessing.html
There was this thing mentioned in the docs:
An applet running with the applet security manager can play, but not record, audio.
An application running with no security manager can both play and record audio.
An application running with the default security manager can play, but not record, audio.
But I don't think I turned any security manager.
In the end I found no success in what I want to do. Instead of going further in the documentation I thought to ask the question here.
I receive bug to fix. Somebody write program which call printing document from other app. Jar with this program is on microsoft server for a few users. Problem is when one of user want print and other too in the same moment. Fox example user1 click print and have print preview, at the same moment user2 do the same action and he don't have print preview, because print prieview of document user2 is displayed on screen user1. I don't know where is bug. Maybe main app call the same instance of jar or the use the same process. Here is my code of method which is called when user click print.
public void openCreatedFile() {
String filePath = "file:///" + findNewlyCreatedFile().replace("\\", "/");
try {
if (xContext != null)
System.out.println("Opening file ...");
xMCF = xContext.getServiceManager();
Object oDesktop = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", xContext);
xCLoader = UnoRuntime.queryInterface(com.sun.star.frame.XComponentLoader.class, oDesktop);
com.sun.star.beans.PropertyValue[] szEmptyArgs = new com.sun.star.beans.PropertyValue[0];
xComp = xCLoader.loadComponentFromURL(filePath, "_blank", 0, szEmptyArgs);
System.out.println(filePath);
xDoc = UnoRuntime.queryInterface(com.sun.star.text.XTextDocument.class, xComp);
xContext = null;
} catch (Exception e) {
e.printStackTrace(System.err);
Thread.currentThread();
}
}
You must change your BootstrapSocketConnector. Here is solution:
for(int i = 8100; i<=65535 ; i++){
if(available(i)){
libre.xContext = BootstrapSocketConnector.bootstrap(libre.oooExeFolder, "localhost", i);
System.out.println(i);
break;
}
}
8100 is for example, here you must use your port which is using to connection.
And for example method available() where you check closest available port:
private static boolean available(int port) {
try{
Socket ignored = new Socket("localhost", port);
return false;
} catch (IOException ignored) {
return true;
}
}
I'm trying to get some values from rs232 (card reader Roger prt66lt) with a java simple serial connetor (jssc) but it look like i dont get any events started. I'm using the example code. The card reader is set to send card number when he reads it. But i dont get any event started coz i dont see event that System.out. I'm including jssc.jar (from 2.5.0 version) and its connecting to divice. Is this version of jssc doesnt need any .dll ? I think its included in it ? The similar code to this one but with RXTX library works fine but i need to include attahed dll library.
Card reader send me HEX value: 02, 10 chars DATA, 0D, 0A, 03
Code:
import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import jssc.SerialPortException;
public class Main {
static SerialPort serialPort;
public static void main(String[] args) {
serialPort = new SerialPort("COM4");
try {
serialPort.openPort();//Open port
serialPort.setParams(9600, 8, 1, 0);//Set params
int mask = SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR;//Prepare mask
serialPort.setEventsMask(mask);//Set mask
serialPort.addEventListener(new SerialPortReader());//Add SerialPortEventListener
}
catch (SerialPortException ex) {
System.out.println(ex);
}
}
static class SerialPortReader implements SerialPortEventListener {
public void serialEvent(SerialPortEvent event) {
System.out.println("Event started");
if(event.isRXCHAR()){//If data is available
if(event.getEventValue() == 10){//Check bytes count in the input buffer
//Read data, if 10 bytes available
try {
byte buffer[] = serialPort.readBytes(10);
}
catch (SerialPortException ex) {
System.out.println(ex);
}
}
}
else if(event.isCTS()){//If CTS line has changed state
if(event.getEventValue() == 1){//If line is ON
System.out.println("CTS - ON");
}
else {
System.out.println("CTS - OFF");
}
}
else if(event.isDSR()){///If DSR line has changed state
if(event.getEventValue() == 1){//If line is ON
System.out.println("DSR - ON");
}
else {
System.out.println("DSR - OFF");
}
}
}
}
}
The problem was that the default setting of RTS and DTR line is High so i need to use another setParams method and set the RTS line to false as needed by device.
Solution:
serialPort.setParams(9600, 8, 1, 0, false, true);
I have set mp3plugin.jar in the run time libraries needed in my netbeans project .But still i get the above exception when i try to play mp3 file.What is the reason for this :
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
public class tester_1 {
private static final int EXTERNAL_BUFFER_SIZE = 128000;
public static void main(String[] args)
{
/*
We check that there is exactely one command-line
argument.
If not, we display the usage message and exit.
*/
/*
Now, that we're shure there is an argument, we
take it as the filename of the soundfile
we want to play.
*/
String strFilename = "mp3tester.mp3";
File soundFile = new File("mp3tester.mp3");
/*
We have to read in the sound file.
*/
AudioInputStream audioInputStream = null;
try
{
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
}
catch (Exception e)
{
/*
In case of an exception, we dump the exception
including the stack trace to the console output.
Then, we exit the program.
*/
e.printStackTrace();
System.exit(1);
}
/*
From the AudioInputStream, i.e. from the sound file,
we fetch information about the format of the
audio data.
These information include the sampling frequency,
the number of
channels and the size of the samples.
These information
are needed to ask Java Sound for a suitable output line
for this audio file.
*/
AudioFormat audioFormat = audioInputStream.getFormat();
/*
Asking for a line is a rather tricky thing.
We have to construct an Info object that specifies
the desired properties for the line.
First, we have to say which kind of line we want. The
possibilities are: SourceDataLine (for playback), Clip
(for repeated playback) and TargetDataLine (for
recording).
Here, we want to do normal playback, so we ask for
a SourceDataLine.
Then, we have to pass an AudioFormat object, so that
the Line knows which format the data passed to it
will have.
Furthermore, we can give Java Sound a hint about how
big the internal buffer for the line should be. This
isn't used here, signaling that we
don't care about the exact size. Java Sound will use
some default value for the buffer size.
*/
SourceDataLine line = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class,
audioFormat);
try
{
line = (SourceDataLine) AudioSystem.getLine(info);
/*
The line is there, but it is not yet ready to
receive audio data. We have to open the line.
*/
line.open(audioFormat);
}
catch (LineUnavailableException e)
{
e.printStackTrace();
System.exit(1);
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
/*
Still not enough. The line now can receive data,
but will not pass them on to the audio output device
(which means to your sound card). This has to be
activated.
*/
line.start();
/*
Ok, finally the line is prepared. Now comes the real
job: we have to write data to the line. We do this
in a loop. First, we read data from the
AudioInputStream to a buffer. Then, we write from
this buffer to the Line. This is done until the end
of the file is reached, which is detected by a
return value of -1 from the read method of the
AudioInputStream.
*/
int nBytesRead = 0;
byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
while (nBytesRead != -1)
{
try
{
nBytesRead = audioInputStream.read(abData, 0, abData.length);
}
catch (IOException e)
{
e.printStackTrace();
}
if (nBytesRead >= 0)
{
int nBytesWritten = line.write(abData, 0, nBytesRead);
}
}
/*
Wait until all data are played.
This is only necessary because of the bug noted below.
(If we do not wait, we would interrupt the playback by
prematurely closing the line and exiting the VM.)
Thanks to Margie Fitch for bringing me on the right
path to this solution.
*/
line.drain();
/*
All data are played. We can close the shop.
*/
line.close();
/*
There is a bug in the jdk1.3/1.4.
It prevents correct termination of the VM.
So we have to exit ourselves.
*/
System.exit(0);
}
private static void printUsageAndExit()
{
out("tester_1: usage:");
out("\tjava tester_1 <soundfile>");
System.exit(1);
}
private static void out(String strMessage)
{
System.out.println(strMessage);
}
}
when i run this program i get the following exception :
java.lang.IllegalArgumentException: No line matching interface SourceDataLine supporting format MPEG1L3 44100.0 Hz, unknown bits per sample, stereo, unknown frame size, unknown frame rate, is supported.
at javax.sound.sampled.AudioSystem.getLine(AudioSystem.java:476)
at mp3tester_mp3plugin.tester_1.main(tester_1.java:178)
Java Result: 1
What is the reason i am getting this error ?
import java.io.File;
import javax.media.Format;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.media.PlugInManager;
import javax.media.format.AudioFormat;
public class AudioTest {
public static void main(String[] args) {
Format input1 = new AudioFormat(AudioFormat.MPEGLAYER3);
Format input2 = new AudioFormat(AudioFormat.MPEG);
Format output = new AudioFormat(AudioFormat.LINEAR);
PlugInManager.addPlugIn(
"com.sun.media.codec.audio.mp3.JavaDecoder",
new Format[]{input1, input2},
new Format[]{output},
PlugInManager.CODEC
);
try{
Player player = Manager.createPlayer(new MediaLocator(new File("mp3tester.mp3").toURI().toURL()));
player.start();
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
Courtsey of morgenstille.at.
Cheers,
Wim
PS Oracle 'hid' the JMF library here.
Edit: To actually answer your question: javax.sound has issues with playing 'some' mp3 formats, while javax.media and the mp3 plugin will have less (no?) issues.
It might be worthwile to check API's like JavaZoom or others for your mp3 needs.
I was getting the same error on Linux system. The workaround for me was to run it as root. Probably, I need to sort out some device permissions.
Using Java is it possible to capture the speaker output? This output is not being generated by my program but rather by other running applications. Can this be done with Java or will I need to resort to C/C++?
I had a Java based app. that used Java Sound to tap into the sound flowing through the system to make a trace of it. It worked well on my own (Windows based) machine, but failed completely on some others.
It was determined that in order to get it working on those machines, would take nothing short of an audio loop-back in either software or hardware (e.g. connect a lead from the speaker 'out' jack to the microphone 'in' jack).
Since all I really wanted to do was plot the trace for music, and I figured how to play the target format (MP3) in Java, it became unnecessary to pursue the other option further.
(And I also heard that Java Sound on Mac. was horribly broken, but I never looked closely into it.)
Java is not the best tool when dealing with the OS. If you need/want to use it for this task, probably you will end using Java Native Interface (JNI), linking to libraries compiled in other languages (probably c/c++).
Take an AUX cable, connect to HEADPHONE JACK and other end to MICROPHONE JACK and run this code
https://www.codejava.net/coding/capture-and-record-sound-into-wav-file-with-java-sound-api
import javax.sound.sampled.*;
import java.io.*;
public class JavaSoundRecorder {
// record duration, in milliseconds
static final long RECORD_TIME = 60000; // 1 minute
// path of the wav file
File wavFile = new File("E:/Test/RecordAudio.wav");
// format of audio file
AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;
// the line from which audio data is captured
TargetDataLine line;
/**
* Defines an audio format
*/
AudioFormat getAudioFormat() {
float sampleRate = 16000;
int sampleSizeInBits = 8;
int channels = 2;
boolean signed = true;
boolean bigEndian = true;
AudioFormat format = new AudioFormat(sampleRate, sampleSizeInBits,
channels, signed, bigEndian);
return format;
}
/**
* Captures the sound and record into a WAV file
*/
void start() {
try {
AudioFormat format = getAudioFormat();
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
// checks if system supports the data line
if (!AudioSystem.isLineSupported(info)) {
System.out.println("Line not supported");
System.exit(0);
}
line = (TargetDataLine) AudioSystem.getLine(info);
line.open(format);
line.start(); // start capturing
System.out.println("Start capturing...");
AudioInputStream ais = new AudioInputStream(line);
System.out.println("Start recording...");
// start recording
AudioSystem.write(ais, fileType, wavFile);
} catch (LineUnavailableException ex) {
ex.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
/**
* Closes the target data line to finish capturing and recording
*/
void finish() {
line.stop();
line.close();
System.out.println("Finished");
}
/**
* Entry to run the program
*/
public static void main(String[] args) {
final JavaSoundRecorder recorder = new JavaSoundRecorder();
// creates a new thread that waits for a specified
// of time before stopping
Thread stopper = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(RECORD_TIME);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
recorder.finish();
}
});
stopper.start();
// start recording
recorder.start();
}
}