Advertisement
Guest User

JavaKarmelFriend

a guest
Feb 22nd, 2020
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.84 KB | None | 0 0
  1.  
  2. import jaco.mp3.player.MP3Player;
  3.  
  4. import java.awt.Color;
  5. import java.awt.FlowLayout;
  6. import java.awt.Font;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.MouseAdapter;
  10. import java.awt.event.MouseEvent;
  11. import java.io.File;
  12. import java.io.FileNotFoundException;
  13. import java.util.Scanner;
  14.  
  15. import javax.swing.*;
  16.  
  17. class Demo implements ActionListener
  18. {
  19.  
  20.     JFrame f1 = new JFrame("Music Player");
  21.  
  22.     JButton playlist = new JButton();
  23.     JButton browse = new JButton();
  24.     JButton favourite = new JButton();
  25.     JButton skip_Backward = new JButton();
  26.     JButton play = new JButton();
  27.     JButton skip_Forward = new JButton();
  28.     JButton stop = new JButton();
  29.     JButton volume = new JButton();
  30.    
  31.     JSlider volume_Slider = new JSlider();
  32.  
  33.  
  34.     JFileChooser fc;
  35.  
  36.     MP3Player mp3;
  37.  
  38.     String filepath;
  39.  
  40.     JTextField tf1 = new JTextField("");
  41.  
  42.     private int music_is_Playing;
  43.  
  44.  
  45.     ImageIcon ic1 = new ImageIcon("E:\\Java Projects\\Music Player2\\src\\Image Icons\\512.png");
  46.    
  47.     ImageIcon ic2 = new ImageIcon("E:\\Java Projects\\Music Player\\src\\playlist.png");
  48.     ImageIcon ic3 = new ImageIcon("E:\\Java Projects\\Music Player\\src\\browse.png");
  49.     ImageIcon ic4 = new ImageIcon("E:\\Java Projects\\Music Player\\src\\Favourite.png");
  50.     ImageIcon ic5 = new ImageIcon("E:\\Java Projects\\Music Player\\src\\Skip Backward.png");
  51.    
  52.     ImageIcon ic6 = new ImageIcon("E:\\Java Projects\\Music Player\\src\\Play.png");
  53.     ImageIcon ic7 = new ImageIcon("E:\\Java Projects\\Music Player\\src\\Skip Forward.png");
  54.     ImageIcon ic8 = new ImageIcon("E:\\Java Projects\\Music Player\\src\\stop.png");
  55.     ImageIcon ic9 = new ImageIcon("E:\\Java Projects\\Music Player\\src\\volume-adjustment.png");
  56.     ImageIcon ic10 = new ImageIcon("E:\\Java Projects\\Music Player\\src\\Favourite_Added.png");
  57.     ImageIcon ic11 = new ImageIcon("E:\\Java Projects\\Music Player\\src\\Pause.png");
  58.  
  59.  
  60.  
  61.  
  62.     Font font1 = new Font("Century", Font.BOLD, 12);
  63.  
  64.     Color LIGHT_YELLOW = new Color(255, 255, 153);
  65.    
  66.    
  67.     public int volume_Value, play_button_is_pressed;
  68.    
  69.    
  70.     JFrame f2 = new JFrame("Playlist");
  71.    
  72.     JButton add_new_playlist = new JButton("Add New Playlist");
  73.    
  74.     JButton delete_playlist = new JButton("Delete Playlist");
  75.    
  76.     JButton favourites_Playlist = new JButton("Favourites");
  77.    
  78.     JButton open_Playlist = new JButton("Open Playlist");
  79.    
  80.     JButton cancel = new JButton("Cancel");
  81.    
  82.     JButton add_To_Playlist = new JButton("Add Tracks To Playlist");
  83.  
  84.    
  85.    
  86.    
  87.    
  88.     //Program Control is getting started from here //
  89.    
  90.     void proceed() {
  91.  
  92.         playlist.setIcon(ic2);
  93.         playlist.addActionListener(this);
  94.        
  95.         browse.setIcon(ic3);
  96.         browse.addActionListener(this);
  97.        
  98.         favourite.setIcon(ic4);
  99.         favourite.addActionListener(this);
  100.        
  101.         skip_Backward.setIcon(ic5);
  102.         skip_Backward.addActionListener(this);
  103.        
  104.         play.setIcon(ic6);
  105.         play.addActionListener(this);
  106.        
  107.         skip_Forward.setIcon(ic7);
  108.         skip_Forward.addActionListener(this);
  109.        
  110.         stop.setIcon(ic8);
  111.         stop.addActionListener(this);
  112.        
  113.         volume.setIcon(ic9);
  114.         volume.addActionListener(this);
  115.  
  116.         volume_Slider.setToolTipText("Volume");
  117.         volume_Slider.addMouseListener(new MouseAdapter() {
  118.            
  119.             public void mousePressed(MouseEvent me) {
  120.                 try
  121.                 {
  122.                      volume_Value = volume_Slider.getValue();
  123.                      mp3.setVolume(volume_Value);
  124.                 }
  125.                 catch(Exception e)
  126.                 {
  127.                    JOptionPane.showMessageDialog(f1, e);
  128.                 }
  129.             }
  130.         });
  131.  
  132.        
  133.  
  134.        
  135.  
  136.        
  137.         tf1.setFont(font1);
  138.         tf1.setBackground(LIGHT_YELLOW);
  139.         tf1.setEditable(false);
  140.         tf1.setVisible(false);
  141.  
  142.    
  143.         f1.add(playlist);
  144.         f1.add(browse);
  145.         f1.add(favourite);
  146.         f1.add(tf1);
  147.         f1.add(skip_Backward);
  148.         f1.add(play);
  149.         f1.add(skip_Forward);
  150.         f1.add(stop);
  151.         f1.add(volume);
  152.         f1.add(volume_Slider);
  153.    
  154.    
  155.         f1.setIconImage(ic1.getImage());
  156.         f1.setLocation(10, 50);
  157.         f1.setVisible(true);
  158.         f1.setLayout(new FlowLayout());
  159.         f1.setSize(1350, 150);
  160.         f1.setDefaultCloseOperation(3);
  161.         f1.setResizable(true);
  162.     }
  163.  
  164.    
  165.    
  166.    
  167.    
  168.     //Playlist Frame Settings //
  169.    
  170.    
  171.     void playList_Form_Settings()
  172.     {
  173.         cancel.addActionListener(this);
  174.         open_Playlist.addActionListener(this);
  175.        
  176.         f2.add(open_Playlist);
  177.         f2.add(add_To_Playlist);
  178.         f2.add(add_new_playlist);
  179.         f2.add(delete_playlist);
  180.         f2.add(favourites_Playlist);
  181.         f2.add(cancel);
  182.        
  183.        
  184.         f2.setVisible(true);
  185.         f2.setSize(400,400);
  186.         f2.setLocation(100,50);
  187.         f2.setLayout(new FlowLayout());
  188.     }
  189.    
  190.    
  191.    
  192.    
  193.    
  194.     void eventForBrowseLabel() {
  195.         if (music_is_Playing == 1) {
  196.             mp3.stop();
  197.            
  198.         }
  199.        
  200.        
  201.         if(play_button_is_pressed %2 !=0)
  202.         {
  203.             play_button_is_pressed--;
  204.             play.setIcon(ic6);
  205.         }
  206.  
  207.         tf1.setText("Now Playing : ");
  208.         fc = new JFileChooser();
  209.         int i = fc.showOpenDialog(null);
  210.  
  211.         if (i == JFileChooser.APPROVE_OPTION) {
  212.  
  213.             filepath = fc.getSelectedFile().getPath();
  214.  
  215.             mp3 = new MP3Player(new File(filepath));
  216.  
  217.             tf1.setText("Now Playing : " + filepath.substring(13));
  218.             tf1.setVisible(true);
  219.  
  220.             if (mp3.isStopped()) {
  221.                 mp3.setRepeat(true);
  222.             }
  223.         }
  224.     }
  225.  
  226.    
  227.    
  228.    
  229.     void eventForPlayLabel() {
  230.         tf1.setVisible(true);
  231.         tf1.setText("Now Playing : " + filepath.substring(13));
  232.         mp3.play();
  233.         mp3.setVolume(volume_Slider.getValue());
  234.  
  235.         music_is_Playing = 1;
  236.  
  237.         if (mp3.isStopped()) {
  238.             mp3.setRepeat(true);
  239.         }
  240.     }
  241.  
  242.    
  243.    
  244.    
  245.     void eventForPauseLabel() {
  246.         if (mp3.isPaused()) {
  247.             tf1.setText("Now Playing : " + filepath.substring(13));
  248.             mp3.play();
  249.  
  250.             if (mp3.isStopped()) {
  251.                 mp3.setRepeat(true);
  252.             }
  253.         } else {
  254.             tf1.setText("Now Paused : " + filepath);
  255.             mp3.pause();
  256.         }
  257.     }
  258.  
  259.    
  260.    
  261.    
  262.     void eventForStopLabel() {
  263.         tf1.setVisible(false);
  264.         mp3.stop();
  265.        
  266.        
  267.         if(play_button_is_pressed %2 !=0)
  268.         {
  269.             play_button_is_pressed--;
  270.             play.setIcon(ic6);
  271.         }
  272.     }
  273.    
  274.    
  275.    
  276.    
  277.    
  278.     public void actionPerformed(ActionEvent ae)
  279.     {
  280.         if(ae.getSource() == playlist)
  281.         {
  282.             playList_Form_Settings();
  283.         }
  284.        
  285.        
  286.        
  287.         if(ae.getSource() == browse)
  288.         {
  289.              try
  290.                 {
  291.                    eventForBrowseLabel();
  292.                 }
  293.                 catch(Exception e)
  294.                 {
  295.                    JOptionPane.showMessageDialog(f1, e);
  296.                 }
  297.         }
  298.        
  299.        
  300.        
  301.        
  302.         if(ae.getSource() == favourite)
  303.         {
  304.              favourite.setIcon(ic10);
  305.              JOptionPane.showMessageDialog(f1, "Favourite Added");
  306.         }
  307.        
  308.        
  309.        
  310.        
  311.         if(ae.getSource() == skip_Backward )
  312.         {
  313.             try
  314.             {
  315.                mp3.skipBackward();
  316.             }
  317.             catch(Exception e)
  318.             {
  319.                JOptionPane.showMessageDialog(f1, e);
  320.             }
  321.         }
  322.        
  323.        
  324.        
  325.        
  326.         if(ae.getSource() == play)
  327.         {
  328.             try
  329.             {
  330.             play_button_is_pressed ++;
  331.            
  332.             if(play_button_is_pressed % 2 != 0)
  333.             {
  334.                 eventForPlayLabel();
  335.                 play.setIcon(ic11);
  336.             }
  337.            
  338.             else
  339.             {
  340.                 eventForPauseLabel();
  341.                 play.setIcon(ic6);
  342.             }
  343.             }
  344.             catch(Exception e)
  345.             {
  346.                 JOptionPane.showMessageDialog(f1, e);
  347.             }
  348.         }
  349.        
  350.        
  351.        
  352.        
  353.         if(ae.getSource() == skip_Forward)
  354.         {
  355.             try
  356.             {
  357.                mp3.skipForward();
  358.             }
  359.             catch(Exception e)
  360.             {
  361.                JOptionPane.showMessageDialog(f1, e);
  362.             }
  363.         }
  364.        
  365.        
  366.        
  367.         if(ae.getSource() == stop)
  368.         {
  369.             eventForStopLabel();
  370.         }
  371.        
  372.        
  373.        
  374.        
  375.        
  376.        
  377.         //PlayList Frame Buttons Settings//
  378.        
  379.         if(ae.getSource() == cancel)
  380.         {
  381.          f2.dispose();
  382.         }
  383.        
  384.        
  385.         if(ae.getSource() == open_Playlist)
  386.         {
  387.            
  388.             fc = new JFileChooser();
  389.             int i = fc.showOpenDialog(null);
  390.  
  391.             if (i == JFileChooser.APPROVE_OPTION)
  392.             {
  393.  
  394.             filepath = fc.getSelectedFile().getPath();
  395.            
  396.             try
  397.             {
  398.  
  399.             File playList_File = new File(fc.getSelectedFile().getPath());
  400.            
  401.             Scanner sc = new Scanner(playList_File);
  402.  
  403.             while (sc.hasNextLine())
  404.             {
  405.               String data = sc.nextLine();
  406.              
  407.               mp3 = new MP3Player(new File(data));
  408.              
  409.               mp3.setShuffle(true);
  410.               System.out.println(data);
  411.             }
  412.  
  413.             sc.close();
  414.  
  415.             }catch (FileNotFoundException e)
  416.             {
  417.             JOptionPane.showMessageDialog(f1, e);
  418.             }
  419.                
  420.             }
  421.         }
  422.     }
  423.    
  424.    
  425.  
  426.     public static void main(String args[]) {
  427.         new Demo().proceed();
  428.     }
  429.  
  430. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement