B4J Question (SOLVED) Record what hear (output) from pc and send with MQTT...

Magma

Expert
Licensed User
Longtime User
Hi there...


Well, found the following code from stackoverflow... says that record everything "output at speakers" and send via socket...

Well i need to transform it someway different... record to bytes (chunk...buffer) and the send with MQTT.... and play at other MQTT client...

But I am not trying to touch it... because my JAVA knowledge is so little... :-(

Any help will make me happy!

B4X:
public class Server {
ServerSocket MyService;
Socket clientSocket = null;
InputStream input;
AudioFormat audioFormat;
SourceDataLine sourceDataLine;
byte tempBuffer[] = new byte[10000];
static Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();

Server() throws LineUnavailableException {

    try {
        Mixer mixer_ = AudioSystem.getMixer(mixerInfo[0]);
        audioFormat = getAudioFormat();
        DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat);
        sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
        sourceDataLine.open(audioFormat);
        sourceDataLine.start();
        MyService = new ServerSocket(500);
        clientSocket = MyService.accept();

        input = new BufferedInputStream(clientSocket.getInputStream());
        while (input.read(tempBuffer) != -1) {
            sourceDataLine.write(tempBuffer, 0, 10000);

        }

    } catch (IOException e) {

        e.printStackTrace();
    }

}

private AudioFormat getAudioFormat() {
    float sampleRate = 8000.0F;
    int sampleSizeInBits = 8;
    int channels = 1;
    boolean signed = true;
    boolean bigEndian = false;
    return new AudioFormat(
            sampleRate,
            sampleSizeInBits,
            channels,
            signed,
            bigEndian);
}

public static void main(String s[]) throws LineUnavailableException {
    Server s2 = new Server();
}}
 

kimstudio

Active Member
Licensed User
Longtime User
Hi Magma, I see the function of this code is to recieve data from a client socket and play it to speaker, nothing fancy.

In old days it is very hard to record everything "output at speakers", you need to write a Windows virtual soundcard driver to hijack audio streams to your real soundcard card to realize this function. Nowadays I don't know whether there are any new APIs supporting this. If you find some lights about this tell me...
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
Hi Magma, I see the function of this code is to recieve data from a client socket and play it to speaker, nothing fancy.

In old days it is very hard to record everything "output at speakers", you need to write a Windows virtual soundcard driver to hijack audio streams to your real soundcard card to realize this function. Nowadays I don't know whether there are any new APIs supporting this. If you find some lights about this tell me...
Sorry.. but this code not records everything output? ..hmmm
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
@kimstudio Found the way... will record STEREO-MIX...

coming with... more info... :)

Ofcourse i will share it here... +I hope use it at your apps too... (as I remember you are making cool stuff - like drum machine... fruit loop style) ...So you will use it for sure!
 
Upvote 0
Top