Clip interface...

R

romain

hi i'm looking for a class implementing the Clip interface of java.sound.sampled
i'm begining to use this package, and i'm a bit lost
please help...
 
K

Knute Johnson

romain said:
hi i'm looking for a class implementing the Clip interface of java.sound.sampled
i'm begining to use this package, and i'm a bit lost
please help...

Here is a very simple test program for the Clip interface. Clips are
very handy but they have some limitations and are sometimes harder to
implement well than their more complicated cousins the Source and
TargetDataLine. After this example is the same program using a
SourceDataLine to play an audio file. Put the name of the file on the
command line when you run these programs. The file type must be a .wav
or .au since those are the only files JavaSound knows.

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

public class Play2 {
static Clip line;

public static void main(String[] args) {
try {
AudioInputStream ais =
AudioSystem.getAudioInputStream(new File(args[0]));
AudioFormat af = ais.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class,af);

if (!AudioSystem.isLineSupported(info)) {
System.out.println("unsupported line");
System.exit(0);
}

line = (Clip) AudioSystem.getLine(info);
line.open(ais);
line.start();

line.addLineListener(new LineListener() {
public void update(LineEvent le) {
if (le.getType().equals(LineEvent.Type.STOP)) {
line.close();
System.exit(0);
}
}
});

} catch (Exception e) {
System.out.println(e.toString());
}
}
}


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

public class Play {
public static void main(String[] args) {
try {
AudioInputStream ais =
AudioSystem.getAudioInputStream(new File(args[0]));
AudioFormat af = ais.getFormat();
DataLine.Info info = new
DataLine.Info(SourceDataLine.class,af);

if (!AudioSystem.isLineSupported(info)) {
System.out.println("unsupported line");
System.exit(0);
}

SourceDataLine line = (SourceDataLine)
AudioSystem.getLine(info);
line.open(af);
line.start();

byte[] data = new byte[2048];
int bytesRead;

while ((bytesRead = ais.read(data,0,data.length)) != -1)
line.write(data,0,bytesRead);

line.drain();
line.stop();
line.close();
} catch (Exception e) {
System.out.println(e.toString());
}
System.exit(0);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top