Skip to content Skip to sidebar Skip to footer

Android Mp3 Player To Play List Of Songs

I need the sample code of an mp3 player in android to play more than one file. i.e Song should be played one after the other from a particular folder in our system. Can any one pos

Solution 1:

Solution 2:

A complete mp3 player ready to be integrated in your project: github.com/avafab/URLMediaPlayer

Solution 3:

simple functionality media player, with most of the functions,

class represent the song

privateclassSongDetails {
        StringsongTitle="";
        StringsongArtist="";
        //song location on the deviceStringsongData="";
 }

function that return all the media files mark as mp3 and add them to array

privatevoidgetAllSongs()
    {
       //creating selection for the databaseStringselection= MediaStore.Audio.Media.IS_MUSIC + " != 0";
        final String[] projection = newString[] {
                MediaStore.Audio.Media.DISPLAY_NAME,
                MediaStore.Audio.Media.ARTIST,
                MediaStore.Audio.Media.DATA};

        //creating sort by for databasefinalStringsortOrder= MediaStore.Audio.AudioColumns.TITLE
                + " COLLATE LOCALIZED ASC";

        //stating pointerCursorcursor=null;

        try {
            //the table for queryUriuri= android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            // query the db
            cursor = getBaseContext().getContentResolver().query(uri,
                    projection, selection, null, sortOrder);
            if (cursor != null) {

                //create array for incoming songs
                songs = newArrayList<SongDetails>(cursor.getCount());

                //go to the first row
                cursor.moveToFirst();

                SongDetails details;

                while (!cursor.isAfterLast()) {
                    //collecting song information and store in array,//moving to the next row
                    details = newSongDetails();
                    details.songTitle = cursor.getString(0);
                    details.songArtist = cursor.getString(1);
                    details.songData = cursor.getString(2);
                    songs.add(details);
                    cursor.moveToNext();

                }
            }
        } catch (Exception ex) {

        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

shared interface between the player and the activity

publicinterfaceOnPlayerEventListener {
    voidonPlayerComplete();
    voidonPlayerStart(String Title,int songDuration,int songPosition);
}

class represent the media player

publicclasssimplePlayer
{
    OnPlayerEventListener mListener;
    Activity mActivity;

    //give access to the guipublic ArrayList<songDetails> songs = null;
    public boolean isPaused = false;
    publicint currentPosition = 0;
    publicint currentDuration = 0;

    //single instancepublicstatic MediaPlayer player;

   //getting gui player interfacepublicsimplePlayer(Activity ma)
   {
       mActivity = ma;
       mListener = (OnPlayerEventListener) mActivity;
   }

    //initialize the playerpublicvoidinit(ArrayList<songDetails>_songs)
    {
        songs = _songs;
        currentPosition = 0;


        if(player == null)
        {
            player = new MediaPlayer();
            player.setAudioStreamType(AudioManager.STREAM_MUSIC);
            player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                publicvoidonCompletion(MediaPlayer mp) {

                    player.stop();
                    player.reset();

                    nextSong();
                    mListener.onPlayerSongComplete();

                }
            });
        }
    }

    //stop the current songpublicvoidstop()
    {
        if(player != null)
        {
            if(player.isPlaying())
                //stop music
                player.stop();

            //reset pointer
            player.reset();
        }
    }

    //pause the current songpublicvoidpause()
    {
        if(!isPaused && player != null)
        {
            player.pause();
            isPaused = true;
        }
    }

    //playing the current songpublicvoidplay()
    {
        if(player != null)
        {
            if(!isPaused && !player.isPlaying())
            {
                if(songs != null)
                {
                    if(songs.size() > 0)
                    {
                        try {
                            //getting file path from data
                            Uri u = Uri.fromFile(new File(songs.get(currentPosition).songData));

                            //set player file
                            player.setDataSource(mActivity,u);
                            //loading the file
                            player.prepare();
                            //getting song total time in milliseconds
                            currentDuration = player.getDuration();

                            //start playing music!
                            player.start();
                            mListener.onPlayerSongStart("Now Playing: "
                                    + songs.get(currentPosition).songArtist
                                    + " - "+ songs.get(currentPosition).songTitle
                                    ,currentDuration,currentPosition);
                        }
                        catch (Exception ex)
                        {
                            ex.printStackTrace();
                        }
                    }
                }
                else
                {
                    //continue playing, reset the flag
                    player.start();
                    isPaused = false;
                }
            }
        }
    }

    //playing the next song in the arraypublicvoidnextSong()
    {
        if(player != null)
        {
            if(isPaused)
                isPaused = false;

            if(player.isPlaying())
                player.stop();

            player.reset();

            if((currentPosition + 1) == songs.size())
                currentPosition = 0;
            else
                currentPosition  = currentPosition + 1;

            play();
        }
    }

    //playing the previous song in the arraypublicvoidpreviousSong()
    {
        if(player != null)
        {
            if(isPaused)
                isPaused = false;

            if(player.isPlaying())
                player.stop();

            player.reset();

            if(currentPosition - 1 < 0)
                currentPosition = songs.size();
            else
                currentPosition = currentPosition -1;

            play();
        }
    }

    //getting new position for playing by the gui seek barpublicvoidsetSeekPosition(int msec)
    {
        if(player != null)
            player.seekTo(msec);
    }

    //getting the current duration of musicpublicintgetSeekPosition()
    {
        if(player != null)
            return  player.getDuration();
        elsereturn-1;
    }
}

class represent the main activity

publicclassMainActivityextendsActionBarActivityimplementsOnPlayerEventListener {

simplePlayer player;

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //getting all songs in the devicegetAllSongs();

    if (player == null) {
        //create new instance and send the listener
        player = newsimplePlayer(this);
        //initialize the simplePlayer
        player.init(songs);
        //start playing the first song
        player.play();

        seekBar.setMax(player.currentDuration);
    }
}

@OverridepublicvoidonPlayerSongComplete() {
 //need to be implement!
}

@OverridepublicvoidonPlayerSongStart(String Title, int songDuration, int songPosition) {
    this.setTitle(Title);
    seekBar.setMax(songDuration);
    seekBar.setProgress(0);
}
}

good luck!

Solution 4:

At some point, you are going have to deal with this when it comes to playing music on Android.

http://developer.android.com/reference/android/media/MediaPlayer.html

Solution 5:

Post a Comment for "Android Mp3 Player To Play List Of Songs"