Android Building Audio Player Tutorial

In this tutorial i am going to discuss building a simple audio player with basic controls like play, pause, forward, backward, next, previous, playlist and seekbar. This app basically will read all audio files(.mp3) from sdcard and plays selected song. For this tutorial i am referencing MediaPlayer and go through it if you need any documentation about usage.

Android MediaPlayer Class

Android SDK is providing MediaPlayer Class to access android in built mediaplayer services like playing audio, video etc., In this tutorial i am using following functions of this class to control audio player.

MediaPlayer mp = new MediaPlayer();
// Set data source -
setDataSource("/sdcard/path_to_song");
// Play audio
mp.start();
// Pause audio
mp.pause();
// Reset mediaplayer
mp.reset();
// Get song length duration - in milliseconds
mp.getDuration();
// Get current duration - in milliseconds
mp.getCurrentDuration();
// Move song to particular second - used for Forward or Backward
mp.seekTo(positon); // position in milliseconds
// Check if song is playing or not
mp.isPlaying(); // returns true or false

1. Designing the Audio Player Layout

Design your audio player using some graphic designing softwares like photoshop. I used photoshop to design this app layout. If you are not aware of designing just download the required images from the internet. Following is a screenshot of the audio player which we are going to build in this tutorial. (You can find this layout PSD in the download code)

2. Preparing Required Icons and Images

Once you are done with your app layout design, prepare the required icons and background images for the audio player application. Prepare your icons in different states like default, focused and pressed and place them all in your drawable folder.

3. Writing XML layouts for ICON states (default/hover/pressed)

After saving all the icons with different states, we need to write xml drawable for each icon. Following is a sample for play button. Save this file under drawable folder.

5. Writing XML for Player Layout

So far we created separate xml layout for all the icons, seekbar. Now we need to combine everything into single layout. Create a new file called player.xml under layout folder and paste the following code.

By using above layout we can achieve following list view by loading data into it.

7. Writing Class for reading MP3 files from SDcard

So far we are done with static layouts for the player. Now the actual code starts.
Create a new class file and name it as SongsManager.java. This class will read all the files from device sdcard and filters the files which are having .mp3 extension.

9. Helper Class functions

Create a new class called Utilities.java for handling extra work like converting time to progress percentage and vice-versa. Also it has function to convert millisecond to a timer string which will displayed on the seekbar of the player.

7. Writing Classes needed for Audio Player

Open your main activity class which deals with main player interface and make the class implements from OnCompletionListener, SeekBar.OnSeekBarChangeListener.
In this case my main activity name is AndroidBuildingMusicPlayerActivity.

Implementing song onCompletion Listener

It is important to implement this listener which will notify you once the song is completed playing. In this method we need to play next song automatically depending on repeat and shuffle conditions.

/**
* On Song Playing completed
* if repeat is ON play same song again
* if shuffle is ON play random song
* */
@Override
public void onCompletion(MediaPlayer arg0) {
// check for repeat is ON or OFF
if(isRepeat){
// repeat is on play same song again
playSong(currentSongIndex);
} else if(isShuffle){
// shuffle is on - play a random song
Random rand = new Random();
currentSongIndex = rand.nextInt((songsList.size() - 1) - 0 + 1) + 0;
playSong(currentSongIndex);
} else{
// no repeat or shuffle ON - play next song
if(currentSongIndex < (songsList.size() - 1)){
playSong(currentSongIndex + 1);
currentSongIndex = currentSongIndex + 1;
}else{
// play first song
playSong(0);
currentSongIndex = 0;
}
}
}

Update your AndroidManifest.xml

Update your AndroidManifest.xml to following code. Add android:configChanges=”keyboardHidden|orientation” to your main activity node.

Hi there! I am Founder at androidhive and programming enthusiast. My skills includes Android, iOS, PHP, Ruby on Rails and lot more. If you have any idea that you would want me to develop? Let’s talk: ravi@androidhive.info

The method “createAudioExtensionsList()” creates a list of file extensions that you want to find. In your case that would only be mp3 files so you have to remove the other entries.

“getFileExtension(…)” is a simple method to get the extension of a file.

“getFilesInFolder(…)” is a function that calls itself recursively to scan a folder and all its subfolders for the file with the extensions that you give it as parameters. If you want to scan the whole sdcard you just pass the that to the function! (be aware that different Android ROMs might have different naming conventions! There is a method to get the root path of the sd card but I dont remember, so you have to look it up yourself)

kanmane

Hi Mr.bowter…thank u so much fr yr reply..but the Url which u have given is not working..i couldn’t able to connect to that site…can u pls check out n let me know the code..Thank u once again…Waiting for yr reply….

Andi

I already tried it, but I can’t call the function “getFilesInFolder”. Eclipse keep saying ” The method getFilesInFolder(File, ArrayList) in the type SongsManager is not applicable for the arguments (String,
ArrayList)”. Can you please help me? Thank you.

b0wter

see the post above, I’ve posted something that works for me :]

Revoseven

Is there someone who is willing to send source code for android music player that runs smoothly. I would be very grateful to those of you who have helped me. you can get sent to the email: ceo.revoseven @ gmail.com. thanks before

