package JMPCommon;
import java.io.IOException;
import java.net.URL;
import javazoom.jl.decoder.Bitstream;
import javazoom.jl.decoder.BitstreamException;
import javazoom.jl.decoder.Decoder;
import javazoom.jl.decoder.Header;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.decoder.SampleBuffer;
import javazoom.jl.player.AudioDevice;
import javazoom.jl.player.FactoryRegistry;
public class JLayerPausable {
private URL urlToStraemFrom;
private Bitstream bitstream;
private Decoder decoder;
private AudioDevice outDevice;
private boolean isClosed = false;
private boolean isComplete = false;
private int frameIndexCurrent;
private boolean isPaused = false;;
private boolean isStop = true;
private SampleBuffer output;
public JLayerPausable(URL fileURL) throws JavaLayerException{
urlToStraemFrom = fileURL;
}
public boolean play(int startIndex, int endIndex, int correctionFactorInFrames) throws JavaLayerException{
isStop = false;
isPaused = false;
isClosed = false;
try {
bitstream = new Bitstream(urlToStraemFrom.openStream());
} catch (IOException e) {e.printStackTrace();}
outDevice = FactoryRegistry.systemRegistry().createAudioDevice();
outDevice.open(decoder = new Decoder());
boolean continuFrame = true;
isPaused = false;
isStop = false;
frameIndexCurrent = 0;
while(continuFrame && frameIndexCurrent < startIndex - correctionFactorInFrames){
continuFrame = skipFrame();
frameIndexCurrent++;
}
if(endIndex < 0){
endIndex = Integer.MAX_VALUE;
}
while(continuFrame && frameIndexCurrent < endIndex){
if(isPaused){
continuFrame = false;
try {
Thread.sleep(1);
} catch (InterruptedException e) {}
}else{
continuFrame = decodeFrame();
frameIndexCurrent++;
}
}
if(outDevice != null){
outDevice.flush();
synchronized (this) {
isComplete = (isClosed == false);
close();
}
}
return isComplete;
}
public boolean play(int startIndex) throws JavaLayerException{
return play(startIndex, -1, 16);
}
public boolean play() throws JavaLayerException{
return play(0);
}
public void pause(){
isStop = true;
isPaused = true;
close();
}
public void stop(){
isStop = true;
isPaused = false;
close();
frameIndexCurrent = 0;
}
public int getPosition(){
return frameIndexCurrent;
}
public void setPosition(int index){
if(index < 0){
index = 0;
}
frameIndexCurrent = index;
}
public boolean isStop(){
return isStop;
}
public boolean isPause(){
return isPaused;
}
public boolean resume() throws JavaLayerException{
return play(frameIndexCurrent);
}
public synchronized void close(){
if(outDevice != null){
isClosed = true;
outDevice.close();
outDevice = null;
try {
bitstream.close();
} catch (BitstreamException e) {}
}
}
private boolean skipFrame() throws JavaLayerException{
boolean ret = false;
Header header = bitstream.readFrame();
if(header != null){
bitstream.closeFrame();
ret = true;
}
return ret;
}
private boolean decodeFrame() throws JavaLayerException {
if (outDevice != null) {
Header header = bitstream.readFrame();
if (header != null) {
output = (SampleBuffer)decoder.decodeFrame(header, bitstream);
synchronized (this) {
if(!isClosed){
outDevice.write(output.getBuffer(), 0, output.getBufferLength());
}
}
bitstream.closeFrame();
}
}
return true;
}
}
package JMPCommon;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javazoom.jl.decoder.JavaLayerException;
public class MP3Palyer implements Runnable {
private JLayerPausable player;
private Thread playThread;
private String stringURL;
private int nowIndex = 0;
public MP3Palyer(File mp3File){
if(mp3File.isFile() && mp3File.exists()){
stringURL = "file:///" + mp3File.getAbsolutePath();
}
}
public MP3Palyer(String mp3File){
this(new File(mp3File));
}
public void init(){
try {
player = new JLayerPausable(new URL(stringURL));
} catch (MalformedURLException | JavaLayerException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
}
public void play(){
if(player == null){
init();
}
if(player.isStop() == true){
startThread();
}
}
public void stop(){
if(player != null && !player.isStop()){
player.stop();
nowIndex = 0;
playThread = null;
player =null;
}
}
public void pause() throws MalformedURLException, JavaLayerException{
if(player != null){
if(player.isPause() && player.isStop()){
player.setPosition(nowIndex);
play();
} else {
player.pause();
nowIndex = player.getPosition();
playThread = null;
}
}
}
public void startThread(){
//halt_ = false;
playThread = new Thread(this, "MP3PlayerThread");
playThread.start();
}
@Override
public void run() {
// TODO 自動生成されたメソッド・スタブ
try {
player.resume();
} catch (JavaLayerException e) {
e.printStackTrace();
}
}
}