You are on page 1of 27

Music Player – An Android App

Music Player for Android


A PROJECT REPORT
Submitted by
Avishek Gupta(Roll No: 19401214003)
Kundan Kumar(Roll No: 19401214006)
Nivesh Parashar (Roll No: 19401214009)
Rajesh Ray (Roll No: 19401214013)

in fulfillment for the award of the degree


of
BCA
from
INSTITUTE OF MANAGEMENT STUDY

Affiliated to

at
ARDENT Computech Pvt. Ltd.
Music Player – An Android App

BONAFIDE CERTIFICATE

Certified that this project report on ‘Music Player’ in Android is the bonafide work
of Avishek Gupta, Kundan Kumar, Nivesh Parashar and Rajesh Ray who
carried out the project work under my supervision.

Avishek Gupta
Kundan Kumar
Nivesh Parashar
Rajesh Ray

Signature of H.O.D. Signature of Mentor

___________________ ______________________
Dr. Tapoash Saha Prof. Jaydeep Mondal
Institute of Management Study, Ardent Computech Pvt. Ltd.
Mukundapur, Kolkata Salt Lake, Kolkata

2
Music Player – An Android App

ACKNOWLEDGEMENT

The project “Music Player”would not have been possible without the constant guidance of our
guide Prof. Chandan Jana, Ardent Computech Pvt. Ltd. ,who guided us throughout this process
despite his busy schedule. We are immensely thankful to him for his valuable ideas on
improvement of the project.

We are also grateful to Prof. Dr. Tapoash Saha, our HOD, for all the help she provided. The
brainstorming sessions that we had during the project development process, led to serious
arguments and disagreements, but only for the betterment of the project.

Avishek Gupta(Roll No: 19401214003)


Kundan Kumar(Roll No: 19401214006)
Nivesh Parashar (Roll No: 19401214009)
Rajesh Ray (Roll No: 19401214013)

BCA,
Institute of Management Study,
3rd Year,5thSemester, 2016

3
Music Player – An Android App

CONTENTS

NO TOPICS PAGE NO

1. Introduction 5
2. Music Player 5-6
 Description 5
 Scope of project 6
 Implementation 6
3. Basic Concepts and Tools 7-11
4. Component Diagram 12
 E-R Diagram 13
 Data Flow Diagram 13

5. Test Result 14

6. Future Scope 15

7. Code snippets from the implementation 16-28


 Playlist.java 16-18
 Player.java 18-26
 Utilities.java 27-29

8. Conclusion 29
9. References 29
10. Bibliography 30

4
Music Player – An Android App

§1. INTRODUCTION

‘Music Player’ is an Android utility app based on music or songs. We have developed this app in
Android using Android Studio (version 2.1). The minimum requirements for this app are a
Smartphone having Android (version - Ice Cream sandwich or later). We can further improve this
project by adding more interesting and user-friendly features to it. Introduction is just the first
step towards what is yet to be offer in the music player. Music is something which is like a part of
our life and we should keep it as natural as possible because it is nature’s way to interact, connect,
in most efficient way to deliver what is yet to be discovered through our music player which is
rigged with feature and much more yet to be implemented
In coming tomorrow. Because this just the begging with only and only progress in the mind and
satisfaction of the user the priority.

5
Music Player – An Android App

§2. Music Player– A Music Player App

Description
Music Player is a little, minimal features, music player program with following simple
functionalities:

 Play, Stop, Pause, Resume music playing.


 Playing Next, Previous, song control options.
 Shuffle and Repeat All features (playlist modes).
 Volume control setting.
 Time progress bar view, control and current elapsed time displaying.

Additional features:

 Play music file(s), to play music file(s) instead of an entire folder.


Display playlist:Display a window displaying the folder content or the file playlist.
The current playing music file is marked with an icon in the marging. You can double-
click on a file to play it.
 Timeline progress bar: displaying the progress of the music file reading, which you can
drag the slider from to change the current position of the music file.
 Timeline progress bar: displaying the progress of the music file reading, which you can
drag the slider from to change the current position of the music file.
 Elapsed time of the current music file reading displaying.

Supported file types:

Music Player support following music files formats:

The most common music file types:

 mp3 (MPEG-1 Audio Layer III or MPEG-2 Audio Layer III)

Scope of the Project

This project can be further improved with more features in the near future. Features like more file
types, Visualizer, Rating, Albums and etc.

Implementation

6
Music Player – An Android App

In Android Studio, we have implemented our app named ‘Music Player’ that will play music from
different storage location.

§3. BASIC CONCEPTS AND TOOLS

Introduction to Java