In “Cursor cursor = resolver.query(…..” What is “resolver”? Is it a variable or a constant. And how do I initialize it? Thanks in advance.

John Pavlecich

For anyone having trouble with the IndexOutOfBoundsException, it is because it is not finding any music on your sdcard.

Line 80: playSong(0); throws an indexOutBoundsException when it finds no music (getPlaylist() returns a blank list so 0 is not a proper index).

I fixed it by changing the the directory it starts in so it actually finds music. Here is a code snippet to set the directory as the music folder. Make sure you actually have music there or you’ll still get the error.

The original tutorial probably should have a had check on the size to make sure the playlist really had something in it rather than trying to play the first song off the bat which crashes the program if no music exists.

if(songsList.size() >0){
// By default play first song
playSong(0);
}

John Pavlecich

It is not finding music on your sdcard. In the class AndroidBuildingMusicPlayerActivity, it attempts to playSong(0). If the songsManager finds no music, this will throw that exception. You can either change where it is looking for music or prevent the app from playing the first song. See my post at the top.

your code works like charm. Now I have song array list which came through server and populated in list view.I want to implement my codes with your music player,so that I have been looking through your codes and fount out MEDIA_PATH string which is pointed to sdcard. My question is that how could I change this path to load my songs list array properly ? Thanks in advance !

Sagar

Download song list xml file and parse it search xml parsing tutorial on same website

zen kun

i cant download the code 🙁

zen kun

ok i download the code and testing in my nexus isnt working the folder stuff, and could be a slow task i guess scanning so in song manager i changed this:

in song manger then create sdcard folder in your external storage and add songs in it install and tada!!!!!!!1

Writwick Daz

I am loving AndroidHive because of so descriptive UI designing tutorial.. :thumbsup:
Keep up the good work.. Helped me learn how to design the UI…

Nic

Hi!! Great tutorial.can you explain me how to create a series of playlist and pick one of them to play?

TunerX

Thank You very much. Great help 🙂

danny

hey ! great post but i was not able to login into your download section and download the code. because i never recieved the subscription confirmation email. please cehck . Thanks . Cheers ! 😀

jurds

Hi Ravi,

Thanks for this tutorial, I got it working. I have a concern though, the stack trace displays a lot of logs from the constant checking of getDuration and getCurrentPosition. If however someone uses this for a bigger application, I wanted to stop the checking after I’m done or stopped the media player. If someone has resolved this or will resolve this, please let me know. Thanks a lot.

Cheramis

Where should you put this codes? I am new to android and so would appreciate the help. Thank you.

Cheramis

Would you know where to add the codes stated by MarioB? I’m still learning and don’t know exactly what to look for. Thank you.

viraj

Hey thanks for the great tutorial.I am just a noob and want to learn android programming.I have got a force close issue and i dont know how to deal with this so can anyone please help me out!!

viraj

i got the same kind of problem.if you find any solution please let me know

nesseser

rand.nextInt((songsList.size() – 1) – 0 + 1) + 0 What is it?

nesseser

currentSongIndex = rand.nextInt((songsList.size() – 1) – 0 + 1) + 0;

What is it?

jupiter.at

Hi all. If I need timer to close this app. What is technique I should use???

jupiter.at

I need to close this app with specific time. What is technique I should use?

tanhung

thanks

matlab

Thanks for Great Tutorial.
i have 10 songs in raw folder . how can i show in a listview.
all songs from raw folder in listview.

please tell to me how change “playlist.java” for read songs from raw folder

matlab

Edit…

Thanks for Great Tutorial.
i have 10 songs in raw folder . how can i show in a listview.
all songs from raw folder in listview.

please tell to me how change “SongsManager.java” for read songs from raw folder

in song manger then create sdcard folder in your external storage and add songs in it install and tada!!!!!!!23

TrongDuy

How to download a file mp3 throught a path link on internet in android.Please help me.

arpit

hello, nice one working i want dowanload this code ? how if is it possible so plese send me link or option for download this audio player tutoriacl code

tolen

Hi I want to ask you how to design next/back buttons to get list of items from data base please.

gndp

MEDIA_PATH in SongsManager.java is set to “/sdcard/” which might not be the correct path.
In my case I set it to “/storage/emlated/0/Music” and everything works fine.

It plays the music as soon as it starts so incorrect path leads to Force Close

James

Hey, how do you make it so that the program looks for all the MP3 files in the android device rather than in a directory?

Nido Serius

Sir its very Good Tutorial, but When I stop the song and press the back button of the emulator the application Crashes every time. I Tried many times to correct it by using other methods but it continues Crashing on exit or back button?
Kindly Help me

you are getting error on emulator or phone (android)
try phone for install.
check if the error is same if it is same you must check version of phone if you have very high version some methods may be deprecated like in 4.4

Paul75

Hello, I test in my Samsung Galaxy S3 with the version 4.3 Android.
I have selve the problem in SongsManager : final String MEDIA_PATH = new String(Environment.getExternalStorageDirectory() +”/media/”); —-> NOT final String MEDIA_PATH = new String(“/sdcard/”); ….

But now it works but sometimes the application crach …. not everytimes ….
When I quit it….

If the player is working now means playing songs in your media folder then crashes when you close it with error unfortunately the media player is stopped working this error is because of the thread for the seek bar then you have to do this.

this is not working, its seems to force close so can u please help me to out this
this also not work in real device to.

thanks in advance.

ulyray

Hi Danish, are you still active in this forum? I’ve downloaded the code. But when I tried to build it it says that I have to remove the @override notation on the click event and on the part of array list item. Is there something lacking on the code?

I am still trying to decipher why the hell did you add a zero and then subtract one.
Also, limiting the rand to the range within the list but then adding one after it?

The effect is the same as if you just did rand.nextInt without the “-1” inside… as far as I understand the process.

Hellmouth

“if(mp.isPlaying()){
if(mp!=null){”

err WAT

You call on mp’s method, and THEN you check if mp object is null?

James

hi,

iv downloaded all the code, no errors or warnings, my app appears on my huawei y300 , put once i try to open it, an error message comes up saying “appname” has stopped working?? should it not at least build and show the list view etc? is there something simple i am missing?

plz help.

Khushwant Singh

Hi,

Simply the best.

For those this app is crashing and in case they are running this sample directly on phone, please make sure that your phone is set to “Charge only” and NOT disk drive mode. In case your phone is connected in disk drive mode, the SD card will not be accessible by phone/app and app will crash.

Everything is working correctly but there is only one thing I am having issue with. That is this code is unable to find music from all of the SD Card. It would be great help if someone could help me with it.

Could tell me where is to put the”SD card root folder”. is it in the package inside of any folder or what,

and can you explain me about:

to test this app in your android emulator you need to load your
emulator with some songs. You can send files to emulator sdcard using adb tool which comes with Android SDK.

Navigate to your android SDK folder/platform-tools/ using command line. And using push command you can send files to sdcard. (Start your emulator before performing push command)

platform-tools> adb push “c:SongsWhite Flag.mp3” “/sdcard/”

thank you

putracrishtyan

When I try to download my browser doesn’t download the source code, can you please send me the source code please,thank youputracrishtyan1@gmail.com

Palak Mevada

Helpful after completing the new boston series of android development and taking a step ahead in to droid stuffs….would be working on this from tomorrow,thanx a lot…

Anirudha

mUpdateTimeTask is leaked, Just put a log in the run and you will see. Also pausing the current song and pressing back buttons make the application crash since onDestroy media player is released and calling getDuration on it results in IllegalStateException.

It could be any number of reasons. Any crash in the java code can cause your app to stop. Look in your LogCat in Eclipse for red text. It will give you a call stack and help you find where the code is crashing.

This error is occurring because when playsong(0) is called songsList is empty. Put a check in playsong method where you check “songIndex” is less than size of the songList ArrayList

Mayank Langalia

Anirudha

i have same problem please tell me. any help would be appreciated

Miguel Pakira

hey people i am new in android app development… so, i wanna know what i need to develop this audio player…. i know, i need Eclipse or other but i wanna know the best IDE to use and this things, i am in a project and this prject is a audio player for my school… so help me please…. what i do? fast answer please… thanks

Bob Smith

Eclipse is the best IDE in my opinion. Android even has a bundle that installs Eclipse completely ready to develop for android. Here’s a link: http://developer.android.com/sdk/index.html After you install the bundle, you just have to launch eclipse, create a new android application project and then you are ready to run.

Danny Herrera

No he podido descargar el codigo fuente pero el tutorial muy bien

george

Hi all. I am a novice in developing app and thanks to
Ravi for all good work that is doing . I’m working at a simple app and I get
stuck . if you have knowledge of
building android apps, and make team
with me please replay to george.celsie@gmail.com . Thanks ang
good luck for all your projects.!

Paul75

Hello,

Thanks for player.
I want to use it but in my program i create two buttons. In default the player is visibility gone and when I click to the first button I want to appear the player. and in the second button set the visibility VISIBLE. It works but when I start playing I want to stop the player. When I can do this ? Besause I use mp.stop() or mp.release(); but it crash or if I click to the first button it launch the player. …

Thanks for help.

akbar88

Hi,

I’m really new to Android. I try your codes and got this error message “Unfortunately, AndroidBuilderMusicPlayer has stopped” on the emulator.
On the console, it said ‘Failed to install AndroidBuildingMusicPlayer.apk on device ’emulator-5554!’

Really need solution for my study.
thanks a lot.

akbar88

I also have tried the suggestions as mentioned on the previous comments. The app is successfully installed but still crash while being launched on the emulator. Any suggestions?

Paul

same here… i downloaded and crash every time i run it :S

steve

U can check it in storage area.click that storage area in emulator u can see all the mp3 files

Had the same issue going on guys. However, in my case I needed to get the mp3 files from the raw folder, not from the sdcard. If this is your case, on the getplaylist method from “SongsManager class” add the following:

(Where: “songname” is the song’s name, the ActivityClass.PACKAGE_NAME is the name of the package and the “R.raw.file” is your mp3 raw file in the folder: “res/raw” if you don’t have one, create it and that’s it! )

Also (and this goes in general not only for raw files reading) in your onDestroy() method add the following:

Can you send me a print screen pic of your class structure? (java classes inside your package com.officialloadedweaponsapp) and a printscreen from your class Music? That will help a lot.. chavezlucero88@gmail.com is my email!

Yes !! Thank you ! that worked perfect. But I didnt quite understand your post about get song from raw folder. I couldnt get that to work. could you elaborate any more? Please ?!

Shahita Ahamed

Hi.. I had the same error as yours and I’m not able to rectify it. Can you please let me know the changes that you’ve made? Thanks a lot for your help..

Mayank Langalia

madam, i have same problem
can you please tell me what changes that you have done
i’m not able to rectify it

dheeraj

Everything is working correctly but there is only one thing I am having issue with. That is this code is unable to find music from all of the SD Card. It would be great help if someone could help me with it. how to get All song our mobile……plzzzzzz

Hello sir !
what if i want to add / delete playlist… do you hav a tutorial for that?
and i want to ADD some lyrics.. too

Shahita Ahamed

Hi, Ravi and other fellow developers:
Thank you so much for all your help. I’ve a small issue with the player. I want the code to read all the songs that are available in the device (both from internal memeory and SD card).. how do I do that? Please help. Thank you..

(I’m new to android developing. Please tell me how to modify SongsManager.java such that it reads all songs from the device.. Thank you so much for the help)

dheeraj

public class SongsManager {

final String MEDIA_PATH = Environment.getExternalStorageDirectory()

.getPath() + “/”;

private ArrayList<HashMap> songsList = new ArrayList<HashMap>();

private String mp3Pattern = “.mp3”;

// Constructor

public SongsManager() {

}

/**

* Function to read all mp3 files and store the details in

* ArrayList

* */

public ArrayList<HashMap> getPlayList() {

System.out.println(MEDIA_PATH);

if (MEDIA_PATH != null) {

File home = new File(MEDIA_PATH);

File[] listFiles = home.listFiles();

if (listFiles != null && listFiles.length > 0) {

for (File file : listFiles) {

System.out.println(file.getAbsolutePath());

if (file.isDirectory()) {

scanDirectory(file);

} else {

addSongToList(file);

}

}

}

}

// return songs list array

return songsList;

}

private void scanDirectory(File directory) {

if (directory != null) {

File[] listFiles = directory.listFiles();

if (listFiles != null && listFiles.length > 0) {

for (File file : listFiles) {

if (file.isDirectory()) {

scanDirectory(file);

} else {

addSongToList(file);

}

}

}

}

}

private void addSongToList(File song) {

if (song.getName().endsWith(mp3Pattern)) {

HashMap songMap = new HashMap();

songMap.put(“songTitle”,

song.getName().substring(0, (song.getName().length() – 4)));

songMap.put(“songPath”, song.getPath());

// Adding each song to SongList

songsList.add(songMap);

}

}

Shahita Ahamed

Hi, Dheeraj! Thank you so much for the reply. I really appreciate your help..

Hi Ravi. My problem is .. In my media player app . it runs in background after pressing home or back .. it is working fine. but when i select another song while other is running .. Two songs are playing together ..how to get rid of this. i have used mp.release(); but no idea what’s my mistake .. Thanks in advance..

For me, the problem was the android application permissions. I past that on my manifest file:

Soumyadeep Sur

Hi, Ravi
This as an awesome tutorial for music player. It helps me very much. You have created this app for the SD Card, but if we want to download mp3 from the url so how can we do that features?
I want like that, A website where different songs like 10 or 15, and the app will take those mp3 files from the internet. Thus that app will work like online streaming app. So Please help about that matter.

Joe

Does the androidhive subscribe feature still work? I don’t get any emails for me to activate…

i’m already fix the red sign in script…no red sign anymore. but still the same problem has apear in emulator/smartphone screen: “Unfortunately, music player has stopped” :'(

Shreyas

I want to play music in background. I am using stream music. what should i do?

Mohammad Tanvirul Islam

just play your media player by android service.

app

can you write the code of the Activity class ? i suffer from bug in eclipse :/

Mohammad Tanvirul Islam

your comments is not clear. please describe or mail me at my email tanvirul.cse.jkkniu@gmail.com . i need some one who worked with streaming music

姿妤 林

If I want to play audio from internet , How do I do?
For Example,I use php upload mp3 file to my local ,so there are many mp3 files,
But I want to click one mp3 file then play … that,I don’t know How to do?Please help me,
Thank you.

Hi the tutorial provided here does not work …it shows music played=r has stopped…Please provide a solution!

Qadzealous

i dont know what the problem seems to be but this code keeps crashing “Unfortunately AppName Has Stopped” no errors at all i tried the logcat to see whats wrong it keeps pointing to line 72 on MainActivity heres the code on the line 72 i dont seem to know whats wrong // Listeners

To play all sound in SDCARD, please insert this code
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public class SongsManager {

Hi , this tutorial really useful for me … I have a doubt I need to trim a audio file..
for example I have a audio file with 5 MIn.. I want to trim the file 2min to 4min… and save to sdcard…

thanks for advance….

Azhar Mohammed

Hi..I am using this musicplayer as a part of my app. I have given (button)onclicklistener from homepage(activity) to musicplayer(activity). And a button in musicplayer to Homepage. When i click on the musicplayer button in homepage. the song starts playing and everything works fine. When i go to homepage through intent and click on the musicplayer button in homepage the old song which started before is playing and a new media player is created and a new song is playing along with that Musicplayer UI is updated to the new song . Multiple clicks in musicplayer button creates multiple songs playing together . I just want to resume only the first created intent music player and not want to replace mediaplayer with new one.I have referred several sites but cant fix this. can any one help me with this.
Thanks in advance

Hi…here every thing is running on the main UI Thread…which is causing this error as it is taking long time to run the methods or loading the songs..instead you can start a service to do that and bind it to the activity so that you can avoid this issue….

I implemented this code, except I changed it to play music from R.raw instead of from the sdcard. But onCompletion is never being called, rendering the shuffle and repeat functionality useless. Thus you’re only able to play one song, then the user has to manually select the next song.

Does anyone know what the problem could be? I set breakpoints in onCompletion, and it’s just never being called period.

abdul

how did u run this code for R.raw will u please help me..

cc

Well, I’m not actually reading from R.raw. I’ve got everything hard-coded. The reason for that is eventually I want to have it streaming from the internet, but I’m going to need to find a web server I can use that allows streaming to android devices. If I can’t get streaming from the internet working, I’m just gonna bundle the songs with the app in the R.raw folder, and have it hard-coded. Of course that takes up a lot of space on the user’s device, so I’ll have to include a lot less songs.

JB

Hello
does anyone know about which license this Audio Player is based on? I appreciate if anyone could help me…

Jhazzmine

force close!! :'( i can’t run it.. can someone help me? newbie here..

Ramesh

Give path Ex: /sdcard/Music/.

Ken

it crashes when I run it on real device.

Ramesh

It Works fine.Thank you

Naim

Can You send me Source code kindly bro ?
I face Many error in my code.I can not solve it.
Email=naimcse131@gmail.com
Thanks.

Anshdeep Singh

can anybody tell me how to make the artists and albums activity which is present in other music players to play the songs of a selected album/artist??

姿妤 林

well…A little problem
When I try to escape system,it’s will cause system crash…

evA

I test this project on my phone and not work .. application crash .. i have android 4.1.1 JB on vodafone 875

KAUSHIK MUKHERJEE

great tutorial….I am able to build video player also with the help of this tutorial…..Thanks !!!!! 🙂

Rayma Usman

plz send me ur code plz i really need.plzzz

Timkraai

it give the error: “has stopped”

logcat gives Caused by: java.lang.NullPointerException

at com.androidhive.musicplayer.SongsManager.getPlayList(SongsManager.java:25)

at com.androidhive.musicplayer.AndroidBuildingMusicPlayerActivity.onCreate(AndroidBuildingMusicPlayerActivity.java:77)

i did changed my path to my music folder i have a nexus 5 so the path is: “/storage/emulated/0/Music” and there is a song in there

Guest

Sir I installed this app in my phone but it crashes whenever i m trying to launch it……………………why?????

Dawinder

hiii…
i have a problem while open audio player app.
there is a null point exception at SongManager.getPlayList.

i d’nt know how to solve this,
please help….

Dawinder

now after giving permission in manifest it again shows exception given below.

maybe you have no mp3 files directly within the MEDIA_PATH defined in SongsManager.java.
on my Samsung S4 I had to change it to ‘/storage/extSdCard/Music’ and I can (successfully) play only mp3 files located within the folder, subfolders are ignored.

banggarong

im not have a problem at running application.. but a error when i close application.. why ?

damien

Hi i search this player but where we can read music from url it’s posible ? thank for your reply

cpot23

Nice tutorial, thanks!
I’m wondering how to get the cover art of the currently played song since we have this nice cover art placeholder in the layout file.
Does anyone have a suggestion how to get it?
Thanks in advance!

Jatin

use MediaMetadataRetreiver class with getEmbeddedPicture() method.. use the following code.

I am creating application that load sequential mp3 files from URLs. As i try it with initialize next mp3 files on completion of first mp3 files, but the problem is that, when first files complete, player take time to prepare or initialize the next mp3 files, yes I know that is part of the media player,it will take time to Prepare(For prepare() it may be long And for prepareAsync() comparatively sort time).

But , What i need that, when first mp3 files complete there playing, next mp3 files (From url) should not take time to prepare,as it should play without interrupt. I want to know , Is there way to attach next mp3 files for buffering while playing current mp3?, so we can playing it without interrupt.

Maybe a tutorial how to sort songs by album or artist ? Because the list of just all songs on the devices is way too long …

Shashimith

How to settle Media path problem moto g uses /sdcard/Music/, where as my Samsung S4 I had to change it to ‘/storage/extSdCard/Music’ and also use final String MEDIA_PATH = Environment.getExternalStorageDirectory().getPath() + “/”; as this i have read in comment , please specify a solution which would work on all the devices .And also app crashes on
backpress how to handle that.

baskar

can anyone tell me how to get the album cover in the Image view? Thanks in advance

Tim Kraai

Retrieving the song doesn’t work. I get the first song every time. Is there a solution?

i have a problem while open audio player app. Unfortunately Stopped
i d’nt know how to solve this,..
Plzz sent me code Email :naimuiu001@gmail.com That’s very helpfull for mee .

Thanks

abalta

Make sure you have .mp3 files on specified directory like “/sdcard/”

Paras Nath Chaudhary

Can you write on “handling audio focus” with media apps?

gencoder1

Is there an way to incorporate notification control for the application to pause play etc…….. or if there are any tutorial similar to this one, great tut bTW..

silistre

hi thank you very very much for tutorial but I can’t make “delete mp3 button” in playlist , How I can add delete mp3 button ? thanks for help

ullas

hi was this code working In your devices people….?

abalta

Yes it works great.

Guest

can you send me this code kindly ..
I face some trouble and more error this code..
can not solve it.
Email=naimuiucse131@gmail.com
Thanks.

Rayma Usman

please also send me this code please

Naim

Can You send me Source code kindly ?
I face Many error in my code.I can not solve it.
Email=naimcse131@gmail.com
Thanks.

vivek

Really great code is working fine…

Rayma Usman

please send me your code

Ahsan Misbah

please send me your code, as i have a problem to launch this app.

Selva Kumar

Hi Vivek,

could you please send your working code? I’m getting plenty of errors when I opened in Android Visual Studio, and this is my first project,and also I’m in a hurry, kindly help.
email: Kumar.selva.c@gmail.com

Thanks

Ken

Cannot download source code

Asile

What if I want to make it read also other types of audio files? Like .m4a, .aac, .wma …

Priyank

Thanks. its great tutorial…but i want back button , how to use service in this code

satyenhacks

It crashed when I tested on my phone. I also added permission to read external storage but no use. What am I missing here?

How can I use this Player with parse.com, Can you suggest me any think? Means load files (Song URLs / Songs Titles audio/video) from parse.com database?
If Possible
Please Massage me on https://www.facebook.com/ambreen.khan.3304

Denis Marinx

Now it works ! I had the error about the NullPointerException in getPlayList() so I replaced : “new File(MEDIA_PATH)” by “Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)” and add in AndroidManifest.xml file : Hope it helps ! Works on my device now 🙂

Denis Marinx

I forgot to say thanks for this tutorial ! I’m beginning Android since a few days and it’s really easy to understand basics concepts. Some code will be changed because some bugs appear for me, anyway it will be a good exercice :). PS : You can add this additional feature for btnPrevious.setOnClickListener : if(mp.getCurrentPosition() > 15000){playSong(currentSongIndex);} // usually used

Rayma Usman

plz can u send me ur code plz.i really need this code which run properly.plz can u help me plz

shahin

Hello dear
Thank you very much for your education.
I have a button in my activity. Through this button I want to go to a music player activity. After pressing the button the forcclosed.
Below you can see a picture of it.
I am in a hurry. I appreciate your help.

please put a condition in AndroidBuildingMusicPlayerActivity class and method playSong(int songIndex) as songList.size()>0{
}
then it will work

sali

Hi there
Well, sorry, I can not speak the language designed for.
It’s fine, I do not want to go play songs from Sdkard. I called the folder named Res raw in the world.
I am very busy.
Please help me get faster through e-mail me to do.

Hi Ravi, is there a method to layer 2 mp3 sounds? I have been googling but i can find answers. I hope you can help. thank you so much and more power to your blog.

Atmiya Patel

*****i m having this error please help*******

03-09 14:29:26.989 23989-23989/com.mycompany.androidbuildingmusicplayer E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mycompany.androidbuildingmusicplayer/com.mycompany.androidbuildingmusicplayer.AndroidBuildingMusicPlayerActivity}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
at java.util.ArrayList.get(ArrayList.java:311)
at com.mycompany.androidbuildingmusicplayer.AndroidBuildingMusicPlayerActivity.playSong(AndroidBuildingMusicPlayerActivity.java:281)
at com.mycompany.androidbuildingmusicplayer.AndroidBuildingMusicPlayerActivity.onCreate(AndroidBuildingMusicPlayerActivity.java:80)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)

Manivel

Hi,

can’t get started.. Its force closed. the path “/sdcard/” only i gave. and while run the cmd ‘platform-tools> adb push “c:Songs1.mp3” “/sdcard/”‘ it show the error is.. ‘Read only access’ like that. so am not able to run can you plz help me to common path for all the device.. am using AVD and device too..

Logcat error i gues “file or directory is missing” something

raman kumar

i Need Method To get Song Info Like Song Artist,album,Thumbnail … Please upload These Methods also .. I would Be Thankful To You!!

Vincent Tirgei

Did you find a method?

Please share if so

ankit gajjar

Hello,

I want to integrate only One Song to this music which is playing continuously.That song is in the android project assests folder. How can i play that song. give me advice.

Rayma Usman

i have problem my my app run properly but when open it in my tab its giving me error unfortunately media player stop working.where’s the problem is anyone tell me please..

I am not able to access my external memory mp3 files , but its showing internal memory files. i m using my samsung galaxy s4 as the debugger.

kartik

I am facing same problem with moto e
even after using Environment.getExternalStorageDirectory.getAbsolutePath()/.getPath()
and is even Showing MOUNTED as its status

please help

Kadam Priyanka

I have a same problem,when I launch the app it shows unfortunately music player has stopped.please help me.

rAyMa uSmAn

change ur path.go to song manger.and set the path that is ur device path.

Tabrez Qureshi

bru i tried all my device path bt it still shows unfortunately music player has stopped..

rAyMa uSmAn

add to your manifest

after this add ur right path in this line

final String MEDIA_PATH = new String(“/sdcard/Music”);

my device path is /sdcard/Music add ur right path with right spelling

gourav

The app Crashes at back press, due to Runnable method…. have any solution 4 this???

kartik

Hy Ravi Tamada thank u very much for explation and code

but app is using only internal storage and not accessing SD card .i have chaged manifest file and also used Environment.getExternalStorageDirectory.getAbsolutePath()/.getPath() but its not working .and by status its showing MOUNTED .

Thanks for tutorial . but now days i am faceing one issue that occur only lolipop version. In my app i have 10 song and when i tap to next button or pervious button click whenever song is playing that time onCompletelistener oncompletemethod fire more then 5 or 6 time and song autometically play one by one automatically . step for getting this issue click on start play song button and and tap next button befor song starting time less then 10 second that time autometically 3 or 4 song goes away.

akn

I manage to work this code

1.Add “AndroidManifest.xml” this permision(it should be between “android:versionName=”1.0″ >:” to )

2.Go “SongsManager.java” change:

final String MEDIA_PATH = new Environment().getExternalStorageDirectory().getPath();

4.go mnt->sdcard then select this .Now you should add .mp3 file to do that look above
there is a button “push a file onto the device” click this then choose your .mp3 file.

5.if step 4 gives error , it is about permission find sdcard with d–rwxr-x permission
it can be under stroge forder or only sdcard folder.In correct sdcard there is a thing called
LOST.DIR
or watch this video https://www.youtube.com/watch?v=ipRke1P4rIc

hardik

i downloaded full source code but i am getting run time error on it. unfortunately all has stop working please any one tell me soiution for it..

Hetuka

this code is very helpful for me, thanks a lot, now i want to add those songs in playlist and add to favorite please give a any solution,,,Thanks in Advance.

Namrata

hi! this is just a minor issue but its still a bit of a nagger. The player.xml and playlist.xml files have the layout_width and layout_height set to “match_parent” and the compiler is taking it as an error. What can i replace it with? Thanks in advance. The rest is flawless.

Thanks for the Tutorial dude!!!. It really helped me alot 🙂
But i am facing a problem,Whenever i try to close the app, the media player is getting stopped.Ideally it has to play songs in the background, but its not happening.

i have tried every single thing mentioned in the comments below but still its not working..

can anyone please help me ????

“unfortunately all has stop working ” pops out each time i open the app

Zaier

Hi everybody,
I am here just to mention that Ravi don’t know the difference between between Runnable and thread Runnable is NOT a thread and handler.post(runnable) is for running the runnable in the thread where the handler was created/ instantiated. So all this application is running in the UI thead.
If I was him I would learn about multi-threading & then correct this tutorial.

Zaier

Ravi my friend try this:
in onCreate methode , just to be sure that you are in the UI thread and then see witch thread is created and called

add this to the main activity so it for background or lock screen playng 🙂

Pranshu Sinha

hello everyone..
i have been trying to make this project but i am facing the same error as everyone i.e ava.lang.RuntimeException: Unable to start activity … i added the permissions in the manifest file and also changed the file path…. it is not running on emulator as well as the mobile phone.. pls help me… contact me on pranssin@gmail.com

nimahkh

thank you for your nice code , i have a problem , i want to get all of musics from assets folder ( app assets folder ), how can i do this? it is very important for me , thank you

Ravi can you please guide me? i want to have my offline playlist and publish it for my users , i want users just listen to playlist that i make inside of application . how can i change /sdcard/ address to my resource from application ? thank you very much .

virinchi reddy

Hey Jack I just went through your git hub account and want to develop a audio widget just like yours . Can i use your ‘s as a tutorial to develop my own android widget thank you . If it is okay then please mail me at yekkantivirinchireddy@gmail.com .

vinod

When app opens unfortunaltely app stopped. whats the solutions!!!!!!!!!!plz any one help me….

Jerome

Thank you for your wonderful work, I have a question :
Is this code open-source?
Can I reuse it even in a commercial app ?

Thanks again

Pratik Saluja

Awesome, Thank you

Osama Abu Nuwar

How to download code???

Prayag Ramanuj

error occur. while start activity.

David

If you are using Android 6.0 you need to check and ask for storage permission.
Did you made that ?

Vinoth Vino

Thank you for the wonderful tutorial 🙂

Touchstone toucstone

Everything is working like charm except when i hit back button of device , application is crashing with this exception
Process: com.androidhive.musicplayer, PID: 29938
java.lang.IllegalStateException

I have also added permissions like Read and Write External Storage . Please help

Kedrian Fiwi Linkz James

if your are clicking the back button and exiting update your ondestroy method to

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hp.insanemediaplayer/com.example.hp.insanemediaplayer.MusicPlayer}: java.lang.NullPointerException: Attempt to get length of null array
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to get length of null array
at com.example.hp.insanemediaplayer.SongsManager.getPlayList(SongsManager.java:28)
at com.example.hp.insanemediaplayer.MusicPlayer.onCreate(MusicPlayer.java:79)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native
help me out sir fed up of getting this error Please help me 5 days since i have been stuck at this

