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;
}
java类javax.sound.sampled.Clip的实例源码
Sound.java 文件源码
项目:WorldGrower
阅读 29
收藏 0
点赞 0
评论 0
EventCountdown.java 文件源码
项目:EEWD
阅读 13
收藏 0
点赞 0
评论 0
public EventCountdown() {
Application app = Application.getInstance();
String fileName = app.getProperty(Application.PropertyCountdownSound, (String) null);
countdownMillis = (long) (app.getProperty(Application.PropertyCountdownSeconds, (Float) 0.0f) * 1000f);
vs = app.getProperty(Application.PropertyVS, Application.DefaultVS);
if (fileName != null && countdownMillis > 0) {
try {
sound = new File(fileName);
AudioInputStream inputStream = AudioSystem.getAudioInputStream(sound);
AudioFormat format = inputStream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
clip = (Clip) AudioSystem.getLine(info);
} catch (LineUnavailableException | UnsupportedAudioFileException | IOException e) {
LOG.error("could not open countdown sound file", e);
}
}
}
MSNView.java 文件源码
项目:NoMoreDropboxMSN
阅读 18
收藏 0
点赞 0
评论 0
/**
* Performs message received sound, if sound setting allows it.
*/
public void messageSound(){
if(sound){
new Thread(() -> {
try {
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(
(getClass().getResource("/Media/MSNsound.wav")));
clip.open(inputStream);
clip.start();
} catch (Exception e) {
// System.err.println(e.getMessage());
}
}).start();
}
}
AudioTool.java 文件源码
项目:jwarframe
阅读 15
收藏 0
点赞 0
评论 0
private void playIt(InputStream inputStream) throws IOException, UnsupportedAudioFileException, LineUnavailableException, InterruptedException, IllegalArgumentException {
AudioListener listener = new AudioListener();
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new BufferedInputStream(inputStream));
AudioFormat format = audioInputStream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
Clip clip = (Clip) AudioSystem.getLine(info);
clip.addLineListener(listener);
clip.open(audioInputStream);
try {
clip.start();
listener.waitUntilDone();
} finally {
clip.close();
}
} finally {
inputStream.close();
}
}
Audio.java 文件源码
项目:openhab-hdl
阅读 22
收藏 0
点赞 0
评论 0
private static void playInThread(final Clip clip) {
// run in new thread
new Thread() {
public void run() {
try {
clip.start();
while (clip.isActive()) {
sleep(1000L);
}
clip.close();
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
}.start();
}
Wavefile.java 文件源码
项目:MakamBox
阅读 15
收藏 0
点赞 0
评论 0
private void setClip(File music){ // Create clip from file
try {
System.gc();
af = music; // af is wave file from disk
stream = AudioSystem.getAudioInputStream(music); // Creating a input stream
format = stream.getFormat();
rawData = new byte[(int)stream.getFrameLength()*2*format.getChannels()]; // Define byte array size
IOUtils.read(AudioSystem.getAudioInputStream(af), rawData); // Read all data from clip to array
info = new DataLine.Info(Clip.class, format);
clip = (Clip)AudioSystem.getLine(info); // Creating clip
clip.open(stream); // Opening clip with stream
System.gc();
} catch (Exception e) {
e.printStackTrace();
}
}
PlaySoundActionImpl.java 文件源码
项目:raspberry-pi-api
阅读 15
收藏 0
点赞 0
评论 0
@Override
public void invokeAction(final PiAction action) throws RaspberryPiAppException {
final String soundFile = baseSoundDirectory + action.getValue();
LOGGER.debug("Playing sound file: '{}'", soundFile);
try {
final AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(
new File(soundFile));
final Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
// play at maximum volume
final FloatControl gainControl =
(FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
gainControl.setValue(gainControl.getMaximum());
clip.start();
} catch (Exception e) {
throw new RaspberryPiAppException(e.getMessage(), e);
}
}
MSNView.java 文件源码
项目:DropboxMSN
阅读 18
收藏 0
点赞 0
评论 0
/**
* Performs message received sound, if sound setting allows it.
*/
public void messageSound(){
if(sound){
new Thread(() -> {
try {
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(
(getClass().getResource("/Media/MSNsound.wav")));
clip.open(inputStream);
clip.start();
} catch (Exception e) {
// System.err.println(e.getMessage());
}
}).start();
}
}
VNSound.java 文件源码
项目:VN-RW
阅读 17
收藏 0
点赞 0
评论 0
/**
* Opens a new sound and replaces the old one. If object is set to loop the sound (isLoop = true) then it will loop
*
* @param file - the file path and name of the audio to play
*/
public void open(File file) {
try {
clip.stop();
clip.close();
audio = AudioSystem.getAudioInputStream(file);
clip.open(audio);
volumeControl = (FloatControl)clip.getControl(FloatControl.Type.MASTER_GAIN);
clip.start();
if(isLoop)
clip.loop(Clip.LOOP_CONTINUOUSLY);
else
clip.loop(0);
}
catch(UnsupportedAudioFileException uae) {
System.out.println(uae);
}
catch(IOException ioe) {
System.out.println(ioe);
System.out.println(file);
}
catch(LineUnavailableException lua) {
System.out.println(lua);
}
}
VNSound.java 文件源码
项目:VN-RW
阅读 15
收藏 0
点赞 0
评论 0
/**
* Will open a new file for a certain volume.
*
* @param file - File of audio to open
* @param volume - Volume of audio to play from 100 to 0
*/
public void open(File file, float volume) {
try {
clip.stop();
clip.close();
audio = AudioSystem.getAudioInputStream(file);
clip.open(audio);
volumeControl = (FloatControl)clip.getControl(FloatControl.Type.MASTER_GAIN);
setVolume(volume);
clip.start();
if(isLoop)
clip.loop(Clip.LOOP_CONTINUOUSLY);
else
clip.loop(0);
}
catch(UnsupportedAudioFileException uae) {
System.out.println(uae);
}
catch(IOException ioe) {
System.out.println(ioe);
System.out.println(file);
}
catch(LineUnavailableException lua) {
System.out.println(lua);
}
}