下面列出了怎么用javax.sound.sampled.DataLine的API类实例代码及写法,或者点击链接到github查看源代码。
private boolean createSourceDataLine() {
if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createSourceDataLine()");
try {
DataLine.Info info = new DataLine.Info(SourceDataLine.class, loadedAudioFormat);
if (!(AudioSystem.isLineSupported(info)) ) {
if (DEBUG || Printer.err)Printer.err("Line not supported: "+loadedAudioFormat);
// fail silently
return false;
}
SourceDataLine source = (SourceDataLine) AudioSystem.getLine(info);
datapusher = new DataPusher(source, loadedAudioFormat, loadedAudio, loadedAudioByteLength);
} catch (Exception e) {
if (DEBUG || Printer.err)e.printStackTrace();
// fail silently
return false;
}
if (datapusher==null) {
// fail silently
return false;
}
if (DEBUG || Printer.debug)Printer.debug("Created SourceDataLine.");
return true;
}
private boolean createSourceDataLine() {
if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createSourceDataLine()");
try {
DataLine.Info info = new DataLine.Info(SourceDataLine.class, loadedAudioFormat);
if (!(AudioSystem.isLineSupported(info)) ) {
if (DEBUG || Printer.err)Printer.err("Line not supported: "+loadedAudioFormat);
// fail silently
return false;
}
SourceDataLine source = (SourceDataLine) AudioSystem.getLine(info);
datapusher = new DataPusher(source, loadedAudioFormat, loadedAudio, loadedAudioByteLength);
} catch (Exception e) {
if (DEBUG || Printer.err)e.printStackTrace();
// fail silently
return false;
}
if (datapusher==null) {
// fail silently
return false;
}
if (DEBUG || Printer.debug)Printer.debug("Created SourceDataLine.");
return true;
}
private Clip openClip(boolean closeAfterPlaying) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
AudioInputStream audioStream = AudioSystem.getAudioInputStream(new ByteArrayInputStream(audioFilePath));
DataLine.Info info = getLineInfo(audioStream);
Clip audioClip = (Clip) AudioSystem.getLine(info);
if (closeAfterPlaying) {
audioClip.addLineListener(new LineListener() {
@Override
public void update(LineEvent myLineEvent) {
if (myLineEvent.getType() == LineEvent.Type.STOP)
audioClip.close();
}
});
}
audioClip.open(audioStream);
return audioClip;
}
private static AudioFormat getFirstLinearFormat(Line.Info[] infos) {
for (int i = 0; i < infos.length; i++) {
if (infos[i] instanceof DataLine.Info) {
AudioFormat[] formats = ((DataLine.Info) infos[i]).getFormats();
for (int j = 0; j < formats.length; j++) {
AudioFormat.Encoding encoding = formats[j].getEncoding();
int sampleSizeInBits = formats[j].getSampleSizeInBits();
if (encoding.equals(AudioFormat.Encoding.PCM_SIGNED) &&
sampleSizeInBits == 16 ||
encoding.equals(AudioFormat.Encoding.PCM_UNSIGNED) &&
sampleSizeInBits == 16) {
return formats[j];
}
}
}
}
return null;
}
public static void checkLines(Mixer mixer, Line.Info[] infos) {
for (int i = 0; i<infos.length; i++) {
try {
if (infos[i] instanceof DataLine.Info) {
DataLine.Info info = (DataLine.Info) infos[i];
System.out.println(" Line "+info+" (max. "+mixer.getMaxLines(info)+" simultaneously): ");
AudioFormat[] formats = info.getFormats();
for (int f = 0; f < formats.length; f++) {
try {
AudioFormat otherEndianOrSign = getOtherEndianOrSign(formats[f]);
if (otherEndianOrSign != null) {
checkFormat(formats, otherEndianOrSign);
}
} catch (Exception e1) {
out(" Unexpected exception when getting a format: "+e1);
}
}
}
} catch (Exception e) {
out(" Unexpected exception when getting a line: "+e);
}
}
}
private boolean createSourceDataLine() {
if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createSourceDataLine()");
try {
DataLine.Info info = new DataLine.Info(SourceDataLine.class, loadedAudioFormat);
if (!(AudioSystem.isLineSupported(info)) ) {
if (DEBUG || Printer.err)Printer.err("Line not supported: "+loadedAudioFormat);
// fail silently
return false;
}
SourceDataLine source = (SourceDataLine) AudioSystem.getLine(info);
datapusher = new DataPusher(source, loadedAudioFormat, loadedAudio, loadedAudioByteLength);
} catch (Exception e) {
if (DEBUG || Printer.err)e.printStackTrace();
// fail silently
return false;
}
if (datapusher==null) {
// fail silently
return false;
}
if (DEBUG || Printer.debug)Printer.debug("Created SourceDataLine.");
return true;
}
private boolean createSourceDataLine() {
if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createSourceDataLine()");
try {
DataLine.Info info = new DataLine.Info(SourceDataLine.class, loadedAudioFormat);
if (!(AudioSystem.isLineSupported(info)) ) {
if (DEBUG || Printer.err)Printer.err("Line not supported: "+loadedAudioFormat);
// fail silently
return false;
}
SourceDataLine source = (SourceDataLine) AudioSystem.getLine(info);
datapusher = new DataPusher(source, loadedAudioFormat, loadedAudio, loadedAudioByteLength);
} catch (Exception e) {
if (DEBUG || Printer.err)e.printStackTrace();
// fail silently
return false;
}
if (datapusher==null) {
// fail silently
return false;
}
if (DEBUG || Printer.debug)Printer.debug("Created SourceDataLine.");
return true;
}
private boolean createSourceDataLine() {
if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createSourceDataLine()");
try {
DataLine.Info info = new DataLine.Info(SourceDataLine.class, loadedAudioFormat);
if (!(AudioSystem.isLineSupported(info)) ) {
if (DEBUG || Printer.err)Printer.err("Line not supported: "+loadedAudioFormat);
// fail silently
return false;
}
SourceDataLine source = (SourceDataLine) AudioSystem.getLine(info);
datapusher = new DataPusher(source, loadedAudioFormat, loadedAudio, loadedAudioByteLength);
} catch (Exception e) {
if (DEBUG || Printer.err)e.printStackTrace();
// fail silently
return false;
}
if (datapusher==null) {
// fail silently
return false;
}
if (DEBUG || Printer.debug)Printer.debug("Created SourceDataLine.");
return true;
}
private static void doMixerClip(Mixer mixer, AudioFormat format) {
if (mixer==null) return;
try {
System.out.println("Clip from mixer "+mixer+":");
System.out.println(" "+mixer.getMixerInfo());
DataLine.Info info = new DataLine.Info(
Clip.class,
format);
if (mixer.isLineSupported(info)) {
Clip clip = (Clip) mixer.getLine(info);
doLine1(clip, format);
} else {
System.out.println(" - Line not supported");
}
} catch (Throwable t) {
System.out.println(" - Caught exception. Not failed.");
System.out.println(" - "+t.toString());
}
}
/**
* WAVE�t�@�C�������[�h
* @param url WAVE�t�@�C����URL
*/
public static void load(URL url) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
// �I�[�f�B�I�X�g���[�����J��
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
// WAVE�t�@�C���̃t�H�[�}�b�g���擾
AudioFormat format = ais.getFormat();
// ���C�����擾
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format, AudioSystem.NOT_SPECIFIED);
// WAVE�f�[�^���擾
DataClip clip = new DataClip(ais);
// WAVE�f�[�^��o�^
clips[counter] = clip;
lines[counter] = (SourceDataLine)AudioSystem.getLine(info);
// ���C�����J��
lines[counter].open(format);
counter++;
}
private boolean createSourceDataLine() {
if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createSourceDataLine()");
try {
DataLine.Info info = new DataLine.Info(SourceDataLine.class, loadedAudioFormat);
if (!(AudioSystem.isLineSupported(info)) ) {
if (DEBUG || Printer.err)Printer.err("Line not supported: "+loadedAudioFormat);
// fail silently
return false;
}
SourceDataLine source = (SourceDataLine) AudioSystem.getLine(info);
datapusher = new DataPusher(source, loadedAudioFormat, loadedAudio, loadedAudioByteLength);
} catch (Exception e) {
if (DEBUG || Printer.err)e.printStackTrace();
// fail silently
return false;
}
if (datapusher==null) {
// fail silently
return false;
}
if (DEBUG || Printer.debug)Printer.debug("Created SourceDataLine.");
return true;
}
private boolean createSourceDataLine() {
if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createSourceDataLine()");
try {
DataLine.Info info = new DataLine.Info(SourceDataLine.class, loadedAudioFormat);
if (!(AudioSystem.isLineSupported(info)) ) {
if (DEBUG || Printer.err)Printer.err("Line not supported: "+loadedAudioFormat);
// fail silently
return false;
}
SourceDataLine source = (SourceDataLine) AudioSystem.getLine(info);
datapusher = new DataPusher(source, loadedAudioFormat, loadedAudio, loadedAudioByteLength);
} catch (Exception e) {
if (DEBUG || Printer.err)e.printStackTrace();
// fail silently
return false;
}
if (datapusher==null) {
// fail silently
return false;
}
if (DEBUG || Printer.debug)Printer.debug("Created SourceDataLine.");
return true;
}
private void initJavaSound(int channels, int rate) {
try {
AudioFormat audioFormat = new AudioFormat(rate, 16,
channels, true, // PCM_Signed
false // littleEndian
);
DataLine.Info info = new DataLine.Info(SourceDataLine.class,
audioFormat, AudioSystem.NOT_SPECIFIED);
if (!AudioSystem.isLineSupported(info)) {
throw new Exception("Line " + info + " not supported.");
}
outputLine = (SourceDataLine) AudioSystem.getLine(info);
// outputLine.addLineListener(this);
outputLine.open(audioFormat);
this.rate = rate;
this.channels = channels;
setBalance(balance);
setGain(gain);
} catch (Exception ex) {
LOGGER.log(Level.WARNING, "can not init sound system", ex);
}
}
private void playSirenSound() {
try {
File soundFile = new File(sirenFile);
AudioInputStream soundIn = AudioSystem.getAudioInputStream(soundFile);
AudioFormat format = soundIn.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
clip = (Clip) AudioSystem.getLine(info);
clip.addLineListener(new LineListener() {
@Override
public void update(LineEvent event) {
if (event.getType() == LineEvent.Type.STOP) {
soundOn = false;
}
}
});
clip.open(soundIn);
clip.start();
soundOn = true;
} catch (Exception e) {
e.printStackTrace();
}
}
public static FloatControl getControl(AudioInputStream in) {
try {
AudioFormat baseFormat = in.getFormat();
AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
baseFormat.getSampleRate(),
16,
baseFormat.getChannels(),
baseFormat.getChannels() * 2,
baseFormat.getSampleRate(),
false);
AudioInputStream ain = AudioSystem.getAudioInputStream(decodedFormat, in);
DataLine.Info info = new DataLine.Info(Clip.class, decodedFormat);
try ( Clip clip = (Clip) AudioSystem.getLine(info)) {
clip.open(ain);
FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
return gainControl;
}
} catch (Exception e) {
logger.debug(e.toString());
return null;
}
}
public static void rawplay(AudioFormat targetFormat, AudioInputStream din)
throws IOException, LineUnavailableException {
byte[] data = new byte[CommonValues.IOBufferLength];
DataLine.Info info = new DataLine.Info(SourceDataLine.class, targetFormat);
SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
line.open(targetFormat);
if (line != null) {
// Start
FloatControl vol = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN);
logger.debug(vol.getValue() + vol.getUnits());
line.start();
int nBytesRead = 0, nBytesWritten = 0;
while (nBytesRead != -1) {
nBytesRead = din.read(data, 0, data.length);
if (nBytesRead != -1) {
nBytesWritten = line.write(data, 0, nBytesRead);
}
}
// Stop
line.drain();
line.stop();
line.close();
din.close();
}
}
private boolean createSourceDataLine() {
if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createSourceDataLine()");
try {
DataLine.Info info = new DataLine.Info(SourceDataLine.class, loadedAudioFormat);
if (!(AudioSystem.isLineSupported(info)) ) {
if (DEBUG || Printer.err)Printer.err("Line not supported: "+loadedAudioFormat);
// fail silently
return false;
}
SourceDataLine source = (SourceDataLine) AudioSystem.getLine(info);
datapusher = new DataPusher(source, loadedAudioFormat, loadedAudio, loadedAudioByteLength);
} catch (Exception e) {
if (DEBUG || Printer.err)e.printStackTrace();
// fail silently
return false;
}
if (datapusher==null) {
// fail silently
return false;
}
if (DEBUG || Printer.debug)Printer.debug("Created SourceDataLine.");
return true;
}
public static void main(String[] args) throws LineUnavailableException, IOException {
AudioPlayerManager manager = new DefaultAudioPlayerManager();
AudioSourceManagers.registerRemoteSources(manager);
manager.getConfiguration().setOutputFormat(COMMON_PCM_S16_BE);
AudioPlayer player = manager.createPlayer();
manager.loadItem("ytsearch: epic soundtracks", new FunctionalResultHandler(null, playlist -> {
player.playTrack(playlist.getTracks().get(0));
}, null, null));
AudioDataFormat format = manager.getConfiguration().getOutputFormat();
AudioInputStream stream = AudioPlayerInputStream.createStream(player, format, 10000L, false);
SourceDataLine.Info info = new DataLine.Info(SourceDataLine.class, stream.getFormat());
SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
line.open(stream.getFormat());
line.start();
byte[] buffer = new byte[COMMON_PCM_S16_BE.maximumChunkSize()];
int chunkSize;
while ((chunkSize = stream.read(buffer)) >= 0) {
line.write(buffer, 0, chunkSize);
}
}
/**
* FIXME:
* specify the buffer size in the open(AudioFormat,int) method. A delay of 10ms-100ms will be acceptable for realtime audio. Very low latencies like will
* not work on all computer systems, and 100ms or more will probably be annoying for your users. A good tradeoff is, e.g. 50ms. For your audio format,
* 8-bit, mono at 44100Hz, a good buffer size is 2200 bytes, which is almost 50ms
*/
void initializeOutput() {
DataLine.Info dataLineInfo = new DataLine.Info( SourceDataLine.class, audioFormat);
//line = (TargetDataLine) AudioSystem.getLine(info);
//Mixer m = AudioSystem.getMixer(null);
try {
//sourceDataLine = (SourceDataLine)m.getLine(dataLineInfo);
sourceDataLine = (SourceDataLine)AudioSystem.getLine(dataLineInfo);
sourceDataLine.open(audioFormat);
sourceDataLine.start();
} catch (LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace(Log.getWriter());
}
}
/**
* WAVE�t�@�C�������[�h
* @param url WAVE�t�@�C����URL
*/
public static void load(URL url) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
// �I�[�f�B�I�X�g���[�����J��
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
// WAVE�t�@�C���̃t�H�[�}�b�g���擾
AudioFormat format = ais.getFormat();
// ���C�����擾
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format, AudioSystem.NOT_SPECIFIED);
// WAVE�f�[�^���擾
DataClip clip = new DataClip(ais);
// WAVE�f�[�^��o�^
clips[counter] = clip;
lines[counter] = (SourceDataLine)AudioSystem.getLine(info);
// ���C�����J��
lines[counter].open(format);
counter++;
}
private boolean createClip() {
if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createClip()");
try {
DataLine.Info info = new DataLine.Info(Clip.class, loadedAudioFormat);
if (!(AudioSystem.isLineSupported(info)) ) {
if (DEBUG || Printer.err)Printer.err("Clip not supported: "+loadedAudioFormat);
// fail silently
return false;
}
Object line = AudioSystem.getLine(info);
if (!(line instanceof AutoClosingClip)) {
if (DEBUG || Printer.err)Printer.err("Clip is not auto closing!"+clip);
// fail -> will try with SourceDataLine
return false;
}
clip = (AutoClosingClip) line;
clip.setAutoClosing(true);
if (DEBUG || Printer.debug) clip.addLineListener(this);
} catch (Exception e) {
if (DEBUG || Printer.err)e.printStackTrace();
// fail silently
return false;
}
if (clip==null) {
// fail silently
return false;
}
if (DEBUG || Printer.debug)Printer.debug("Loaded clip.");
return true;
}
private boolean createClip() {
if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createClip()");
try {
DataLine.Info info = new DataLine.Info(Clip.class, loadedAudioFormat);
if (!(AudioSystem.isLineSupported(info)) ) {
if (DEBUG || Printer.err)Printer.err("Clip not supported: "+loadedAudioFormat);
// fail silently
return false;
}
Object line = AudioSystem.getLine(info);
if (!(line instanceof AutoClosingClip)) {
if (DEBUG || Printer.err)Printer.err("Clip is not auto closing!"+clip);
// fail -> will try with SourceDataLine
return false;
}
clip = (AutoClosingClip) line;
clip.setAutoClosing(true);
if (DEBUG || Printer.debug) clip.addLineListener(this);
} catch (Exception e) {
if (DEBUG || Printer.err)e.printStackTrace();
// fail silently
return false;
}
if (clip==null) {
// fail silently
return false;
}
if (DEBUG || Printer.debug)Printer.debug("Loaded clip.");
return true;
}
public Line getLine(Line.Info info) throws LineUnavailableException {
if (!isLineSupported(info))
throw new IllegalArgumentException("Line unsupported: " + info);
if ((info.getLineClass() == SourceDataLine.class)) {
return new SoftMixingSourceDataLine(this, (DataLine.Info) info);
}
if ((info.getLineClass() == Clip.class)) {
return new SoftMixingClip(this, (DataLine.Info) info);
}
throw new IllegalArgumentException("Line unsupported: " + info);
}
public Line getLine(Line.Info info) throws LineUnavailableException {
if (!isLineSupported(info))
throw new IllegalArgumentException("Line unsupported: " + info);
if ((info.getLineClass() == SourceDataLine.class)) {
return new SoftMixingSourceDataLine(this, (DataLine.Info) info);
}
if ((info.getLineClass() == Clip.class)) {
return new SoftMixingClip(this, (DataLine.Info) info);
}
throw new IllegalArgumentException("Line unsupported: " + info);
}
private boolean createClip() {
if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createClip()");
try {
DataLine.Info info = new DataLine.Info(Clip.class, loadedAudioFormat);
if (!(AudioSystem.isLineSupported(info)) ) {
if (DEBUG || Printer.err)Printer.err("Clip not supported: "+loadedAudioFormat);
// fail silently
return false;
}
Object line = AudioSystem.getLine(info);
if (!(line instanceof AutoClosingClip)) {
if (DEBUG || Printer.err)Printer.err("Clip is not auto closing!"+clip);
// fail -> will try with SourceDataLine
return false;
}
clip = (AutoClosingClip) line;
clip.setAutoClosing(true);
if (DEBUG || Printer.debug) clip.addLineListener(this);
} catch (Exception e) {
if (DEBUG || Printer.err)e.printStackTrace();
// fail silently
return false;
}
if (clip==null) {
// fail silently
return false;
}
if (DEBUG || Printer.debug)Printer.debug("Loaded clip.");
return true;
}
public AsyncLineStopper(DataLine line, long delayMS) {
this.line = line;
this.delayMS = delayMS;
thread = new Thread(this);
thread.setDaemon(true);
// starts the thread and waits until it becomes ready
synchronized (readyEvent) {
thread.start();
try {
readyEvent.wait();
} catch (InterruptedException ex) { }
}
}
public AsyncLineStopper(DataLine line, long delayMS) {
this.line = line;
this.delayMS = delayMS;
thread = new Thread(this);
thread.setDaemon(true);
// starts the thread and waits until it becomes ready
synchronized (readyEvent) {
thread.start();
try {
readyEvent.wait();
} catch (InterruptedException ex) { }
}
}
SoftMixingDataLine(SoftMixingMixer mixer, DataLine.Info info) {
this.mixer = mixer;
this.info = info;
this.control_mutex = mixer.control_mutex;
controls = new Control[] { gain_control, mute_control, balance_control,
pan_control, reverbsend_control, chorussend_control,
apply_reverb };
calcVolume();
}
public void setDevice(int position) throws LineUnavailableException, IllegalArgumentException {
if (position == 0 || position == -1) {
initializeOutput();
} else {
Mixer appMixer = mixerList[position];
Info sdlLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat);
sourceDataLine = (SourceDataLine) appMixer.getLine(sdlLineInfo);
sourceDataLine.open(audioFormat);
sourceDataLine.start();
}
}
public static TargetDataLine get() throws Exception {
AudioFormat format = new AudioFormat(16000, 16, 1, true, false);
DataLine.Info datalineInfo = new DataLine.Info(TargetDataLine.class, format);
TargetDataLine dataLine = (TargetDataLine) AudioSystem.getLine(datalineInfo);
dataLine.open(format);
return dataLine;
}