replace File(MEDIA_PATH)” by Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)” ;
and add this permission to manifest file

An Nguyen

11-05 18:31:15.494 2359-2359/com.androidhive.musicplayer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.androidhive.musicplayer, PID: 2359
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidhive.musicplayer/com.androidhive.musicplayer.AndroidBuildingMusicPlayerActivity}: java.lang.NullPointerException: Attempt to get length of null array
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to get length of null array
at com.androidhive.musicplayer.SongsManager.getPlayList(SongsManager.java:25)
at com.androidhive.musicplayer.AndroidBuildingMusicPlayerActivity.onCreate(AndroidBuildingMusicPlayerActivity.java:77)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Please ! help me

Muhammad Abubakar

I have a question how to convert psd Icons and Images to xml ???

Ajish Nair

Hi It is not working properly. It is showing unfortunately app has been stopped.
what is the issue

Hi, can I have a simple play, pause and stop button with seekhbar mediaplayer which can play from URL or better from string, is there any tutorial ?
I have done some but i am unable to play from a URL or a String.
I want to implement this audio player in your MultiLevel ListView, I already implemented VideoView which is working greate, and I need Audio player as well.

thanx, i will try this also, and i actually successfully done all that, but no seekhbar, i tried but seekhbar does not work.

Akshay Sharma

