Emin K.L.
Emin K.L.

Reputation: 23

Java music player progressBar

I was trying to create a simple music player only for become more fluent to use swing components. Whatsoever, I am stuck a just like this. It has no compiler or runtime error, rather creates a window as I intend. But whenever I pause the music and try to continue, progress bar doesn't work anymore and window basically doesn't respond till I terminate the program. (Yet clip starts to play again.) Only progressBar, tenBack and play buttons created yet, as you can see that repetitive while's are meant to simulate the progression of the playing clip. Which first one is working as I intended but second one messes up the execution. I have been trying to solve it myself for hours, and still don't have much of an idea if it's even an error. I also tried to take the second while at the very end of actionPerformed method, and creating a whole another method as progressor. However problem remain the same. Thanks for any assistance.

import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.File;
import javax.sound.sampled.*;
import java.io.IOException;

public class MyFrame extends JFrame implements ActionListener{

    JProgressBar progressBar;
    JButton tenBack;
    JButton play;
    Image icon;
    Clip clip;
    File musicFile;
    
    MyFrame() throws LineUnavailableException, UnsupportedAudioFileException, IOException{

        ImageIcon icon=new ImageIcon("icon.png");

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setIconImage(icon.getImage());
        this.setLayout(new FlowLayout());
        this.setTitle("Music Player");
        this.setSize(420, 200);

        musicFile=new File("pigstep.wav");

        AudioInputStream audioStream=AudioSystem.getAudioInputStream(musicFile);
        clip=AudioSystem.getClip();
        clip.open(audioStream);
        clip.start();

        progressBar=new JProgressBar();
        progressBar.setPreferredSize(new Dimension(380,20));
        progressBar.setBackground(Color.white);
        progressBar.setStringPainted(true);

        tenBack=new JButton("<<");
        tenBack.addActionListener(this);
        tenBack.setFont(new Font("Calibri",Font.BOLD,30));

        play=new JButton("||");
        play.addActionListener(this);
        play.setFont(new Font("Calibri",Font.BOLD,30));

        this.add(progressBar);
        this.add(tenBack);
        this.add(play);
        this.setVisible(true);

        while(clip.isRunning()){
            progressBar.setValue((int)(100*clip.getMicrosecondPosition()/clip.getMicrosecondLength()));
            progressBar.setString((int)(clip.getMicrosecondPosition()/(1e6*60))+":"+(int)((clip.getMicrosecondPosition()/1e6)%60)+" / "+(int)(clip.getMicrosecondLength()/(1e6*60))+":"+(int)((clip.getMicrosecondLength()/1e6)%60));
        }
    }

    @Override
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==tenBack)
            clip.setMicrosecondPosition((long)(clip.getMicrosecondPosition()-1e7));
        else if(e.getSource()==play){
            if(clip.isRunning()){
                play.setText(">");
                clip.stop();
            }
            else{
                play.setText("||");
                clip.start();
                while(clip.isRunning()){
            progressBar.setValue((int)(100*clip.getMicrosecondPosition()/clip.getMicrosecondLength()));
            progressBar.setString((int)(clip.getMicrosecondPosition()/(1e6*60))+":"+(int)((clip.getMicrosecondPosition()/1e6)%60)+" / "+(int)(clip.getMicrosecondLength()/(1e6*60))+":"+(int)((clip.getMicrosecondLength()/1e6)%60));
                }
            }
        }
    }
}

Upvotes: 0

Views: 693

Answers (0)

Related Questions