0

I found this TestCode at http://www.tutorials.de/threads/java-einen-sound-abspielen.17178/ It's working on OSX but not on my PC Win7. Are there any issues with the Sound API on Win7? I have several Sound Devices installed (integerated and FireWire AudioInterface) and tried both and heared nothing.

import javax.sound.sampled.*;
import java.io.*;

/*
 * SoundTest.java
 *
 * Created on 1. August 2003, 21:06
 */

/**
 *
 * @author  Administrator
 */
public class SoundTest {

    /** Creates a new instance of SoundTest */
    public SoundTest() {
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        try{
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("sample.wav"));
            AudioFormat af     = audioInputStream.getFormat();
            int size      = (int) (af.getFrameSize() * audioInputStream.getFrameLength());
            byte[] audio       = new byte[size];
            DataLine.Info info      = new DataLine.Info(Clip.class, af, size);
            audioInputStream.read(audio, 0, size);

           // for(int i=0; i < 32; i++) {
                Clip clip = (Clip) AudioSystem.getLine(info);
                clip.open(af, audio, 0, size);
                clip.start();
           // }
        }catch(Exception e){ e.printStackTrace(); }

    }

}

Thanks for replies!

J

user3834607
  • 37
  • 1
  • 6

2 Answers2

1

If you are using Java 7 I recommend trying the JavaFX APIs. They are way more advanced and compatible across platforms. You are not limited to the web when using JavaFX, before anyone asks.

Akira
  • 4,001
  • 1
  • 16
  • 24
0

You took a bad example, thats mixing up code from two different approaches (reading the audiostream yourself versus letting clip handle the stream).

The top answer here How can I play sound in Java? shows how to play via Clip.

Community
  • 1
  • 1
Durandal
  • 19,919
  • 4
  • 36
  • 70