简体   繁体   English

如何从Media Player列出音频文件,然后在android中播放它们

[英]How to list audio files from Media Player and then play them in android

I want to get the list of all the songs that are added in the media player in android. 我想获取在Android媒体播放器中添加的所有歌曲的列表。 if i dont have a path to where all the music files are stored,then how can i retrieve the list of the songs? 如果我没有存储所有音乐文件的路径,那么我该如何检索歌曲列表?

is there any way to get the path where the songs are stored without taking the path from the user? 有什么方法可以在不从用户那里获取路径的情况下获得存储歌曲的路径?

UPDATED->> i got the list. 更新->>我得到了列表。 now i want to play the selected song from another activity when i am just passing the song name. 现在,当我刚刚传递歌曲名称时,我想播放其他活动中选择的歌曲。 is that possible? 那可能吗? and how?? 如何??

Please help. 请帮忙。 Thanks 谢谢

try below code to get mp3 from SDCard :- 尝试以下代码从SDCard获取mp3:-

final String MEDIA_PATH = Environment.getExternalStorageDirectory()
        .getPath() + "/";
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
private String mp3Pattern = ".mp3";

// Constructor
public SongsManager() {

}

/**
 * Function to read all mp3 files and store the details in
 * ArrayList
 * */
public ArrayList<HashMap<String, String>> getPlayList() {
    System.out.println(MEDIA_PATH);
    if (MEDIA_PATH != null) {
        File home = new File(MEDIA_PATH);
        File[] listFiles = home.listFiles();
        if (listFiles != null && listFiles.length > 0) {
            for (File file : listFiles) {
                System.out.println(file.getAbsolutePath());
                if (file.isDirectory()) {
                    scanDirectory(file);
                } else {
                    addSongToList(file);
                }
            }
        }
    }
    // return songs list array
    return songsList;
}

private void scanDirectory(File directory) {
    if (directory != null) {
        File[] listFiles = directory.listFiles();
        if (listFiles != null && listFiles.length > 0) {
            for (File file : listFiles) {
                if (file.isDirectory()) {
                    scanDirectory(file);
                } else {
                    addSongToList(file);
                }

            }
        }
    }
}

private void addSongToList(File song) {
    if (song.getName().endsWith(mp3Pattern)) {
        HashMap<String, String> songMap = new HashMap<String, String>();
        songMap.put("songTitle",
                song.getName().substring(0, (song.getName().length() - 4)));
        songMap.put("songPath", song.getPath());

        // Adding each song to SongList
        songsList.add(songMap);
    }
}

or 要么

final Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    final String[] cursor_cols = {
            MediaStore.Audio.Media._ID,
            MediaStore.Audio.Media.ARTIST,
            MediaStore.Audio.Media.ALBUM,
            MediaStore.Audio.Media.TITLE,
    };
    final String where = MediaStore.Audio.Media.IS_MUSIC + "=1";
    final Cursor cursor = getContentResolver().query(uri, cursor_cols, where, null, null);
    cursor.moveToNext();
    final String artist = cursor.getString(_cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
    final String album = cursor.getString(_cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
    final String track = cursor.getString(_cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
    doSomethingHere(artist, album, track);

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

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