Trying to play audio in AnotherTerm

40 Views Asked by At

I'm exploring Java with AnotherTerm, using AnLinux Debian rootfs and a script to install IceWM for VNC server usage in proot. I'm encountering difficulty playing audio within the proot and IceWM. Any guidance would be greatly appreciated.

I tried...

import javax.sound.sampled.*;

public class AudioTest {

    public static void main(String[] args) {
        try {
            // Set the parameters for the audio format
            AudioFormat audioFormat = new AudioFormat(44100, 16, 1, true, false);

            // Get a SourceDataLine that matches the specified audio format
            DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
            SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);

            // Open the SourceDataLine and start it
            line.open(audioFormat);
            line.start();

            // Generate a simple sine wave and write it to the audio output
            int bufferSize = 1024;
            byte[] buffer = new byte[bufferSize];

            for (int i = 0; i < 1000; i++) {
                double angle = i / (audioFormat.getSampleRate() / 440.0) * 2.0 * Math.PI;
                short sample = (short) (Short.MAX_VALUE * Math.sin(angle));
                buffer[0] = (byte) (sample & 0xFF);
                buffer[1] = (byte) ((sample >> 8) & 0xFF);
                line.write(buffer, 0, 2);
            }

            // Close the line when finished
            line.drain();
            line.close();

        } catch (LineUnavailableException e) {
            e.printStackTrace();
        }
    }
}

Exception in thread "main" java.lang.IllegalArgumentException: No line matching interface SourceDataLine supporting format PCM_SIGNED 44100.0 Hz, 16 bit, mono, 2 bytes/frame, little-endian is supported.
        at java.desktop/javax.sound.sampled.AudioSystem.getLine(AudioSystem.java:425)
        at AudioTest.main(AudioTest.java:12)

Also...

$ paplay $AUDIO_FILE_PATH
Failed to open audio file.

$ java --version
openjdk 11.0.21 2023-10-17
OpenJDK Runtime Environment (build 11.0.21+9-post-Debian-1deb11u1)
OpenJDK Server VM (build 11.0.21+9-post-Debian-1deb11u1, mixed mode)

$ pulseaudio --version
pulseaudio 14.2

Android 10 armeabi-v7a Not Rooted

1

There are 1 best solutions below

1
Cyrus Justine On

Discovered a solution for pulseaudio: the PulseAudio server is capable of streaming over TCP/UDP using various protocols.

How to Play Sound from Termux when using Linux?

Audio in graphical session

Simple Protocol Player

However, I'm perplexed about the recurring exception I encounter when playing an audio file in Java.