0

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.

  1. 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

  2. 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:

  1. https://www.youtube.com/watch?v=GVtl19L9GxU
  2. 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.

Shobhit Tewari
  • 535
  • 5
  • 20
  • I assume you also googled for things like "record system audio with java in linux"? I found https://www.codejava.net/coding/capture-and-record-sound-into-wav-file-with-java-sound-api and haven't tried it, but then this isn't my problem - you should give that a go and see if it works. If it does: hurray, this question can be deleted. If it doesn't: hurray, now you have another chunk of code to show in your post that is known to not work, so folks know what _not_ to recommend you try. – Mike 'Pomax' Kamermans Mar 14 '20 at 19:03
  • 1
    Spoken as someone who _didn't check that link and tried the code that showed_. You're not posting to Stackoverflow for yourself, you're posting here for yourself *and everyone else with this same problem in the future*, so put in the effort to solve the problem when someone gives you new information instead of saying "you are of no help". You get to try what people suggest, and update your post accordingly based on whether or not that works, for yourself and all future visitors. Which you were before you decided to post here, too. It's how SO stays useful to the entire world. – Mike 'Pomax' Kamermans Mar 14 '20 at 19:42
  • I have tried this code. So shall I put it on the post above? Shall I post everything above? I posted all the information because someone suggested me to first try to use most of the available resources before asking question and mention things I have tried to tell other developers what might be the exact problem. I think I followed the guidelines. Good Sir. – Shobhit Tewari Mar 14 '20 at 20:01
  • Yes, you should. You should say "I tried this (SO link) and this (link to that article)" and then explain why neither worked for you, as part of making sure you've got a [well written question](/help/how-to-ask). Explicitly saying what you already tried means people won't suggest you do that again before they offer more help. – Mike 'Pomax' Kamermans Mar 14 '20 at 20:02

0 Answers0