0% found this document useful (0 votes)
12 views

Music Player Documentation

Uploaded by

Ichigo Kurosaki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Music Player Documentation

Uploaded by

Ichigo Kurosaki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

MUSIC PLAYER APPLICATION

INTRODUCTION :
I am Dasari Snehith, a second-year student in GITAM University,
currently pursuing my degree in BSc CSCS. I am very keen in learning
Java programming language and it’s mechanism to enhance my skills and
knowledge in the Computer Science field. The Music Player Application
project that I have done and documented here represents my knowledge
to apply theoretical concepts learned in my coursework to practical
programming projects. By studying and implementing this project, I tried
to deepen my knowledge and understanding of GUI(Graphical User
Interface) , while also creating a simple and basic platform for music
enthusiasts.

QUESTION:
Create a simple GUI-based music player.Use classes for songs, playlists,
and user settings.Implement method overloading for playing music by
song name or playlist.

OVERVIEW:
The Music Player Application is a simple GUI-based Java program that
allows a user to choose and play a song. The User is given a choice to
select and play one song at a time from the available songs and playlists.
The program mostly depends on GUI components, overriding and
overloading concepts to handle the input from the user and display the
songs selected from the user.
CONCEPTS INVOLVED IN THE PROGRAM:
1) Swing GUI (Graphical User Interface):
 Swing is a GUI widget toolkit for Java. It is part of Oracle's Java
Foundation Classes (JFC) – an API for providing a graphical user
interface (GUI) for Java programs.
 Swing was developed to provide a more sophisticated set of
GUI components than the earlier Abstract Window Toolkit
(AWT).
 It provides a rich set of easy-to-use GUI components such as
buttons, text fields, labels, tables, and more for building desktop
applications in Java.
 The objects used from Swing GUI are:

a) JFrame: Represents a window in a Swing application.


b) JPanel: A container used to hold and organize other
components.
c) JLabel: Displays text or an image.
d) JComboBox: A drop-down list from which the user can select
an item.
e) JButton: A button that can be clicked by the user.
f) ActionListener: Interface used to handle events such as button
clicks.

2) Event Handling:
 In Java, event handling refers to the mechanism through which
events, such as user actions (e.g., mouse clicks, key presses) or
system-generated events (e.g., timer expiration, data arrival), are
detected and processed by a program.
 Event handling is crucial for building interactive graphical user
interfaces (GUIs) and responsive applications.
 Java provides several classes and interfaces in the “java.awt” and
“javax.swing” packages for event handling.
 When an event occurs, the event source notifies all registered event
listeners by invoking appropriate callback methods. Event listeners
then perform the necessary actions in response to the event.
 Event Handlers used in the program are:
a) ActionEvent: Represents an action that has occurred, such as a
button click.
b) addActionListener(): Method used to register an action listener to
handle events.

3) Collections:
In Java, the “java.util” package provides a framework for collection
classes and interfaces. Collections in Java allow you to manipulate
groups of objects, providing functionalities such as adding, removing,
and accessing elements within the collection.
Collections used in the program are:

a) ‘List’: An interface representing an ordered collection of elements.


b) ‘ArrayList’: A class that implements the ‘List’ interface using a
dynamically resizable array.

4) Classes and Objects:


In Java, classes and objects are fundamental concepts in object-
oriented programming (OOP).
 Class: A class is a blueprint or template for creating objects. It
defines the properties (attributes) and behaviors (methods) that
objects of that class will have. In Java, a class is declared using the
class keyword followed by the class name.
 Object: An object is an instance of a class. It represents a specific
entity or concept in your program. Objects have state (attributes)
and behavior (methods) as defined by their class. In Java, you create
objects using the new keyword followed by a constructor of the class.
The program defines classes such as “Song”, “Playlist”, and
“MusicPlayer”, each representing a real-world entity with its properties
and behaviors.

5) Constructors and Methods:


Constructors: A constructor in Java is a special type of method that
is automatically called when an instance (object) of a class is created.
It typically initializes the newly created object. Constructors have the
same name as the class and do not have a return type, not even void.
Constructors used in the program are:

 Song( )
 Playlist( )
 MusicPlayer( )
Methods : Methods in Java are functions defined within a class to
perform certain actions or calculations. They are used to define the
behavior of objects. Methods can accept parameters and return values.
Methods used in the program are:

 getName(),
 getArtist(),
 addSong(),
 getSongs(),
 updateSongComboBox(),
 play().

6) Lambda Expressions:
 Lambda expressions were introduced in Java 8 as a way to
enable functional programming features in the language.
 Lambda expressions provide a concise way to express
instances of single-method interfaces (also known as functional
interfaces).
 They allow you to treat functionality as a method argument or
create an instance of a functional interface without having to
explicitly define a separate class implementing that interface.
The Lambda Expression is used in the main method in the program to
create and initialize the “MusicPlayer” object SwingUtilities.invokeLater( )

7)ExceptionHandling:
Exception handling in Java is a mechanism used to deal with
runtime errors, also known as exceptions, in a structured and
controlled manner.
Exceptions occur during the execution of a program when an
unexpected situation arises that disrupts the normal flow of the
program.
The Exception used in the program is:
JOptionPane.showMessageDialog( ): Displays a dialog box to
inform the user about an error (song not found).

