How can I make a Java application with audio games enjoyable on Linux?

I have a Java application whose interface is highly dependent on sound. On Windows and OS X, everything works fine; on Linux, however, the application requires exclusive access to the sound device, it drops LineUnavailableExceptionand the sound is not heard. I am using Kubuntu 9.10.

This means that no other application can play sound while the program is running and cannot even hold the audio device when the program starts. This is naturally unacceptable.

Here is the code I use to play audio:

AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);

Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);

clip.start();

this.wait((clip.getMicrosecondLength() / 1000) + 100);

clip.stop();

Am I doing something wrong? Is using Java to play audio on Linux a lost cause?

+3
5

, Linux - . Java Bug. , . , Ubuntu PulseAudio/ALSA, Kubuntu.

( ).

, , , , (, , ).

+5

GNU/Linux (Ubuntu 10.10) OpenJDK . , LineUnavailableException ​​PulseAudio 10.10.

(- Windows).

AudioInputStream audioIn = AudioSystem.getAudioInputStream(in);

// needed for working on GNU/Linux (openjdk) {
AudioFormat format = audioIn.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
Clip clip = (Clip)AudioSystem.getLine(info);
// }
// on windows, {
//Clip clip = AudioSystem.getClip();
// }

, Clip.getMicrosecondLength() .

+2

Java Sound Linux. , . Java Sound OpenAL, Linux. FMOD, , , .

, , PortAudio. SIP Communicator.

RtAudio, , ALSA.

+1

I got this code somewhere on the Internet, the sound appears most often, sometimes it does not appear

import java.util.*;
import java.text.*;
import java.io.*;

import java.net.*;
import javax.sound.sampled.*;
public class Sound2
{
    public static
    void main (String name[])
    {
        playSound ( "somesound.wav" );
    }
    public static
    void playSound (String filename)
    {

        int BUFFER_SIZE = 128000;
         //File soundFile = null;
         AudioInputStream audioStream = null;
         AudioFormat audioFormat = null;
         SourceDataLine sourceLine = null;
        try 
        {
            audioStream = 
            AudioSystem.getAudioInputStream
            (
                new
                BufferedInputStream 
                (
                    new FileInputStream ( filename )
                )
            //soundFileStream
            );
        } 
        catch (Exception e)
        {
            e.printStackTrace();
            System.exit(1);
        }

        audioFormat = audioStream.getFormat();

        DataLine.Info info = new DataLine.Info
        (
            SourceDataLine.class, 
            audioFormat
        );
        try 
        {
            sourceLine = (SourceDataLine) AudioSystem.getLine(info);
            sourceLine.open(audioFormat);
        } 
        catch (LineUnavailableException e) 
        {
            e.printStackTrace();
            System.exit(1);
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
            System.exit(1);
        }

        sourceLine.start();

        int nBytesRead = 0;
        byte[] abData = new byte[BUFFER_SIZE];
        while (nBytesRead != -1) 
        {
            try 
            {
                nBytesRead = 
                audioStream.read(abData, 0, abData.length);
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }
            if (nBytesRead >= 0) 
            {
                @SuppressWarnings("unused")
                int nBytesWritten = 
                sourceLine.write(abData, 0, nBytesRead);
            }
        }

        sourceLine.drain();
        sourceLine.close();
    }
}
0
source

Send the mplayer command through the shell. The simplest solution.

0
source

Source: https://habr.com/ru/post/1726264/


All Articles