Java is a programming language created by James Gosling from Sun Microsystems (Sun) in 1991.
The first publicly available version of Java (Java 1.0) was released in 1995. Sun Microsystems
was acquired by the Oracle Corporation in 2010. Over time new enhanced versions of Java have
been released. The current version of Java is Java 1.7 which is also known as Java 7. From the
Java programming language, the Java platform evolved. The Java platform allows software
developers to write program code in other languages than the Java programming language and
still runs on the Java virtual machine. The Java platform is usually associated with the Java
virtual machine and the Java core libraries.

Java Virtual machine

The Java virtual machine (JVM) is a software implementation of a computer that executes
programs like a real machine. The Java virtual machine is written specifically for a specific
operating system, e.g. for Linux a special implementation is required as well as for Windows.

Java Runtime Environment vs. Java Development Kit

A Java distribution comes typically in two flavors, the Java Runtime Environment (JRE) and
the Java Development Kit (JDK). The Java runtime environment (JRE) consists of the JVM and
the Java class libraries and contains the necessary functionality to start Java programs. The JDK
contains in addition the development tools necessary to create Java programs. The JDK consists
therefore of a Java compiler, the Java virtual machine, and the Java class libraries.

7
Music Player – An Android App

Characteristics of Java

The target of Java is to write a program once and then run this program on multiple operating
systems.
Java has the following properties:

Platform independent: Java programs use the Java virtual machine as abstraction and do not
access the operating system directly. This makes Java programs highly portable. A Java program
(which is standard complaint and follows certain rules) can run unmodified on all supported
platforms, e.g. Windows or Linux.

Object-orientated programming language: Except the primitive data types, all elements in
Java are objects.

Strongly-typed programming language: Java is strongly-typed, e.g. the types of the used
variables must be pre-defined and conversion to other objects is relatively strict, e.g. must be
done in most cases by the programmer.

Interpreted and compiled language: Java source code is transferred into the byte code format
which does not depend on the target platform. These byte code instructions will be interpreted
by the Java Virtual machine (JVM). The JVM contains a so called Hotspot-Compiler which
translates performance critical byte code instructions into native code instructions.

Automatic memory management: Java manages the memory allocation and de-allocation for
creating new objects. The program does not have direct access to the memory. The so-called
garbage collector deletes automatically objects to which no active pointer exists.

Android
Android is a software platform and operating system for mobile devices. Android is available as
open source. It allows developers to write managed code in the Java language, controlling the
device via Google-developed Java libraries.
Android SDK was released by Open Handset Alliance in the month of November of the year
2007. Android was actually developed using the kernel of Linux 2.6 and the highlighting features
of Android include the following:
• No fees for licensing, distribution and release approval
• GSM, 3G EDGE networks for telephony
• IPC message passing

8
Music Player – An Android App

• Background processes and applications


• Shared data stores
• Complete multimedia hardware control
• API’s for location based services such as GPS.

Architecture of Android OS
The skeleton of Android framework and its constituents are shown in the following figure:

Figure: Architecture of Android OS

Applications Layer
Android ships with a set of core applications including an email client, SMS program, calendar,
maps, browser, contacts and others. All applications are built using the Java. Each of the
applications aims at performing a specific task that it is actually intended to do.

Application Framework Layer


The next layer is the application framework. This includes the programs that manage the phone’s
basic functions like resource allocation, telephone applications, switching between processes or
programs and keeping track of the phone's physical location. Application developers have full
access to Android's application framework. This allows them to take advantage of Android's
processing capabilities and support features when building an Android application. We can think

9
Music Player – An Android App

of the application framework as a set of basic tools with which a developer can build much more
complex tools.

Libraries Layer
The next layer contains the native libraries of Android. These shared libraries are all written in C
or C++, compiled for the particular hardware architecture used by the phone and preinstalled by
the phone vendor.
Android Runtime Layer
Android Runtime layer includes Dalvik Virtual Machine (DVM) and a set of core java libraries.
Every Android app gets its own instance of DVM. Dalvik has been written so that a device can
run multiple virtual machines efficiently and it executes files with. dex (Dalvik Executable
Format) extension optimized for minimum memory.

Components of Android
The basic components of an Android application include Activity, Broadcast Receiver, Service,
and Content Provider. Each of the above, which when used for any application, has to be declared
in the AndroidManifest.xml. The user interface of the component is determined by the Views. For
the communication among these basic components we use Intents and Intent filters which play
crucial role during app development.

Android
Applicatio
n
Broadcast Content
Activity Service
Receiver Provider
Figure: Android Components
Activity
An Activity is, fundamentally, an object that has a lifecycle. An Activity is a chunk of code that
does some work, as necessary. The work can include displaying a UI to the user, though it doesn't
have to as some Activities never display UIs. Typically, we designate one of our application's
Activities as the entry point to our application.

