如何将几个文件添加到播放列表中

时间:2017-08-01 17:56:24

标签: java android playlist

我使用下一种方法创建新的播放列表:

ContentResolver resolver = getContentResolver();
ContentValues value = new ContentValues();
value.put(MediaStore.Audio.Playlists.NAME, "Name PlayList");
value.put(MediaStore.Audio.Playlists.DATE_MODIFIED, System.currentTimeMillis());
resolver.insert(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, value);

我有mp3文件的列表(ArrayList<File>)。

如何将此文件添加到新播放列表中?

1 个答案:

答案 0 :(得分:1)

在我的情况下,我只有File列表,所以首先我需要使用path to file获取音频ID:

new ByteArrayInputStream(bytes);

之后我可以将音频添加到播放列表中:

public static long getTrackIdByPath(Context context, String pathToFile){
long id = - 1;
String[] projection = {MediaStore.Audio.Media._ID,
                       MediaStore.Audio.Media.DATA};
String selection = MediaStore.Audio.Media.DATA + " like ?";
Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
        projection, selection,
        new String[] {pathToFile}, null);
cursor.moveToFirst();
if(cursor.getCount() > 0)
    id = cursor.getLong(0);
cursor.close();
return id;