Thanks ……
Please tell me how to use listview of above songs in tabbed layout

Rishabh Kaushik

There is a tiny error in onActivityResult() method in your main activity (AndroidBuildingSomethingActivity) dude. You tried to retrieve “songIndex” by getExtras() method from intent. It should be “songPath”.

If you remove getPlaylist() method …there is no way you will get the songs from the sdcard.
It is not a solution, rather you are just removing the problem and ultimately the main purpose of the app.
No Songs , Noting to Play.

Yuneeb Arshad

has you solved this problem

Abhishek Lahoti

can anyone send me the download link of this source code file fast

darpan

how to add album art of my mp3 in this music app?

Vaibhav Chugh

Caused by: java.lang.NullPointerException: Attempt to get length of null array
at com.androidhive.musicplayer.SongsManager.getPlayList(SongsManager.java:27)

Vaibhav Chugh

how can we read all mp3 songs from a whole mobile storage ….i cannot get all mp3 , i only getting from SD-Card…please help

umme sulaith

in the UI of player im getting a rendering problem: ” invalid drawable tag Linear Layout”
can anybody help asap pls …….

Caused by: java.lang.NullPointerException: Attempt to get length of null array
at com.example.rg.deadlab_1.SongsManager.getPlayList(SongsManager.java:26)
at com.example.rg.deadlab_1.AndroidBuildingMusicPlayerActivity.onCreate(AndroidBuildingMusicPlayerActivity.java:76)