8) StringManipulation:
String manipulation in Java involves performing various operations on
strings such as concatenation, substring extraction, searching, replacing,
converting cases, splitting, trimming, and more.
Some common string manipulation operations in Java are :

 Concatenation
 Substring Extraction
 Searching
 Replacing
 Converting Cases
 Splitting
 Trimming

The string manipulation operator used in this program is:


equals( ): Compares the contents of two strings to determine if they are
equal.

9) Overloading and Overriding:


Overloading occurs when multiple methods in the same class have the
same name but different parameters. It allows a class to have multiple
methods with the same name but with different method signatures.
 Method Signature: Overloaded methods must have different
parameter lists (number of parameters or type of parameters).
 Return Type: Overloaded methods can have the same or different
return types.
 Access Modifier: Overloaded methods can have different access
modifiers.
 Exception Handling: Overloaded methods can throw different
exceptions.
In this program , the method which was overloaded is “play” method.

Overriding occurs when a subclass provides a specific implementation for


a method that is already defined in its superclass. It allows a subclass to
provide a specific implementation of a method that is already provided by
one of its parent classes.

 The method signature (name and parameters) in the subclass must


exactly match the method signature in the superclass.
 The return type of the overriding method can be a subtype of the
return type of the overridden method or it can be the same.
 The overriding method can throw the same, a subtype, or no
exception compared to the overridden method.
In this program, the method which was overrided is “ actionPerformed( )”
MusicPlayer.java
This Java program is a simple music player GUI application that allows
users to select playlists and songs from those playlists and play them. It
consists of three classes: “Song”, “Playlist”, and “MusicPlayer”.

SONG CLASS:
Code:
class Song {
private String name;
private String artist;

public Song(String name, String artist) {


this.name = name;
this.artist = artist;
}

public String getName() {


return name;
}

public String getArtist() {


return artist;
}
}

 This class represents a song.


 It has two private member variables name and artist.
 The constructor initializes these variables with the provided values.
 “getName()” returns the name of the song.
 “getArtist()” returns the artist of the song.

PLAYLIST CLASS:
Code:
class Playlist {
private String name;
private List<Song> songs;

public Playlist(String name) {


this.name = name;
songs = new ArrayList<>();
}

public void addSong(Song song) {


songs.add(song);
}

public List<Song> getSongs() {


return songs;
}

public String getName() {


return name;
}
}

 This class represents a playlist containing a list of songs.


 It has two private member variables “name” and “songs”
 (a list of “Song” objects).
 The constructor initializes the playlist's name and creates an empty
list for songs.
 “addSong()” adds a song to the playlist.
 “getSongs()” returns the list of songs in the playlist.
 “getName()” returns the name of the playlist.

