简体   繁体   English

Android MP3文件的动态播放列表

[英]Dynamic Playlists for MP3 files Android

I am quite new to android development, and i'm currently attempting to make a media player. 我对android开发非常陌生,目前正在尝试制作媒体播放器。 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. 我的下一个目标是实现一个播放列表系统,可以在其中添加和删除项目,并且可以在触发onCompleteListener()时将其自动删除。 I am currently pulling songs from the SD card using contentResolver 我目前正在使用contentResolver从SD卡中提取歌曲

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. 至此,我已经进行了设置,因此可以单击listView中的项目,它可以通过服务开始播放歌曲。 My question is what is the best way to store the data of the song URI and position. 我的问题是,存储歌曲URI和位置数据的最佳方法是什么。 I would also like for the application to be able to store the playlists last position when the app restarts, even after an onDestroy(). 我还希望该应用程序能够在应用程序重新启动时存储播放列表的最后位置,即使在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: Android框架提供了多种持久性选项和策略:

  • 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. SQLite数据库 -将数据保留在特定于应用程序的数据库中的表中。
  • ORM - Describe and persist model objects using a higher level query/update syntax. ORM-使用更高级别的查询/更新语法描述和持久化模型对象。

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) 本地文件 -通常用于Blob数据或数据文件缓存(即磁盘映像缓存)
  • SQLite Database - Used for complex local data manipulation or for raw speed SQLite数据库 -用于复杂的本地数据操作或原始速度
  • ORM - Used to store simple relational data locally to reduce SQL boilerplate ORM-用于在本地存储简单的关系数据以减少SQL样板

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. 您可以使用其中一个选项,但是出于您的目的, SQLite DatabaseORM是更好的选择。

You can visit Local Databases with SQLiteOpenHelper for SQLite Database. 您可以使用SQLiteOpenHelper访问SQLite数据库的本地数据库。

For ORM, you can try greenDAO . 对于ORM,您可以尝试greenDAO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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