Aminju Muhammad

Yeah i get same error, can anyone help us?

Need Help

hi,can u help me design the chat app where we can listen to song as soon as we sign in/sign up.and we can watch video directly from group chat or share the video to your friend.thank you so much if u can help me.

Sir! How can I get all songs from both External and Internal storage.
I downded source code and ran this.
Now, I have a little problem.

ကို ဘုန္း

java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

That is my problem.And then

final String MEDIA_PATH = new String(“/sdcard/”);

instead of “/sdcard/” I made sure the path of songs
But it still no Ok 🙁
How can I fix it. Please reply me!!!
Thz U

neha

hello dear, i am getting same error , if you have resolved this issue , please help me …..

John Bang

Hi Ravi, the same question, can you send link to github?

An Nguyen

11-05 18:31:15.494 2359-2359/com.androidhive.musicplayer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.androidhive.musicplayer, PID: 2359
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidhive.musicplayer/com.androidhive.musicplayer.AndroidBuildingMusicPlayerActivity}: java.lang.NullPointerException: Attempt to get length of null array
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to get length of null array
at com.androidhive.musicplayer.SongsManager.getPlayList(SongsManager.java:25)
at com.androidhive.musicplayer.AndroidBuildingMusicPlayerActivity.onCreate(AndroidBuildingMusicPlayerActivity.java:77)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Please ! help me

Shyam Sunder

Same here to me also.. RAVI I would appreciate if you can help us on this?

The code is very old and you need to debug it well. See on which line the null point exception is occurred?

Shyam Sunder

ERROR:
Caused by: java.lang.NullPointerException: Attempt to get length of null array
at com.androidhive.musicplayer.SongsManager.getPlayList(SongsManager.java:25)
at com.androidhive.musicplayer.AndroidBuildingMusicPlayerActivity.onCreate(AndroidBuildingMusicPlayerActivity.java:77)

10621-10621/com.androidhive.musicplayer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.androidhive.musicplayer, PID: 10621
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidhive.musicplayer/com.androidhive.musicplayer.AndroidBuildingMusicPlayerActivity}: java.lang.NullPointerException: Attempt to get length of null array
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.NullPointerException: Attempt to get length of null array
at com.androidhive.musicplayer.SongsManager.getPlayList(SongsManager.java:25)
at com.androidhive.musicplayer.AndroidBuildingMusicPlayerActivity.onCreate(AndroidBuildingMusicPlayerActivity.java:77)
at android.app.Activity.performCreate(Activity.java:6245)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

Sathish Balaram

use media player permission

Shyam Sunder

Ravi: Can you help me on this error? I’ve tested in mobile MotoG2

Sara