10
Music Player – An Android App

Broadcast Receiver
Broadcast Receiver is yet another type of component that can receive and respond to any of the
broadcast announcements.
Service
A Service is a body of code that runs in the background. It can run in its own process, or in the
context of another application's process, depending on its needs. Other components "bind" to a
Service and invoke methods on it via remote procedure calls. An example of a Service is a media
player; even when the user quits the media-selection UI, she probably still intends for her music
to keep playing. A Service keeps the music going even when the UI has completed.
Content Provider
Content Provider is a data storehouse that provides access to data on the device; the classic
example is the Content Provider that is used to access the user's list of contacts. Our application
can access data that other applications have exposed via a Content Provider, and we can also
define our own Content Providers to expose data of our own.

11
Music Player – An Android App

§4. Component Diagram

 E-R Diagram of Music Player

 Data Flow Diagram for Music Player


 Level - 0 DFD

12
Music Player – An Android App

 Level - 1 DFD

13
Music Player – An Android App

§5. TEST RESULTS

Menu Screen Playlist Screen

14
Music Player – An Android App

Player Screen

§6. FUTURE SCOPE

The future scope of Music Player is limitless. How? Allow me tell you the ongoing innovations in
Music Player: -

 The future scope of the player is as unique as the way it is.


 It is like one would love to have to have the player on their smart phone
 Its just the beginning of the future yet there is more to come.
 Its as efficient as any other player available on google play store.

15
Music Player – An Android App

 The main scope of the player is to deliver the maximum user satisfaction which the
user so richly deserver.
 Scope is just the prospective that we hold as its highly like to only move forward
in future with progress as it is the only scope of the coming future is the future
scope of the project.
 Change is the natures way to progress in the future with the scope of progress,
satisfaction, upgradation and revolution as this is only the future destination.
 Development is a way to endure ones to stay in the race. In the prestnt existing
market condition.
 Development is the key to be in the pace and it always will be . Because sooner or
later its yet to be rigged with more feature like visualizer, equalizer , widget which
will not only enhance your experience of music better than before but redefine it
in order to offer the pure experience without any interruption and it’s the begging
towards the search of more to accomplish more and more .In the distinct future.

§7. Code snippets from the implementation


 Playlist.java :

package com.example.esu.musicplayer;

import android.content.Intent;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.io.File;
import java.util.ArrayList;

public class Playlist extends AppCompatActivity {

ListView listView;
String[] songName; //Store song name in songName

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_playlist);

listView=(ListView)findViewById(R.id.listView);

final ArrayList<File> mySongs=findSong(Environment.getExternalStorageDirectory());


//Contain all the music files name or list

16
Music Player – An Android App

songName=new String[(mySongs.size())];

for(int i=0;i<mySongs.size();i++){
songName[i]=mySongs.get(i).getName().toString().replace(".mp3","");
}

ArrayAdapter<String> adp=new
ArrayAdapter<String>(getApplicationContext(),R.layout.songname_layout,R.id.textView,songN
ame);
listView.setAdapter(adp);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
startActivity(new Intent(getApplicationContext(),Player.class)
.putExtra("POSITION",i)
.putExtra("SONGLIST",mySongs)
);
}
});

public ArrayList<File> findSong(File root){


ArrayList<File> arrayList=new ArrayList<File>(); //For storing the name of the file that is
in mp3
File[] files=root.listFiles(); //Every types of files and folder

for(File singleFile : files){


if(singleFile.isDirectory() && !singleFile.isHidden()){
arrayList.addAll(findSong(singleFile)); //Recursive call of a function that find single
file under the directory
}
else {
if(singleFile.getName().endsWith(".mp3")){
arrayList.add(singleFile); //Add the music file in arraylist
}
}
}
return (arrayList);
}
}

17
Music Player – An Android App

 Player.java :

package com.example.esu.musicplayer;

import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;

public class Player extends AppCompatActivity implements View.OnClickListener{

private static MediaPlayer mediaPlayer;


private ArrayList<File> mySongs;
int position=0;
private Uri uri;

private SeekBar seekBar;


private ImageButton buttonPlay,buttonNext,buttonPre,buttonFF,buttonFB;
private TextView songCurrentTime,songTotalTime,musicName;

private int seekForwardTime = 5000; // 5000 milliseconds


private int seekBackwardTime = 5000; // 5000 milliseconds

private int currentPosition;

// Handler to update UI timer, progress bar etc,.


private Handler mHandler = new Handler();
private Utilities utils;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);

