没有从专辑中获取歌曲

时间:2018-03-31 09:03:13

标签: java android android-mediaplayer media-player

我试图在我的应用中从专辑中获取歌曲。我在网上写了一些代码并在互联网上提供了一些帮助,但现在我将所有项目都设为0.我不知道为什么我会得到0。

public void songs()
{
    String name;
    long genreId;
    Uri uri;
    Cursor genrecursor;
    Cursor tempcursor;
    String[] proj1 = { android.provider.MediaStore.Audio.Albums._ID,
            android.provider.MediaStore.Audio.Albums.ALBUM};
    String[] proj2 = {MediaStore.Audio.Media.DISPLAY_NAME,MediaStore.Audio.Media.ALBUM_ID
            ,MediaStore.Audio.Media.DATA};

    genrecursor = managedQuery(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, proj1, null, null, null);
    if (genrecursor.moveToFirst()) {
        do {
            name = String.valueOf(genrecursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM));
            Log.i("Tag-album name", genrecursor.getString(Integer.parseInt(name)));

            name = String.valueOf(genrecursor.getColumnIndexOrThrow(MediaStore.Audio.Albums._ID));
            genreId = Long.parseLong(genrecursor.getString(Integer.parseInt(name)));
            uri = MediaStore.Audio.Genres.Members.getContentUri("external", genreId);

            tempcursor = managedQuery(uri, proj2, null, null, null);
            Log.i("Tag-Number of songs for this album", tempcursor.getCount() + "");
            if (tempcursor.moveToFirst()) {
                do {
                    name = String.valueOf(tempcursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
                    String art = String.valueOf(tempcursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
                    String path = String.valueOf(tempcursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
                    Log.i("Tag-Song name", tempcursor.getString(Integer.parseInt(name)));
                    albumInfo s = new albumInfo(name,art,path);
                    albumSongList.add(s);
                } while (tempcursor.moveToNext());
            }
        } while (genrecursor.moveToNext());
    }
}

1 个答案:

答案 0 :(得分:0)

我这样做了。

    Uri uri= MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;

    String[] columns = {
            MediaStore.Audio.Albums.ALBUM};

    Cursor cursor =getApplicationContext().getContentResolver().
            query(uri, columns, null,
                    null, null);

    if (cursor != null && cursor.moveToFirst()) {
        do {
            Log.v("bbb",
                    cursor.getString(cursor
                            .getColumnIndex(MediaStore.Audio.Albums.ALBUM)));
            v=cursor.getString(cursor
                    .getColumnIndex(MediaStore.Audio.Albums.ALBUM));
        } while (cursor.moveToNext());
    }
    // I want to list down song in album Rolling Papers (Deluxe Version)

    String[] column = { MediaStore.Audio.Media.DATA,
            MediaStore.Audio.Media.ALBUM_ID,
            MediaStore.Audio.Media.DISPLAY_NAME,
    };


    Intent i=getIntent();
    final String value=i.getStringExtra("sol");
    String [] x=new String[]{value};
    String where = MediaStore.Audio.Albums.ALBUM + "=?";
    Log.e("syn",v);
    String whereVal[] = {v};

    String orderBy = android.provider.MediaStore.Audio.Media.TITLE;

    cursor =getApplicationContext().getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            column, where, x,null);

    if (cursor != null && cursor.moveToFirst()) {
        do {
            Log.v("Vipul",
                    cursor.getString(cursor
                            .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)));
            String name=cursor.getString(cursor
                    .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
            String art=cursor.getString(cursor
                    .getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
            String path=cursor.getString(cursor
                    .getColumnIndex(MediaStore.Audio.Media.DATA));


            albumInfo s = new albumInfo(name,art,path); // EXCLUDED
            albumSongList.add(s);

        } while (cursor.moveToNext());
    }

}