如何从android中的内部和外部存储中获取所有.mp3文件

时间:2016-08-20 07:05:34

标签: android

我想制作一个音乐播放器。但我无法从内部和外部存储中获取所有.mp3文件。请有人帮帮我。 提前致谢。 这是我的代码。

    public void getListOfSong1(Context context) {
    SongData.cart.clear();
    Cursor c = context.getContentResolver().query(Media.INTERNAL_CONTENT_URI, null, "is_music != 0", null, null);
    c.moveToFirst();
    while (c.moveToNext()) {
        SongData songData = new SongData();
        int _id = c.getColumnIndex(MostAndRecentSongTableHelper.ID);
        String title = c.getString(c.getColumnIndex(MostAndRecentSongTableHelper.TITLE));
        String artist = c.getString(c.getColumnIndex(MostAndRecentSongTableHelper.ARTIST));
        String album = c.getString(c.getColumnIndex(FavoriteSongsTableHelper.ALBUM));
        long duration = c.getLong(c.getColumnIndex(MostAndRecentSongTableHelper.DURATION));
        String data = c.getString(c.getColumnIndex("_data"));
        long albumId = c.getLong(c.getColumnIndex(MostAndRecentSongTableHelper.ALBUM_ID));
        String composer = c.getString(c.getColumnIndex("composer"));
        songData.setId(_id);
        songData.setSongTitle(title);
        songData.setAlbum(album);
        songData.setArtist(artist);
        songData.setDuration(duration);
        songData.setSongPath(data);
        songData.setAlbumId(albumId);
        if (!data.endsWith(".mp3")) {
            if (!data.endsWith(".MP3")) {
            }
        }
        this.listOfSongs.add(songData);
    }
    c.close();
}

2 个答案:

答案 0 :(得分:1)

请找到下面的功能,它可以获得你想要的所有mp3歌曲。

public void getMp3Songs() {

    Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

    cursor = managedQuery(allsongsuri, STAR, selection, null, null);

    if (cursor != null) {
        if (cursor.moveToFirst()) {
            do {
                song_name = cursor
                        .getString(cursor
                                .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                int song_id = cursor.getInt(cursor
                        .getColumnIndex(MediaStore.Audio.Media._ID));

                String fullpath = cursor.getString(cursor
                        .getColumnIndex(MediaStore.Audio.Media.DATA));
                fullsongpath.add(fullpath);

                album_name = cursor.getString(cursor
                        .getColumnIndex(MediaStore.Audio.Media.ALBUM));
                int album_id = cursor.getInt(cursor
                        .getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));

                artist_name = cursor.getString(cursor
                        .getColumnIndex(MediaStore.Audio.Media.ARTIST));
                int artist_id = cursor.getInt(cursor
                        .getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));


            } while (cursor.moveToNext());

        }
        cursor.close();
        db.closeDatabase();
    }
}

答案 1 :(得分:0)

fun getAllAudioFromDevice(context: Context): List<AudioModel>? {
    val tempAudioList: MutableList<AudioModel> = ArrayList()
    val uri: Uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
    val projection = arrayOf<String>(
        MediaStore.Audio.AudioColumns.DATA,
        MediaStore.Audio.AudioColumns.TITLE,
        MediaStore.Audio.ArtistColumns.ARTIST,
    )
    val selection = MediaStore.Audio.Media.IS_MUSIC + " != 0"
    /*val projection = arrayOf(
        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
    )*/
    val c: Cursor? = context.contentResolver.query(
        uri, projection, selection, null, null
    )
    if (c != null) {
        while (c.moveToNext()) {
            val audioModel = AudioModel()
            val path = c.getString(0)
            val name = c.getString(1)
            val artist = c.getString(2)
            audioModel.trackName = name
            audioModel.trackDetail = artist
            audioModel.trackUrl = path
            if(path != null && (path.endsWith(".aac")
                        || path.endsWith(".mp3")
                        || path.endsWith(".wav")
                        || path.endsWith(".ogg")
                        || path.endsWith(".ac3")
                        || path.endsWith(".m4a"))) {

                Log.v(mTAG, "trackName :${audioModel.trackName}, trackDetail :${audioModel.trackDetail}, trackUrl :${audioModel.trackUrl}")
                tempAudioList.add(audioModel)
            }
        }
        c.close()
    }

    return tempAudioList
}