简体   繁体   中英

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!

You need to persisting the song data to the device .

The Android framework offers several options and strategies for persistence:

  • Shared Preferences - Easily save basic data as key-value pairs in a private persisted dictionary.
  • Local Files - Save arbitrary files to internal or external device storage.
  • SQLite Database - Persist data in tables within an application specific database.
  • ORM - Describe and persist model objects using a higher level query/update syntax.

Use Cases

Each storage option has typically associated use cases as follows:

  • Shared Preferences - Used for app preferences, keys, and session information.
  • Local Files - Often used for blob data or data file caches (ie disk image cache)
  • SQLite Database - Used for complex local data manipulation or for raw speed
  • ORM - Used to store simple relational data locally to reduce SQL boilerplate

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM