java - 如何将一些文件添加到播放列表中

标签 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> )。

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

最佳答案

就我而言,我只有文件列表,所以首先我需要使用文件路径获取音频 ID:

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;

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

ContentResolver resolver = getContentResolver();
long trackId = getTrackIdByPath(context, pathToFile);
    Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playListId);
    Cursor cursor = resolver.query(uri, new String[] {"count(*)"}, null, null, null);
    cursor.moveToFirst();
    int last = cursor.getInt(0);
    cursor.close();
    ContentValues value = new ContentValues();
        value.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, ++last);
        value.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, trackId);

    resolver.insert(uri, value);

关于java - 如何将一些文件添加到播放列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45444698/

相关文章:

java - 如何让我的应用程序获取在 WebView 中单击的链接的 URL,对其执行操作,然后加载它?

java - jsoup 获取与它们相关的特定标签和值

javascript - 无法获取 YouTube 视频的自定义播放列表

java - 请求所有帐户用户时出现 Google Directory API 403 Insufficient Permission 错误

java - 如何提取rdf :about or rdf:ID properties from triples using SPARQL?

java - MPAndroid图表组合图表不显示数据

android - Activity 问题中的 findViewById

android - 如何创建 XML 可绘制对象的选择器?

python - 音乐播放器播放列表

youtube - 检索播放列表中包含的视频 ID - YouTube API v3