seekBar=(SeekBar)findViewById(R.id.seekBar);

18
Music Player – An Android App

buttonPlay= (ImageButton) findViewById(R.id.btnPlay);


buttonNext= (ImageButton) findViewById(R.id.btnNext);
buttonPre= (ImageButton) findViewById(R.id.btnPrevious);
buttonFB= (ImageButton) findViewById(R.id.btnBackword);
buttonFF= (ImageButton) findViewById(R.id.btnForword);

songCurrentTime=(TextView)findViewById(R.id.songCurrentTime);
songTotalTime=(TextView)findViewById(R.id.songTotalTime);
musicName=(TextView)findViewById(R.id.SongTitle);

buttonPlay.setOnClickListener(this);
buttonNext.setOnClickListener(this);
buttonPre.setOnClickListener(this);
buttonFB.setOnClickListener(this);
buttonFF.setOnClickListener(this);

//Create an object of Utilities Java class and


utils = new Utilities();

//If media player is running the stop it


if(mediaPlayer!=null){
mediaPlayer.stop();
mediaPlayer.release();
}

Intent intent=getIntent();

Bundle bundle=intent.getExtras();

mySongs=(ArrayList) bundle.getParcelableArrayList("SONGLIST");
position=bundle.getInt("POSITION",0);

uri=Uri.parse(mySongs.get(position).toString() );
mediaPlayer=MediaPlayer.create(getApplicationContext(),uri);
mediaPlayer.start();

//Update seekBar postion


updateProgressBar();

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
/**
* When current song get finish then change to next song
* */
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if(songCurrentTime.getText().equals(songTotalTime.getText())){
//Stop Media Player and Release
mediaPlayer.stop();
mediaPlayer.release();

//Change the picture of btnPlay Button


19
Music Player – An Android App

buttonPlay.setImageResource(R.drawable.btn_pause);

position=(position+1) % mySongs.size();
uri=Uri.parse(mySongs.get(position).toString() );
mediaPlayer=MediaPlayer.create(getApplicationContext(),uri);
mediaPlayer.start();

musicTitle();
}
}
/**
* When user starts moving the progress handler
* */
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// remove message Handler from updating progress bar
mHandler.removeCallbacks(mUpdateTimeTask);
}
/**
* When user stops moving the progress hanlder
* */
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//mediaPlayer.seekTo(seekBar.getProgress());

mHandler.removeCallbacks(mUpdateTimeTask);
int totalDuration = mediaPlayer.getDuration();
int currentPosition = utils.progressToTimer(seekBar.getProgress(), totalDuration);

// forward or backward to certain seconds


mediaPlayer.seekTo(currentPosition);

// update timer progress again


updateProgressBar();
}
});

musicTitle();
}

/**
* Update timer on seekbar
* */
public void updateProgressBar() {
mHandler.postDelayed(mUpdateTimeTask, 100);
}

/**
* Background Runnable thread
* */
private Runnable mUpdateTimeTask = new Runnable() {
20
Music Player – An Android App

public void run() {


try {
long totalDuration = mediaPlayer.getDuration();
long currentDuration = mediaPlayer.getCurrentPosition();

// Displaying Total Duration time


songTotalTime.setText("" + utils.milliSecondsToTimer(totalDuration));
// Displaying time completed playing
songCurrentTime.setText("" + utils.milliSecondsToTimer(currentDuration));

// Updating progress bar


int progress = (int) (utils.getProgressPercentage(currentDuration, totalDuration));
//Log.d("Progress", ""+progress);
seekBar.setProgress(progress);

// Running this thread after 100 milliseconds


mHandler.postDelayed(this, 100);
} catch(IllegalStateException ex){
ex.printStackTrace();
}
}
};

/**
* Music Title Method
*/
private void musicTitle(){
StringBuffer stringBuffer=new StringBuffer(uri.toString());
int lastSlashPosition=stringBuffer.lastIndexOf("/") + 1;
stringBuffer.replace(0,lastSlashPosition,"");
musicName.setText(stringBuffer.toString().replace(".mp3",""));
}

