Ich versuche mit Hilfe der JLayer-Library einen Mp3 Player mit den Grundfunktionen Play|Pause|Resume|Stop zu schreiben. Play und Stop funktionieren soweit. Ich weiß nur nicht, wie ich den Player anhalte und dann an der gleichen Position fortsetze. Anbei der Code:
Code:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.AudioDevice;
import javazoom.jl.player.FactoryRegistry;
import javazoom.jl.player.advanced.AdvancedPlayer;
import javazoom.jl.player.advanced.PlaybackEvent;
import javazoom.jl.player.advanced.PlaybackListener;
public class Mp3Player extends JFrame implements ActionListener{
public Mp3Player() {
state = "STOP";
setTitle("Mp3Player");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btnPlay.setActionCommand("PLAY");
btnPlay.addActionListener(this);
btnPause.setActionCommand("PAUSE");
btnPause.addActionListener(this);
btnStop.setActionCommand("STOP");
btnStop.addActionListener(this);
pnlButtons.add(btnPlay);
pnlButtons.add(btnPause);
pnlButtons.add(btnStop);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(pnlInfo, BorderLayout.CENTER);
getContentPane().add(pnlButtons, BorderLayout.SOUTH);
pack();
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
if (actionCommand.equals("PLAY")) {
if (state.equals("STOP"))
play();
else if (state.equals("PAUSE"))
resume();
} else if (actionCommand.equals("PAUSE")) {
if (state.equals("PLAY"))
pause();
} else if (actionCommand.equals("STOP")) {
if (state.equals("PLAY") || (state.equals("PAUSE")))
stop();
}
}
private void play() {
state = "PLAY";
try {
FileInputStream fileInputStream = new FileInputStream("t:/test.mp3");
InputStream inputStream = new BufferedInputStream(fileInputStream);
FactoryRegistry factoryRegistry = FactoryRegistry.systemRegistry();
audioDevice = factoryRegistry.createAudioDevice();
player = new AdvancedPlayer(inputStream, audioDevice);
player.setPlayBackListener(new PlaybackListener() {
@Override
public void playbackStarted(PlaybackEvent arg0) {
System.out.println(">> Playback started.");
}
@Override
public void playbackFinished(PlaybackEvent arg0) {
System.out.println("<< Playback finished.");
}
});
playerThread = new PlayerThread();
playerThread.start();
timeThread = new TimeThread();
timeThread.start();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JavaLayerException e) {
e.printStackTrace();
}
}
public void pause() {
state = "PAUSE";
position = audioDevice.getPosition();
System.out.println("|| Pause (" + position + ")");
// player pausieren
// timer pausieren
}
public void resume() {
state = "PLAY";
// player fortsetzen
// timer fortsetzen
}
public void stop() {
state = "STOP";
player.stop();
timeThread.interrupt();
}
private class PlayerThread extends Thread {
@Override
public void run() {
try {
player.play();
} catch (JavaLayerException e) {
e.printStackTrace();
}
}
}
private class TimeThread extends Thread {
@Override
public void run() {
while (true) {
if (isInterrupted()) {
break;
}
System.out.println("... playing (" + audioDevice.getPosition() + ")");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
interrupt();
}
}
}
}
public static void main(String[] args) {
new Mp3Player();
}
private JButton btnPlay = new JButton("Play");
private JButton btnPause = new JButton("Pause");
private JButton btnStop = new JButton("Stop");
private JPanel pnlInfo = new JPanel(new BorderLayout());
private JPanel pnlButtons = new JPanel(new FlowLayout());
private AudioDevice audioDevice;
private AdvancedPlayer player;
private int position = 0;
private PlayerThread playerThread = null;
private TimeThread timeThread = null;
private String state;
}