Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package audiosurf;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import javax.swing.*;
- import javazoom.jl.decoder.JavaLayerException;
- import javazoom.jl.player.*;
- import javazoom.jl.player.advanced.*;
- /**
- *
- * @author Karel Hrkal
- */
- public class Test {
- private static ThreadPlayer tp;
- public static void main(String[] args) throws Exception {
- tp = new ThreadPlayer("E:/music/Fairy_Tail_-_Glitter_Starving_Trancer_Remix.mp3");
- new Thread(tp).start();
- JFrame main = new JFrame("Test");
- main.setLayout(new FlowLayout());
- JButton start = new JButton("Start");
- start.addActionListener(new StartListener());
- main.add(start);
- JButton stop = new JButton("Stop");
- stop.addActionListener(new StopListener());
- main.add(stop);
- main.pack();
- main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- main.setVisible(true);
- AdvancedPlayer ap;
- ap = new AdvancedPlayer(new FileInputStream("E:/music/Fairy_Tail_-_Glitter_Starving_Trancer_Remix.mp3"));
- ap.play(200);
- /*Toolkit.getDefaultToolkit().beep();
- ap = new AdvancedPlayer(new FileInputStream("E:/music/Fairy_Tail_-_Glitter_Starving_Trancer_Remix.mp3"));
- ap.play(500, 1000);*/
- }
- private static void start() {
- if (!tp.isPlaying()) {
- tp.pause(true);
- }
- }
- private static void stop() {
- if (tp.isPlaying()) {
- tp.pause(false);
- }
- }
- private static class StartListener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- start();
- }
- }
- private static class StopListener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- stop();
- }
- }
- private static class ThreadPlayer implements Runnable {
- private String fileName;
- private int position;
- private boolean isPlaying;
- private boolean die;
- private AdvancedPlayer ap;
- public ThreadPlayer(String fileName) {
- this.fileName = fileName;
- }
- public void run() {
- while (true) {
- if (isPlaying) {
- try {
- ap = new AdvancedPlayer(new FileInputStream(fileName));
- ap.setPlayBackListener(new MyPlaybackListener());
- ap.play(position, Integer.MAX_VALUE);
- } catch (FileNotFoundException ex) {
- System.out.println("File " + fileName + " was not found!");
- ex.printStackTrace(System.out);
- } catch (JavaLayerException ex) {
- ex.printStackTrace(System.out);
- }
- }
- try {
- Thread.sleep(1);
- } catch (InterruptedException ex) {
- }
- if (die) {
- break;
- }
- }
- }
- /**
- * Starts to play the music file from its begin.
- */
- public void start() {
- position = 0;
- isPlaying = true;
- }
- /**
- * Stops playing the music file. <br/>
- * Warning: do NOT call this function before the start() function.
- */
- public void stop() {
- position = 0;
- isPlaying = false;
- ap.stop();
- }
- /**
- * Pauses / unpauses playing music file, depending on isPlaying value. <br/>
- * Warning: do NOT call this function before the start() function.
- */
- public void pause() {
- pause(!isPlaying);
- }
- /**
- * Pauses / unpauses playing music file, depending on play value. <br/>
- * Noting will happen when both play and isPlaying is true, and vise versa. <br/>
- * Warning: do NOT call this function before the start() function.
- * @param play - true if the song hat to conctine its playing, or false if the song has to be paused.
- */
- public void pause(boolean play) {
- if (play) {
- isPlaying = true;
- } else {
- isPlaying = false;
- ap.stop();
- }
- }
- /**
- * Closes the player, making it to stop making any sound and stopping the run() function. <br/>
- * Warning: do NOT call this function before the start() function.
- */
- public void close() {
- ap.close();
- isPlaying = false;
- die = true;
- }
- public boolean isPlaying() {
- return isPlaying;
- }
- private class MyPlaybackListener extends PlaybackListener {
- public void playbackFinished(PlaybackEvent evt) {
- position = evt.getFrame();
- System.out.println("position on : " + position);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement