top of page

Media Player Playlist in Java code

Hi All,
In this post, we will look at code and running screen shots of a Media Player project I completed in Java, the purpose of the code is to Add, Delete, Change position of tracks, play tracks sequentially or play tracks randomly. For ease, I have included a Driver class which will showcase the ability of the program.

Adding tracks to a playlist:

 


​public class Track
{

private String title ; //Creates a private string called "title"​

private String artist ; //Creates a private string called "artist"
private int duration ; // Creates a private integer called "duration"
public Track(String title, String artist, int duration)
//This is the class that adds a track
{
this.title = title ;
this.artist = artist ;
this.duration = duration ;
}
public String getTitle() //this gets the title of a track
{
return title ;
}
public String getArtist() // this gets the artist of the track added
{
return artist ;
}
public String getduration() // this gets the duration of the track
{
long h, m, s ; // this creates 3 long variable h = hours, m = mins, s = secs
String durationString; // Creates a string that will be used for the duration
s = duration; //Duration of track
m = s / 60 ; // Duration in minutes
s = s % 60 ; // Duration in seconds
h = m / 60 ; // Duration in hours
m = m % 60 ; //Minutes
durationString = String.format("%02d",h) + ":" +
String.format("%02d",m) + ":" +
String.format("%02d",s) ;
//Above is where the duration is formatted into a specific layout for returning.
return durationString ;
}
public void setDuration(int h, int m, int s) // User sets duration of track
{
duration = (h*3600 + m*60 + s)  ;
// as java works in seconds, math needs to be done to get the duration in hours and minute
if(h == 0){
duration = (m*60 + s);
}
}
public void setTitle(String title)
{
title = title ;
}
public void setArtist(String artist)
{
artist = artist;
}
public String toString()
{
return  "Title: " + this.title + "\nArtist: " + this.artist + "\nDuration: " + getduration()  ;
}

 

}

​See Driver class below to find out how to use this class.
​
Playlist class:

​​public class PlayList
//Creates a playlist
{
private Track[] list ;
private Track[] tempArray ;
private Track[] played ;
private int[] played2 ;
private int entryCount ;
public PlayList(int listCapacity)
{
this.list = new Track[listCapacity] ;
this.entryCount = 0;
}

Insert Method:

public void insert(String title, String artist, int duration)
{
int pos = 0 ;
if(pos <= this.entryCount) {
if(this.entryCount == this.list.length){
int i ;
Track[] bigger = new Track[this.list.length + 5] ;
for(i=0 ; i < this.list.length ; i++) {
bigger[i] = this.list[i] ;
}
this.list = bigger ;
}
this.list[this.entryCount] = new Track(title,artist,duration) ;
this.entryCount++ ;
pos ++ ;
}
}
private int indexOf(String title,String artist )
{
int i ;
for(i = 0 ; i < this.entryCount && this.list[i].getTitle().compareToIgnoreCase(title) != 0 && this.list[i].getArtist().compareToIgnoreCase(artist) != 0; i++) ;
if(i < this.entryCount) {
return i ;
} else {
return -1 ;
}
}

Delete Method:

public void remove(String title, String artist)
{
int i ;
int pos = indexOf(title,artist) ;
if(pos != -1) {
if(pos == entryCount-1) {
this.list[entryCount-1] = null ;
moveUp(pos+1) ;
} else {
for(i = pos ; i < entryCount-1 ; i++) {
this.list[i] = this.list[i+1] ;
}
}
}
entryCount-- ;
}

Display Method:

public void display()
{
int i ;
for(i = 0 ; i < this.entryCount ; i++) {
System.out.println(this.list[i].toString() + "\n") ;
}
}

Moving a track up in the list method:

public void moveUp(int fromPos)
{
int i = fromPos ;
this.tempArray = new Track[1] ;
if(fromPos <= this.list.length ){
if(fromPos == 0){
this.list[fromPos] = this.list[fromPos] ;
}else{
this.tempArray[0] = this.list[i-1] ;
this.list[i-1] = this.list[i] ;
this.list[i] = this.tempArray[0] ;
}
}
}

Moving a track down in the list method:

public void moveDown(int fromPos)
{
int i = fromPos ;
this.tempArray = new Track[1] ;
if(fromPos <= this.list.length){
if(fromPos == this.list.length -1){
this.list[fromPos] = this.list[fromPos] ;
}else{
this.tempArray[0] = this.list[i+1] ;
this.list[i+1] = this.list[i] ;
this.list[i] = this.tempArray[0] ;
}
}
}
}

Driver Class:

public class Driver
{
public static void main(String[] args)
{
PlayList Tracks = new PlayList(5) ;
Tracks.insert("Viva La Vida","ColdPlay",246);
Tracks.insert("Not Afraid","Eminem",250);
Tracks.insert("When I Am Gone","Eminem",282);
Tracks.insert("Someone I used to know","Goyte",244);
Tracks.insert("Mirror","Lil Wayne FT Bruno Mars",242);
Tracks.display() ;

Result of the above chunk:

​System.out.println("\n");
Tracks.remove("Viva La Vida","ColdPlay") ;
System.out.println("After Deleting 'Viva La Vida','ColdPlay'....\n") ;
Tracks.display() ;

Result of the above chunk:

System.out.println("\n");
Tracks.moveUp(1) ;
System.out.println("After Moving Track at position 1 up.....\n");
Tracks.display() ;

Result of above chunk:

System.out.println("\n");
Tracks.moveDown(0) ;
System.out.println("After Moving Track at position 0 down.....\n");
Tracks.display() ;

Result of above chunk:


 

​​System.out.println("\n");
System.out.println("\n");
System.out.println("END OF DRIVER");
System.out.println("\n");
}
}
​
So there you have it, the code in this post was created for a project and is basic. If any reader has questions about this post/code, feel free to drop me an email and I will be glad to help.
​
​
bottom of page