Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import jaco.mp3.player.MP3Player;
- import java.awt.Color;
- import java.awt.FlowLayout;
- import java.awt.Font;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- import javax.swing.*;
- class Demo implements ActionListener
- {
- JFrame f1 = new JFrame("Music Player");
- JButton playlist = new JButton();
- JButton browse = new JButton();
- JButton favourite = new JButton();
- JButton skip_Backward = new JButton();
- JButton play = new JButton();
- JButton skip_Forward = new JButton();
- JButton stop = new JButton();
- JButton volume = new JButton();
- JSlider volume_Slider = new JSlider();
- JFileChooser fc;
- MP3Player mp3;
- String filepath;
- JTextField tf1 = new JTextField("");
- private int music_is_Playing;
- ImageIcon ic1 = new ImageIcon("E:\\Java Projects\\Music Player2\\src\\Image Icons\\512.png");
- ImageIcon ic2 = new ImageIcon("E:\\Java Projects\\Music Player\\src\\playlist.png");
- ImageIcon ic3 = new ImageIcon("E:\\Java Projects\\Music Player\\src\\browse.png");
- ImageIcon ic4 = new ImageIcon("E:\\Java Projects\\Music Player\\src\\Favourite.png");
- ImageIcon ic5 = new ImageIcon("E:\\Java Projects\\Music Player\\src\\Skip Backward.png");
- ImageIcon ic6 = new ImageIcon("E:\\Java Projects\\Music Player\\src\\Play.png");
- ImageIcon ic7 = new ImageIcon("E:\\Java Projects\\Music Player\\src\\Skip Forward.png");
- ImageIcon ic8 = new ImageIcon("E:\\Java Projects\\Music Player\\src\\stop.png");
- ImageIcon ic9 = new ImageIcon("E:\\Java Projects\\Music Player\\src\\volume-adjustment.png");
- ImageIcon ic10 = new ImageIcon("E:\\Java Projects\\Music Player\\src\\Favourite_Added.png");
- ImageIcon ic11 = new ImageIcon("E:\\Java Projects\\Music Player\\src\\Pause.png");
- Font font1 = new Font("Century", Font.BOLD, 12);
- Color LIGHT_YELLOW = new Color(255, 255, 153);
- public int volume_Value, play_button_is_pressed;
- JFrame f2 = new JFrame("Playlist");
- JButton add_new_playlist = new JButton("Add New Playlist");
- JButton delete_playlist = new JButton("Delete Playlist");
- JButton favourites_Playlist = new JButton("Favourites");
- JButton open_Playlist = new JButton("Open Playlist");
- JButton cancel = new JButton("Cancel");
- JButton add_To_Playlist = new JButton("Add Tracks To Playlist");
- //Program Control is getting started from here //
- void proceed() {
- playlist.setIcon(ic2);
- playlist.addActionListener(this);
- browse.setIcon(ic3);
- browse.addActionListener(this);
- favourite.setIcon(ic4);
- favourite.addActionListener(this);
- skip_Backward.setIcon(ic5);
- skip_Backward.addActionListener(this);
- play.setIcon(ic6);
- play.addActionListener(this);
- skip_Forward.setIcon(ic7);
- skip_Forward.addActionListener(this);
- stop.setIcon(ic8);
- stop.addActionListener(this);
- volume.setIcon(ic9);
- volume.addActionListener(this);
- volume_Slider.setToolTipText("Volume");
- volume_Slider.addMouseListener(new MouseAdapter() {
- public void mousePressed(MouseEvent me) {
- try
- {
- volume_Value = volume_Slider.getValue();
- mp3.setVolume(volume_Value);
- }
- catch(Exception e)
- {
- JOptionPane.showMessageDialog(f1, e);
- }
- }
- });
- tf1.setFont(font1);
- tf1.setBackground(LIGHT_YELLOW);
- tf1.setEditable(false);
- tf1.setVisible(false);
- f1.add(playlist);
- f1.add(browse);
- f1.add(favourite);
- f1.add(tf1);
- f1.add(skip_Backward);
- f1.add(play);
- f1.add(skip_Forward);
- f1.add(stop);
- f1.add(volume);
- f1.add(volume_Slider);
- f1.setIconImage(ic1.getImage());
- f1.setLocation(10, 50);
- f1.setVisible(true);
- f1.setLayout(new FlowLayout());
- f1.setSize(1350, 150);
- f1.setDefaultCloseOperation(3);
- f1.setResizable(true);
- }
- //Playlist Frame Settings //
- void playList_Form_Settings()
- {
- cancel.addActionListener(this);
- open_Playlist.addActionListener(this);
- f2.add(open_Playlist);
- f2.add(add_To_Playlist);
- f2.add(add_new_playlist);
- f2.add(delete_playlist);
- f2.add(favourites_Playlist);
- f2.add(cancel);
- f2.setVisible(true);
- f2.setSize(400,400);
- f2.setLocation(100,50);
- f2.setLayout(new FlowLayout());
- }
- void eventForBrowseLabel() {
- if (music_is_Playing == 1) {
- mp3.stop();
- }
- if(play_button_is_pressed %2 !=0)
- {
- play_button_is_pressed--;
- play.setIcon(ic6);
- }
- tf1.setText("Now Playing : ");
- fc = new JFileChooser();
- int i = fc.showOpenDialog(null);
- if (i == JFileChooser.APPROVE_OPTION) {
- filepath = fc.getSelectedFile().getPath();
- mp3 = new MP3Player(new File(filepath));
- tf1.setText("Now Playing : " + filepath.substring(13));
- tf1.setVisible(true);
- if (mp3.isStopped()) {
- mp3.setRepeat(true);
- }
- }
- }
- void eventForPlayLabel() {
- tf1.setVisible(true);
- tf1.setText("Now Playing : " + filepath.substring(13));
- mp3.play();
- mp3.setVolume(volume_Slider.getValue());
- music_is_Playing = 1;
- if (mp3.isStopped()) {
- mp3.setRepeat(true);
- }
- }
- void eventForPauseLabel() {
- if (mp3.isPaused()) {
- tf1.setText("Now Playing : " + filepath.substring(13));
- mp3.play();
- if (mp3.isStopped()) {
- mp3.setRepeat(true);
- }
- } else {
- tf1.setText("Now Paused : " + filepath);
- mp3.pause();
- }
- }
- void eventForStopLabel() {
- tf1.setVisible(false);
- mp3.stop();
- if(play_button_is_pressed %2 !=0)
- {
- play_button_is_pressed--;
- play.setIcon(ic6);
- }
- }
- public void actionPerformed(ActionEvent ae)
- {
- if(ae.getSource() == playlist)
- {
- playList_Form_Settings();
- }
- if(ae.getSource() == browse)
- {
- try
- {
- eventForBrowseLabel();
- }
- catch(Exception e)
- {
- JOptionPane.showMessageDialog(f1, e);
- }
- }
- if(ae.getSource() == favourite)
- {
- favourite.setIcon(ic10);
- JOptionPane.showMessageDialog(f1, "Favourite Added");
- }
- if(ae.getSource() == skip_Backward )
- {
- try
- {
- mp3.skipBackward();
- }
- catch(Exception e)
- {
- JOptionPane.showMessageDialog(f1, e);
- }
- }
- if(ae.getSource() == play)
- {
- try
- {
- play_button_is_pressed ++;
- if(play_button_is_pressed % 2 != 0)
- {
- eventForPlayLabel();
- play.setIcon(ic11);
- }
- else
- {
- eventForPauseLabel();
- play.setIcon(ic6);
- }
- }
- catch(Exception e)
- {
- JOptionPane.showMessageDialog(f1, e);
- }
- }
- if(ae.getSource() == skip_Forward)
- {
- try
- {
- mp3.skipForward();
- }
- catch(Exception e)
- {
- JOptionPane.showMessageDialog(f1, e);
- }
- }
- if(ae.getSource() == stop)
- {
- eventForStopLabel();
- }
- //PlayList Frame Buttons Settings//
- if(ae.getSource() == cancel)
- {
- f2.dispose();
- }
- if(ae.getSource() == open_Playlist)
- {
- fc = new JFileChooser();
- int i = fc.showOpenDialog(null);
- if (i == JFileChooser.APPROVE_OPTION)
- {
- filepath = fc.getSelectedFile().getPath();
- try
- {
- File playList_File = new File(fc.getSelectedFile().getPath());
- Scanner sc = new Scanner(playList_File);
- while (sc.hasNextLine())
- {
- String data = sc.nextLine();
- mp3 = new MP3Player(new File(data));
- mp3.setShuffle(true);
- System.out.println(data);
- }
- sc.close();
- }catch (FileNotFoundException e)
- {
- JOptionPane.showMessageDialog(f1, e);
- }
- }
- }
- }
- public static void main(String args[]) {
- new Demo().proceed();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement