javaandroidaudio-playerplaylistsandroid-music-player

Dynamic Playlists for MP3 files Android


I am quite new to android development, and i'm currently attempting to make a media player. My next goal on it is to implement a playlist system that items can be added to, as well as removed, and can be auto removed when onCompleteListener() is triggered. I am currently pulling songs from the SD card using contentResolver

public void getSongList(){
        ContentResolver musicResolver=getContentResolver();
        Uri musicUri= MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        Cursor musicCursor=musicResolver.query(musicUri,null,null,null,null);

        if(musicCursor!=null && musicCursor.moveToFirst()){
            //get columns
            int titleColumn = musicCursor.getColumnIndex
                    (android.provider.MediaStore.Audio.Media.TITLE);
            int idColumn = musicCursor.getColumnIndex
                    (android.provider.MediaStore.Audio.Media._ID);
            int artistColumn = musicCursor.getColumnIndex
                    (android.provider.MediaStore.Audio.Media.ARTIST);
            //add songs to list
            do {
                long thisId = musicCursor.getLong(idColumn);
                String thisTitle = musicCursor.getString(titleColumn);
                String thisArtist = musicCursor.getString(artistColumn);
                songList.add(new Song(thisId, thisTitle, thisArtist));
            }
            while (musicCursor.moveToNext());
        }
    }

At this point I have it set up thus that I can click on the item in a listView and it can start playing the song through a service. My question is what is the best way to store the data of the song URI and position. I would also like for the application to be able to store the playlists last position when the app restarts, even after an onDestroy(). I'm not sure what kind of data sets would be best used to store that data, and how to retrieve it. Any help would be much appreciated!


Solution

  • You need to persisting the song data to the device.

    The Android framework offers several options and strategies for persistence:

    Use Cases

    Each storage option has typically associated use cases as follows:

    Read more: Persisting Data To Device.


    You can use one of the options, but for your purpose, SQLite Database or ORM is the better options.

    You can visit Local Databases with SQLiteOpenHelper for SQLite Database.

    For ORM, you can try greenDAO.