Jump to content
Excelsior Forums
milan

Sound is not working after building with Excelsior Jet-linux

Recommended Posts

Hello,

I am having a problem with sound after building application with Excelsior JET, Standard Edition for Linux 7.6.

All is working excellent from eclipse or from executable jar, but after building with Jet sound is not being reproduced.

I see no error message when running from console and paths are static and OK, i have been testing on debian and mint.

Btw. other stuff is working perfect java2d (rendering even faster than from .jar), sockets, mysql and librxtx.

Here is the code i use to play audio files:

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;

public class audio {

public audio(String dje){
	try{

        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(""+dje));
       	AudioFormat format = audioInputStream.getFormat();
      	        DataLine.Info info = new DataLine.Info(Clip.class, format);
	Clip clip = (Clip)AudioSystem.getLine(info);
           	clip.open(audioInputStream);

   	//FloatControl gain = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
   	//gain.setValue(-10.0f);

	clip.start();
	clip.drain();
	clip.close();

    }
    catch (Exception exc)
    {
        exc.printStackTrace(System.out);
    }
}
}

Hope someone can help me around this issue, i am stuck in a loop.

Share this post


Link to post
Share on other sites

Hello,

Could you please tell us about your

1) JVM version

2) Linux distribution

?

Your sample seems not to work on my Linux system with Oracle JVM 1.6.0_33

Share this post


Link to post
Share on other sites

Machine i used for developing and building is running linux Mint-14 (Nadia), edition MATE 32-BIT and openjdk-7-jdk.

Test machine is Debian server with Xserver installed running swing app from console, running openjdk-6-jre.

Today i setup Debian latest version with gnome desktop 32-bit and installed Java SE Runtime Environment (build 1.6.0_39-b04)

You are correct code does not play audio on debian using oracle's JVM.

Same behavior as when compiled with excelsior no exceptions and no sound reproduction.

Since excelsior uses oracle's JVM then i think it is normal to behave in the same way.

So any idea what can i do now i think my existing code for audio will not work with Excelsior.

I really need to be able to play wav(or other format) short clips.

Any idea is more then welcome.

Share this post


Link to post
Share on other sites

it' working ;)


public class audio extends Thread {

   private final int BUFFER_SIZE = 128000;
   private File soundFile;
   private AudioInputStream audioStream;
   private AudioFormat audioFormat;
   private  SourceDataLine sourceLine;
   String strFilename =""; 

   public static void main(String[] argumenti){
   	 new audio("/sound/en/23.wav");	
   	//s.run();
   }

   public audio(String dje){
   	strFilename   = dje;
   this.start();
   }

@Override
public void run() {
	  try {
            soundFile = new File(strFilename);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            audioStream = AudioSystem.getAudioInputStream(soundFile);
        } catch (Exception e){
            e.printStackTrace();
        }

        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();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // Volume control gain value is in db positive or negative <make param>
        FloatControl gain = (FloatControl) sourceLine.getControl(FloatControl.Type.MASTER_GAIN);
           gain.setValue(-30.0f);

        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();
}
}

Share this post


Link to post
Share on other sites

You can also try the following solution:

   public audio(String audioFile) {
       try
       {
           AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(audioFile));

           DataLine.Info info = new DataLine.Info( Clip.class, audioInputStream.getFormat() );
           final Clip clip = (Clip) AudioSystem.getLine(info);

           clip.addLineListener(new LineListener() {
               public void update(LineEvent e) {
                   if (e.getType() == LineEvent.Type.STOP) {
                       synchronized(clip) {
                           clip.notify();
                       }
                   }
               }
           });
           clip.open(audioInputStream);

           clip.start();
           synchronized (clip) {
               clip.wait();
           }
           clip.drain();
           clip.close();
       }
       catch (Exception ex)
       {
           ex.printStackTrace();
       }
   }

(based on http://www.ibm.com/developerworks/java/library/j-5things12/index.html#N101F8)

It works well on my system.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×