/**
* Player Buttton coding and logics
* @param view
*/
@Override
public void onClick(View view) {
int id=view.getId();
switch (id){
/**
* Play button click event
* plays a song and changes button to pause image
* pauses a song and changes button to play image
* */
case R.id.btnPlay:
if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
buttonPlay.setImageResource(R.drawable.btn_play);
}
21
Music Player – An Android App

else{
mediaPlayer.start();
buttonPlay.setImageResource(R.drawable.btn_pause);
}
break;
/**
* Next button click event
* */
case R.id.btnNext:
//Stop Media Player and Release
mediaPlayer.stop();
mediaPlayer.release();

//Change the picture of btnPlay Button


buttonPlay.setImageResource(R.drawable.btn_pause);

position=(position+1) % mySongs.size();
uri=Uri.parse(mySongs.get(position).toString() );
mediaPlayer=MediaPlayer.create(getApplicationContext(),uri);
mediaPlayer.start();

musicTitle();

break;
/**
* Previous button click event
* */
case R.id.btnPrevious:
//Stop Media Player and Release
mediaPlayer.stop();
mediaPlayer.release();

//Change the picture of btnPlay Button


buttonPlay.setImageResource(R.drawable.btn_pause);

position=(position-1<0)? mySongs.size()-1 : position-1;


uri=Uri.parse(mySongs.get(position).toString() );
mediaPlayer=MediaPlayer.create(getApplicationContext(),uri);
mediaPlayer.start();

musicTitle();
break;
/**
* Forward button click event
* Forwards song specified seconds
* */
case R.id.btnForword:
// get current song position
currentPosition = mediaPlayer.getCurrentPosition();
// check if seekForward time is lesser than song duration
if(currentPosition + seekForwardTime <= mediaPlayer.getDuration()){
22
Music Player – An Android App

// forward song
mediaPlayer.seekTo(currentPosition + seekForwardTime);
}else{
// forward to end position
mediaPlayer.seekTo(mediaPlayer.getDuration());
}
break;
/**
* Backward button click event
* Backward song to specified seconds
* */
case R.id.btnBackword:
// get current song position
currentPosition = mediaPlayer.getCurrentPosition();
// check if seekBackward time is greater than 0 sec
if(currentPosition - seekBackwardTime >= 0){
// forward song
mediaPlayer.seekTo(currentPosition - seekBackwardTime);
}else{
// backward to starting position
mediaPlayer.seekTo(0);
}
break;
}
}
}

 Utilities.java :

package com.example.esu.musicplayer;

public class Utilities {

/**
* Function to convert milliseconds time to
* Timer Format
* Hours:Minutes:Seconds
* */
public String milliSecondsToTimer(long milliseconds){
String finalTimerString = "";
String secondsString = "";

// Convert total duration into time


int hours = (int)( milliseconds / (1000*60*60));
int minutes = (int)(milliseconds % (1000*60*60)) / (1000*60);
int seconds = (int) ((milliseconds % (1000*60*60)) % (1000*60) / 1000);
// Add hours if there
if(hours > 0){
finalTimerString = hours + ":";

23
Music Player – An Android App

// Prepending 0 to seconds if it is one digit


if(seconds < 10){
secondsString = "0" + seconds;
}else{
secondsString = "" + seconds;}

finalTimerString = finalTimerString + minutes + ":" + secondsString;

// return timer string


return finalTimerString;
}

/**
* Function to get Progress percentage
* @param currentDuration
* @param totalDuration
* */
public int getProgressPercentage(long currentDuration, long totalDuration){
Double percentage = (double) 0;

long currentSeconds = (int) (currentDuration / 1000);


long totalSeconds = (int) (totalDuration / 1000);

// calculating percentage
percentage =(((double)currentSeconds)/totalSeconds)*100;

// return percentage
return percentage.intValue();
}

/**
* Function to change progress to timer
* @param progress -
* @param totalDuration
* returns current duration in milliseconds
* */
public int progressToTimer(int progress, int totalDuration) {
int currentDuration = 0;
totalDuration = (int) (totalDuration / 1000);
currentDuration = (int) ((((double)progress) / 100) * totalDuration);

// return current duration in milliseconds


return currentDuration * 1000;
}
}

24
Music Player – An Android App

§8. CONCLUSION

We have developed this app, keeping in mind that our app will redefine the experience for
the users seeking for because it’s something to saddle for . This app especially developed
for only one purpose is to deliver the pious nature of music which the user so richly
deserves to have to have in any given point of time in any given situation. Users can
access to their favorite songs from the playlist option anytime because we believe in
simplicity. User don’t have to trouble themselves to update their newly added songs
because it reads songs from both the internal and sd-card which is again one the key
features. This app can be further carried out with more features like visualizer, equalizer
and widgets, and many more. Probably alredy by now you know that it’s just the begging
with only conclusion to carve for more and in the coming future .

25
Music Player – An Android App

§9. REFERENCES

1. Java-The-Complete-Reference-7th-Edition-by Herbert Schildt

26
Music Player – An Android App

§10. BIBLIOGRAPHY

 www.wikipedia.com
 www.javatpoint.com/android-tutorial
 www.tutorialspoint.com/android/
 http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/

27

You might also like