使用 PATH 列出媒体商店中的所有音乐

2022-09-01 09:38:33

好吧,我已经在这个项目上工作了几天,我大部分时间都在研究如何在LIST VIEW或其他东西中列出设备上的所有音乐,我已经搜索了几天了,这正在杀死我。我确实在某一时刻非常接近,所有音乐都在一个文件夹中显示,尽管由于大多数人都会有诸如artiest和专辑之类的子文件夹,我需要一种方法来搜索MP3或音乐文件的子文件夹。

以下是我到目前为止对音乐收藏的看法:

package com.androidhive.musicplayer;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;

import android.provider.MediaStore;

public class SongsManager {
// SDCard Path
final String MEDIA_PATH = new String(MediaStore.Audio.Media.getContentUri("external").toString());
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

// Constructor
public SongsManager(){

}

/**
 * Function to read all mp3 files from sdcard
 * and store the details in ArrayList
 * */
public ArrayList<HashMap<String, String>> getPlayList(){
    File home = new File(MEDIA_PATH);

    if (home.listFiles(new FileExtensionFilter()).length > 0) {
        for (File file : home.listFiles(new FileExtensionFilter())) {
            HashMap<String, String> song = new HashMap<String, String>();
            song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4)));
            song.put("songPath", file.getPath());

            // Adding each song to SongList
            songsList.add(song);
        }
    }
    // return songs list array
    return songsList;
}

/**
 * Class to filter files which are having .mp3 extension
 * */
class FileExtensionFilter implements FilenameFilter {
    public boolean accept(File dir, String name) {
        return (name.endsWith(".mp3") || name.endsWith(".MP3"));
    }
}

}

感谢任何能提供帮助的人。:)


答案 1

虽然,这篇文章很旧,但对于像我这样的其他人来说,要获得使用其文件路径创建音乐列表的想法,我在这里添加了解决方案。 列实际上包含媒体文件路径。您可以使用以下代码段获取必要的信息:MediaStore.Audio.Media.DATA

ContentResolver cr = getActivity().getContentResolver();

Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + "!= 0";
String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
Cursor cur = cr.query(uri, null, selection, null, sortOrder);
int count = 0;

if(cur != null)
{
    count = cur.getCount();

    if(count > 0)
    {
        while(cur.moveToNext())
        {
            String data = cur.getString(cur.getColumnIndex(MediaStore.Audio.Media.DATA));
            // Add code to get more column here

            // Save to your list here
        }

    }

    cur.close();
}

答案 2

您可以使用此代码列出所有音乐文件

//Some audio may be explicitly marked as not being music
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

String[] projection = {
        MediaStore.Audio.Media._ID,
        MediaStore.Audio.Media.ARTIST,
        MediaStore.Audio.Media.TITLE,
        MediaStore.Audio.Media.DATA,
        MediaStore.Audio.Media.DISPLAY_NAME,
        MediaStore.Audio.Media.DURATION
};

cursor = this.managedQuery(
        MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
        projection,
        selection,
        null,
        null);

private List<String> songs = new ArrayList<String>();
while(cursor.moveToNext()) {
        songs.add(cursor.getString(0) + "||" 
                    + cursor.getString(1) + "||" 
                    + cursor.getString(2) + "||"
                    + cursor.getString(3) + "||"
                    + cursor.getString(4) + "||" 
                    + cursor.getString(5));
}

我没有尝试过这个代码,但它似乎是正确的。您将走在正确的轨道上。


推荐