下面列出了javax.sound.sampled.Clip#setFramePosition ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void update(LineEvent event) {
// �X�g�b�v���Ō�܂ōĐ����ꂽ�ꍇ
if (event.getType() == LineEvent.Type.STOP) {
Clip clip = (Clip) event.getSource();
clip.stop();
clip.setFramePosition(0); // �Đ��ʒu���ŏ��ɖ߂�
}
}
public void update(LineEvent event) {
// �X�g�b�v���Ō�܂ōĐ����ꂽ�ꍇ
if (event.getType() == LineEvent.Type.STOP) {
Clip clip = (Clip) event.getSource();
clip.stop();
clip.setFramePosition(0); // �Đ��ʒu���ŏ��ɖ߂�
}
}
public static void main(String args[]) throws Exception {
boolean res = true;
try {
AudioInputStream ais = new AudioInputStream(
new ByteArrayInputStream(new byte[2000]),
new AudioFormat(8000.0f, 8, 1, false, false), 2000); //
AudioFormat format = ais.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format,
((int) ais.getFrameLength()
* format
.getFrameSize()));
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open();
FloatControl rateControl = (FloatControl) clip.getControl(
FloatControl.Type.SAMPLE_RATE);
int c = 0;
while (c++ < 10) {
clip.stop();
clip.setFramePosition(0);
clip.start();
for (float frq = 22000; frq < 44100; frq = frq + 100) {
try {
Thread.currentThread().sleep(20);
} catch (Exception e) {
break;
}
rateControl.setValue(frq);
}
}
} catch (Exception ex) {
ex.printStackTrace();
res = ex.getMessage().indexOf(
"This method should not have been invoked!") < 0;
}
if (res) {
System.out.println("Test passed");
} else {
System.out.println("Test failed");
throw new Exception("Test failed");
}
}
/**
* Plays the clip with the specified volume.
* @param volume the volume the play at
* @param listener the line listener
* @throws LineUnavailableException if a clip object is not available or
* if the line cannot be opened due to resource restrictions
*/
public void start(float volume, LineListener listener) throws LineUnavailableException {
Clip clip = getClip();
if (clip == null)
return;
// PulseAudio does not support Master Gain
if (clip.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
// set volume
FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
float dB = (float) (Math.log(volume) / Math.log(10.0) * 20.0);
gainControl.setValue(dB);
}
if (listener != null)
clip.addLineListener(listener);
clip.setFramePosition(0);
clip.start();
}
/**
* Playback
* @param name Registered name
*/
public void play(String name) {
Clip clip = clipMap.get(name);
if(clip != null) {
// Stop
clip.stop();
// Playback position back to the beginning
clip.setFramePosition(0);
// Playback
clip.start();
}
}
public void update(LineEvent event) {
// If you stop playback or to the end
if(event.getType() == LineEvent.Type.STOP) {
Clip clip = (Clip) event.getSource();
clip.stop();
clip.setFramePosition(0); // Playback position back to the beginning
}
}
/**
* Plays the clip with the specified volume.
* @param volume the volume the play at
* @param listener the line listener
* @throws LineUnavailableException if a clip object is not available or
* if the line cannot be opened due to resource restrictions
*/
public void start(float volume, LineListener listener) throws LineUnavailableException {
Clip clip = getClip();
if (clip == null)
return;
// PulseAudio does not support Master Gain
if (clip.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
// set volume
FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
float dB = (float) (Math.log(volume) / Math.log(10.0) * 20.0);
gainControl.setValue(Utils.clamp(dB, gainControl.getMinimum(), gainControl.getMaximum()));
} else if (clip.isControlSupported(FloatControl.Type.VOLUME)) {
// The docs don't mention what unit "volume" is supposed to be,
// but for PulseAudio it seems to be amplitude
FloatControl volumeControl = (FloatControl) clip.getControl(FloatControl.Type.VOLUME);
float amplitude = (float) Math.sqrt(volume) * volumeControl.getMaximum();
volumeControl.setValue(Utils.clamp(amplitude, volumeControl.getMinimum(), volumeControl.getMaximum()));
}
if (listener != null)
clip.addLineListener(listener);
clip.setFramePosition(0);
clip.start();
}
public void update(LineEvent event) {
// �X�g�b�v���Ō�܂ōĐ����ꂽ�ꍇ
if (event.getType() == LineEvent.Type.STOP) {
Clip clip = (Clip) event.getSource();
clip.stop();
clip.setFramePosition(0); // �Đ��ʒu���ŏ��ɖ߂�
}
}
public void update(LineEvent event) {
// �X�g�b�v���Ō�܂ōĐ����ꂽ�ꍇ
if (event.getType() == LineEvent.Type.STOP) {
Clip clip = (Clip) event.getSource();
clip.stop();
clip.setFramePosition(0); // �Đ��ʒu���ŏ��ɖ߂�
}
}
public void update(LineEvent event) {
// �X�g�b�v���Ō�܂ōĐ����ꂽ�ꍇ
if (event.getType() == LineEvent.Type.STOP) {
Clip clip = (Clip) event.getSource();
clip.stop();
clip.setFramePosition(0); // �Đ��ʒu���ŏ��ɖ߂�
}
}
public void update(LineEvent event) {
// �X�g�b�v���Ō�܂ōĐ����ꂽ�ꍇ
if (event.getType() == LineEvent.Type.STOP) {
Clip clip = (Clip) event.getSource();
clip.stop();
clip.setFramePosition(0); // �Đ��ʒu���ŏ��ɖ߂�
}
}
public void play() {
try {
final Clip clipToPlay;
if (preLoadedClip != null) {
clipToPlay = preLoadedClip;
clipToPlay.setFramePosition(0);
} else {
clipToPlay = openClip(CLOSE_AFTER_PLAYING);
}
clipToPlay.start();
} catch (IOException | LineUnavailableException | UnsupportedAudioFileException e) {
throw new IllegalStateException(e);
}
}
static protected void test()
throws LineUnavailableException, InterruptedException {
DataLine.Info info = new DataLine.Info(Clip.class, format);
Clip clip = (Clip)AudioSystem.getLine(info);
final MutableBoolean clipStoppedEvent = new MutableBoolean(false);
clip.addLineListener(new LineListener() {
@Override
public void update(LineEvent event) {
if (event.getType() == LineEvent.Type.STOP) {
synchronized (clipStoppedEvent) {
clipStoppedEvent.value = true;
clipStoppedEvent.notifyAll();
}
}
}
});
clip.open(format, soundData, 0, soundData.length);
long lengthClip = clip.getMicrosecondLength() / 1000;
log("Clip length " + lengthClip + " ms");
log("Playing...");
for (int i=1; i<=LOOP_COUNT; i++) {
long startTime = currentTimeMillis();
log(" Loop " + i);
clip.start();
synchronized (clipStoppedEvent) {
while (!clipStoppedEvent.value) {
clipStoppedEvent.wait();
}
clipStoppedEvent.value = false;
}
long endTime = currentTimeMillis();
long lengthPlayed = endTime - startTime;
if (lengthClip > lengthPlayed + 20) {
log(" ERR: Looks like sound didn't play: played " + lengthPlayed + " ms instead " + lengthClip);
countErrors++;
} else {
log(" OK: played " + lengthPlayed + " ms");
}
clip.setFramePosition(0);
}
log("Played " + LOOP_COUNT + " times, " + countErrors + " errors detected.");
}