MUSICPLAYER CLASS:
Code:
public class MusicPlayer extends JFrame implements
ActionListener {
private JComboBox<String> playlistComboBox;
private JComboBox<String> songComboBox;
private List<Playlist> playlists;

public MusicPlayer(List<Playlist> playlists) {


super("Music Player");
this.playlists = playlists;

JPanel panel = new JPanel();


JLabel playlistLabel = new JLabel("Choose Playlist:");
playlistComboBox = new JComboBox<>();
for (Playlist playlist : playlists) {
playlistComboBox.addItem(playlist.getName());
}
playlistComboBox.addActionListener(this);

JLabel songLabel = new JLabel("Choose Song:");


songComboBox = new JComboBox<>();
updateSongComboBox(0);

JButton playButton = new JButton("Play");


playButton.addActionListener(this);

panel.add(playlistLabel);
panel.add(playlistComboBox);
panel.add(songLabel);
panel.add(songComboBox);
panel.add(playButton);

add(panel);
setSize(300, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

 The “MusicPlayer” class is a Java Swing application that serves as a


simple music player GUI.
 This “MusicPlayer” class encapsulates the functionality of a basic
music player GUI application, allowing users to select playlists and
songs and play them using Swing components.
 Let's break down the class along with its methods and components:
1)
public class MusicPlayer extends JFrame implements ActionListener {
 This line declares the class MusicPlayer, which extends JFrame and
implements ActionListener.
 This means that the class represents a windowed GUI application
and can handle action events such as button clicks.

2)
private JComboBox<String> playlistComboBox;
private JComboBox<String> songComboBox;
private List<Playlist> playlists;

These are the class member variables:


“playlistComboBox”: A combo box for selecting playlists.
“songComboBox”: A combo box for selecting songs within a playlist.
“playlists”: A list containing playlists.

3)
public MusicPlayer(List<Playlist> playlists) {
super("Music Player");
this.playlists = playlists;
JPanel panel = new JPanel();
JLabel playlistLabel = new JLabel("Choose Playlist:");
playlistComboBox = new JComboBox<>();
for (Playlist playlist : playlists) {
playlistComboBox.addItem(playlist.getName());
}
playlistComboBox.addActionListener(this);

JLabel songLabel = new JLabel("Choose Song:");


songComboBox = new JComboBox<>();
updateSongComboBox(0);

JButton playButton = new JButton("Play");


playButton.addActionListener(this);

panel.add(playlistLabel);
panel.add(playlistComboBox);
panel.add(songLabel);
panel.add(songComboBox);
panel.add(playButton);

add(panel);
setSize(300, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

 This is the constructor of the “MusicPlayer” class. It initializes the


GUI components:
 Creates a panel (“JPanel”) to hold the UI components.
 Adds a label (“JLabel”) for selecting playlists.
 Creates and populates the “playlistComboBox” with the names of
available playlists.
 Adds an action listener to the “playlistComboBox” to handle
selection changes.
 Adds a label for selecting songs.
 Creates an empty “songComboBox”.
 Initializes the song combo box with the songs of the first playlist.
 Adds an action listener to the "Play" button.
 Adds all components to the panel.
 Sets the size of the frame and sets it to be visible.

4)

private void updateSongComboBox(int index) {


songComboBox.removeAllItems();
for (Song song : playlists.get(index).getSongs()) {
songComboBox.addItem(song.getName());
}
}
This method updates the song combo box based on the selected playlist.
It removes all existing items from the combo box and populates it with the
names of songs from the selected playlist.
5)
public void actionPerformed(ActionEvent e) {
if (e.getSource() == playlistComboBox) {
int index = playlistComboBox.getSelectedIndex();
updateSongComboBox(index);
} else if (e.getActionCommand().equals("Play")) {
int playlistIndex = playlistComboBox.getSelectedIndex();
int songIndex = songComboBox.getSelectedIndex();
Playlist playlist = playlists.get(playlistIndex);
Song song = playlist.getSongs().get(songIndex);
play(song);
}
}

This method is invoked when an action occurs, such as a button click. It


handles two cases:

 If the action is triggered by selecting a playlist from the


“playlistComboBox”, it updates the song combo box
accordingly.
 If the action is triggered by clicking the "Play" button, it
retrieves the selected song and plays it.

6)
// Method overloading for playing by song name
public void play(String songName) {
for (Playlist playlist : playlists) {
for (Song song : playlist.getSongs()) {
if (song.getName().equals(songName)) {
play(song);
return;
}
}
}
JOptionPane.showMessageDialog(this, "Song not found", "Error",
JOptionPane.ERROR_MESSAGE);
}

This method provides an overloaded version of the “play()” method that


accepts a song name as input. It searches for the song with the given name
across all playlists and plays it if found.If not found, it displays an error
message.
7)
private void play(Song song) {
JOptionPane.showMessageDialog(this, "Now playing: " +
song.getName() + " by " + song.getArtist());
}

This method displays a message dialog indicating that a particular song is


now playing.

“MAIN” CLASS :
public static void main(String[] args)
{
Song song1 = new Song("Song 1", "Artist 1");
Song song2 = new Song("Song 2", "Artist 2");
Song song3 = new Song("Song 3", "Artist 3");
Song song4 = new Song("Song 4", "Artist 4");
Playlist playlist1 = new Playlist("Playlist 1");
playlist1.addSong(song1);
playlist1.addSong(song2);
Playlist playlist2 = new Playlist("Playlist 2");
playlist2.addSong(song3);
playlist2.addSong(song4);
List<Playlist> playlists = new ArrayList<>();
playlists.add(playlist1);
playlists.add(playlist2);
SwingUtilities.invokeLater(() -> new MusicPlayer(playlists));
}
}

a) The “main” method is a special method in Java that serves as the


starting point for the execution of the program. It takes an array of
strings as input arguments.
b) Four “Song” objects are created, each representing a song with a
name and an artist.These songs are:
"Song 1" by "Artist 1"
"Song 2" by "Artist 2"
"Song 3" by "Artist 3"
"Song 4" by "Artist 4"
c) Two “Playlist” objects are created, each representing a playlist.
Songs are added to each playlist:

“playlist1” contains "Song 1" and "Song 2".


“playlist2” contains "Song 3" and "Song 4".
d) A “List” of playlists is created using the “ArrayList” class. Both
playlists (“playlist1” and “playlist2”) are added to this list.
e) The “invokeLater()” method of “SwingUtilities” is called to ensure
that the creation and initialization of the “MusicPlayer” object
(which involves creating the GUI components) are performed on
the Event Dispatch Thread (EDT), which is the thread responsible
for handling GUI events in Swing applications. This ensures that the
GUI components are created and updated safely.
f) A lambda expression (->) is used to create a new instance of the
“MusicPlayer” class, passing the list of playlists as a parameter. This
instance will create and display the music player GUI.

CONCLUSION:
In conclusion, I have succeeded in completing the java project of
implementing a Music Player Application. Throughout the development
process, I have achieved several key objectives which include GUI
implementation,management of the playlists and songs, event handling
and error handling.Even though I faced difficulties in understanding the
Swing GUI complexity, I have successfully achieved the desired output
and finished this project on time. I am very thankful for Mr.Santosh
Kumar for giving me this great opportunity to demonstrate my skills and
implement the things that I have learned in Java class.

You might also like