Files
jpexs-decompiler/trunk/src/com/jpexs/helpers/SoundPlayer.java
Jindra Petøík c0a8e3f6be internal sound playing refactoring
diplay sound icon
ffdec:format code, organize imports
2014-03-14 20:30:45 +01:00

101 lines
2.7 KiB
Java

/*
* Copyright (C) 2010-2014 JPEXS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.jpexs.helpers;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.Line;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
/**
*
* @author JPEXS
*/
public class SoundPlayer {
private Clip clip;
public SoundPlayer(InputStream is) throws LineUnavailableException, IOException, UnsupportedAudioFileException {
clip = (Clip) AudioSystem.getLine(new Line.Info(Clip.class));
clip.open(AudioSystem.getAudioInputStream(new BufferedInputStream(is)));
}
public long samplesCount() {
return clip.getMicrosecondLength();
}
public void play() {
final SoundPlayer t = this;
clip.addLineListener(new LineListener() {
@Override
public void update(LineEvent event) {
if (event.getType() == LineEvent.Type.STOP) {
//clip.close();
synchronized (t) {
t.notifyAll();
}
}
}
});
clip.start();
try {
synchronized (this) {
wait();
}
} catch (InterruptedException ex) {
//Ignore
}
}
public long getSamplePosition() {
return clip.getMicrosecondPosition();
}
public void setPosition(long frames) {
clip.setMicrosecondPosition(frames);
}
public void stop() {
clip.stop();
}
public boolean isPlaying() {
return clip.isActive();
}
public long getFrameRate() {
return 1000000L;
}
@Override
protected void finalize() throws Throwable {
if (clip != null) {
clip.close();
}
}
}