Thank you for this tutorial! It is very thorough and extremely helpful for someone new to working with audio in Android.

Sir me too a new to it. I tested both on emulator and on real device. Both importing your project and also on my own project based on your tutorial. But application stops responding. Please help

Nguyễn Như Việt

Thank You.. ^^

Nasrullah haq

sir i cant install it in my real device

Amal p

———————————————————————————————————————————————
Modify SongsManger like this and you have to pass context of appropriate activity during object creation of songsmanger in that activity

I have corrected the code for getplaylist() function and dont forget to add runtime permission in manifest and also
check the permission using a dialog box
——————————————————————————————————————————————————

I’m using Android Studio, i try to build the Apk but I don’t arrived, please help me

ADevD

Thanks Ravi…
These were very useful for my Project work.
Rest I faced some problems, worked few days onto them and googling I found there’s better Solution to the problem :: Java*Null*Exception
I recommend using the following code in Songsmanager class to Create Playlist & Load Music Files.
=============================================================================
I found this code athttp://stackoverflow.com/questions/12005566/how-to-play-mp3-files-from-internal-and-external-sd-card-in-android/
=============================================================================
package com.androidhive.musicplayer;
import android.os.Environment;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;
public class SongsManager {

kindly check your path in sd card where mp3 file exist. then set it in this :- final String MEDIA_PATH = new String(“/storage/sdcard0/Music”);

Владимир Ковалёв

Guys, I decided NillPointerExeption problem!
First of all, add to the manifest this lines:

Secondly, in the method OnCreate() paste the following code which requests permission to access files from the user (this thing has become necessary for Android 6 and above)
Then, correct the path to the sd card in the class SongManager. (In my case, it looks like this:
final String MEDIA_PATH = new String ( “/storage/830D-15FF/Music”); )
Finally, in the same class completely delete the contents of the method:
publics SongsManager()
and insert the following code instead:

Rather than using a VIRTUAL DEVICE , prefer to use it in your phone ,by enabling the DEVELOPERS option in your phone and connecting it to the PC/LAPTOP , it will run and load faster than your AVD.

ravi rocky

yes we cant fetch any song from any kind of virtual machine it may raise null pointer exception also
so better go on with physical device

Shami

Hi Awesome code but it got few bugs like u r using Runnable which keep on running and we are not shutting it down on activity stop or activity pause which could cause crash since our media player object is null and Runnable is still working…..

ABILASH SOMAN

how to avoid stoping music on clicking back button(i need continuously playing song if we press back button)

Jamal Zafar

did you solved it?

ABILASH SOMAN

how to add services for playing song in background????

Soft key

How to add Music Equalizer animation ?? Please help

Panuwat Kumsaikaew

How to delete mp3 file from listview ? please advice.

Yacine

How did you arrived to build the Apk, please Tell me

Benz Panuwat

I just follow up this tutorial.

UBAID ABBASI

Not Working I got Error
Caused by: java.lang.NullPointerException at SongsManager.getPlayList(SongsManager.java
any one guide?

ravi rocky

do use try catch !!!
expection will be avoid

Tavish Barua

Need Help! How can i separate Service and create a dedicated service file so that i can bind that service with the activity later .

Tavish Barua

Need Help! How can i separate Service and create a dedicated service file so that i can bind that service with the activity later .

kamal

I want the list of songs in the main screen then user click on the song then the media player opens….what to do for that

is the onclick on next button taking care of the fact that the user might have shuffle on…so it has to play a random song on next button click, maybe i am wrong but i did not find the code to care of this condition.

suriya pandian

i downloaded your code and compiled it through my eclipse. but my apk is not working properly

Angga

How to play mp3 in to apk raw folder?
Tanks

Yogesh Singh

its easy checkout javatpoint

Tulaib Mirza

bro please can you tell how we can achieve same thing but loading audio from url because saving in raw folder is increasing my app size ?