Advertisement
Guest User

Untitled

a guest
Mar 16th, 2023
1,847
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.18 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.*;
  4. import javax.sound.sampled.*;
  5. import javax.swing.*;
  6. import javax.swing.filechooser.*;
  7.  
  8. public class JavaGUIMusicPlayerJFrame extends JFrame implements ActionListener {
  9.  
  10.     private JTextField filePathField;
  11.     private JButton playButton;
  12.     private JButton pauseButton;
  13.     private JButton chooseButton;
  14.     private JButton loopButton;
  15.     private boolean isPaused;
  16.     private boolean isLooping = false;
  17.     private JFileChooser fileChooser;
  18.     private Clip clip;
  19.    
  20.     public JavaGUIMusicPlayerJFrame()
  21.     {
  22.         super("Music Player");
  23.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  24.         setLayout(new FlowLayout());
  25.        
  26.         filePathField = new JTextField(20);
  27.         playButton = new JButton("Play");
  28.         pauseButton = new JButton("Pause");
  29.         chooseButton = new JButton("Choose File");
  30.         loopButton = new JButton("Loop");
  31.         isPaused = false;
  32.         isLooping = false;
  33.        
  34.         playButton.addActionListener(this);
  35.         pauseButton.addActionListener(this);
  36.         chooseButton.addActionListener(this);
  37.         loopButton.addActionListener(this);
  38.        
  39.         add(filePathField);
  40.         add(chooseButton);
  41.         add(playButton);
  42.         add(pauseButton);
  43.         add(loopButton);
  44.        
  45.         fileChooser = new JFileChooser(".");
  46.         fileChooser.setFileFilter(new FileNameExtensionFilter("WAV Files", "wav"));
  47.        
  48.         setSize(500, 100);
  49.         setLocationRelativeTo(null);
  50.         setVisible(true);
  51.     }
  52.    
  53.     @Override
  54.     public void actionPerformed(ActionEvent event) {
  55.        
  56.         if (event.getSource() == playButton)
  57.         {
  58.             playMusic();
  59.         }
  60.         else if (event.getSource() == pauseButton)
  61.         {
  62.             pauseMusic();
  63.         }
  64.         else if (event.getSource() == chooseButton)
  65.         {
  66.             chooseFile();
  67.         }
  68.         else if (event.getSource() == loopButton)
  69.         {
  70.             toggleLoop();
  71.         }
  72.     }
  73.    
  74.     private void playMusic() {
  75.        
  76.         if (clip != null && clip.isRunning())
  77.         {
  78.             clip.stop();
  79.         }
  80.        
  81.         try
  82.         {
  83.             File file = new File(filePathField.getText());
  84.             AudioInputStream audioIn = AudioSystem.getAudioInputStream(file);
  85.            
  86.             clip = AudioSystem.getClip();
  87.             clip.open(audioIn);
  88.            
  89.             if (isLooping)
  90.             {
  91.                 clip.loop(Clip.LOOP_CONTINUOUSLY);
  92.             }
  93.            
  94.             clip.start();
  95.  
  96.         }
  97.         catch(Exception e)
  98.         {
  99.             System.out.println(e);
  100.         }
  101.        
  102.     }
  103.    
  104.     private void pauseMusic()
  105.     {
  106.         if (clip != null && clip.isRunning())
  107.         {
  108.             clip.stop();
  109.             isPaused = true;
  110.             pauseButton.setText("Resume");
  111.         }
  112.         else if (clip != null && isPaused)
  113.         {
  114.             clip.start();
  115.            
  116.             if(isLooping)
  117.             {
  118.                 clip.loop(Clip.LOOP_CONTINUOUSLY);
  119.             }
  120.            
  121.             isPaused = false;
  122.             pauseButton.setText("Pause");
  123.         }
  124.     }
  125.    
  126.     private void chooseFile()
  127.     {
  128.         fileChooser.setCurrentDirectory(new File("."));
  129.         int result = fileChooser.showOpenDialog(this);
  130.         if (result == JFileChooser.APPROVE_OPTION)
  131.         {
  132.             File selectedFile = fileChooser.getSelectedFile();
  133.             filePathField.setText(selectedFile.getAbsolutePath());
  134.         }
  135.     }
  136.    
  137.     private void toggleLoop()
  138.     {
  139.         isLooping = !isLooping;
  140.         if (isLooping)
  141.         {
  142.             loopButton.setText("Stop Loop");
  143.            
  144.             if(clip.isRunning())
  145.             {
  146.                 clip.loop(Clip.LOOP_CONTINUOUSLY);
  147.             }
  148.         }
  149.         else
  150.         {
  151.             loopButton.setText("Loop");
  152.  
  153.             if(clip.isRunning())
  154.             {
  155.                 clip.loop(0);
  156.             }
  157.         }
  158.     }
  159.    
  160.     public static void main(String[] args) {
  161.         new JavaGUIMusicPlayerJFrame();
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement