Design a music player with shuffle functionality

Prakhar Gurawa
4 min readJan 19, 2020

--

Photo by Mick Haupt on Unsplash
"What is the most efficient way to implement a shuffle function in a music player to play random songs without repetition"

We have to design a music player with the following functionalities:

  1. Add a song to the music player.
  2. Play a random song from music player such those same songs are not repeated consecutively.
  3. Play a particular song.

First, we have to define a structure to store the songs. Each song will have attributes like Song ID, Song name, Singer and Genre. Although there can be other attributes that can be included and can be used to sort and search the songs later according to user needs.

To implement the music player we will use two data structures: Linked List [ArrayList in Java]and a queue. We will create a new class “MusicPlayer” which includes the appropriate variable and structures for this task.

Here music list will contain the list of all the songs present in our music player. A separate queue musicQ will act as the song queue for our system.

The variable total_Song_Count will represent the section of songs that will be considered for random song search. Once a song is found by a random song search, it will be removed from the list and added at the end of the list to prevent the repetition of the song.

A simple function will be used to add songs on our music list. This function adds a new song at the particular index at total_Song_Count.

The index total_Song_Count will bifurcate the ArrayList into two groups. The first one will contain song which will be considered for random shuffle music and the second one will not be considered. All new songs are added to the first group.

To play a random song, we will generate a random number between 0 to total_Song_Count -1. Then we remove the song at that index from the list and then add to the music queue. We will add the song at the ending of arraylist. Also, we will decrement the total_Song_Count, so that the group containing songs for random shuffle music decreases.

To play a song of a particular song ID we will keep track whether that song belongs to the first group or second. If it is of the first group, we will remove it from the first group and add it to the second. For this, we can use a function playSong which has additional variable addtoQ, which gives the functionality to either add in musicQ or play at that moment.

We can create a function to close the music player which will empty the musicQ and will dissolve the second group to the first group.

A function to play from the queue can be used, which plays songs from the queue. This function also checks the content of the queue and starts new song whenever a song ends if the queue is not empty.

Finally, the main function to call the function of the above-implemented functions from MusicPlayer class.

The code is available at: https://gist.github.com/prakhargurawa/979c333104a3e98bda12bdedd0ac00dd

There can be multiple other functionalities that can be added to our system:

  1. Search/Sort/Manage songs according to the genre of songs.
  2. Apply machine learning techniques to create a playlist according to user mood.
  3. Add more features to a song like a date, mood, user listening count which can help us to find user liking.

Any modification/update will be highly appreciated. Thank you.

--

--

Prakhar Gurawa

AI Engineer| Learner | Caricaturist | Omnivorous | DC Fanboy