2

我可以列出媒体商店中的所有歌曲,但我也想按艺术家/专辑/文件夹/流派/播放列表对它们进行排序。

我通常使用游标查询自己的数据库,但这在这种情况下无效,我必须使用适配器。我可以通过更改参数来查询它,但我不知道如何在适配器中对其进行分组(我找不到如何添加/使用 GROUPBY),这意味着我需要适配器中的每个艺术家/专辑/文件夹/..只有一次,不是每首歌。

我试图通过互联网搜索,但没有找到答案。我也在这里找到了一些主题,但对我来说仍然不清楚。因此,我想请您在此示例中向我展示:

package com.exercise.AndroidListMedia;

import android.app.ListActivity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Video.Media;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class AndroidListMediaActivity extends ListActivity {

 SimpleCursorAdapter adapter;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  String[] from = {
    MediaStore.MediaColumns.TITLE};
  int[] to = {
    android.R.id.text1};

  Cursor cursor = managedQuery(
    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
    null,
    null,
    null,
    MediaStore.Audio.Media.TITLE);

  adapter = new SimpleCursorAdapter(this,
    android.R.layout.simple_list_item_1, cursor, from, to);
  setListAdapter(adapter);
  }

 @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {
  Cursor cursor = adapter.getCursor();
  cursor.moveToPosition(position);

  String _id = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media._ID));
  String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
  String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
  String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
  int duration = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));

  String info = "_ID: " + _id + "\n"
 + "TITLE: " + title + "\n"
 + "ARTIST: " + artist + "\n"
 + "ALBUM: " + album + "\n"
 + "DURATION: " + duration/1000 + "s";

  Toast.makeText(this, info, Toast.LENGTH_LONG).show();
 }
}

谢谢你的任何想法。

4

2 回答 2

6

这将为您提供所有专辑:

public Cursor getAlbumAlbumcursor(Context context, Cursor cursor)
{
    String where = null;
    ContentResolver cr = context.getContentResolver();
    final Uri uri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
    final String _id = MediaStore.Audio.Albums._ID; 
    final String album_id = MediaStore.Audio.Albums.ALBUM_ID; 
    final String album_name =MediaStore.Audio.Albums.ALBUM;
    final String artist = MediaStore.Audio.Albums.ARTIST;
    final String[]columns={_id,album_name, artist};
    cursor = cr.query(uri,columns,where,null, null);
    return cursor;
}

这将为您提供所有曲目:

public Cursor getTrackTrackcursor(Context context, Cursor cursor)
{
    final String track_id = MediaStore.Audio.Media._ID; 
    final String track_no =MediaStore.Audio.Media.TRACK;
    final String track_name =MediaStore.Audio.Media.TITLE;
    final String artist = MediaStore.Audio.Media.ARTIST;
    final String duration = MediaStore.Audio.Media.DURATION;
    final String album = MediaStore.Audio.Media.ALBUM;
    final String composer = MediaStore.Audio.Media.COMPOSER;
    final String year = MediaStore.Audio.Media.YEAR;
    final String path = MediaStore.Audio.Media.DATA;
    Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

    ContentResolver cr =  context.getContentResolver();
    final String[]columns={track_id, track_no, artist, track_name,album, duration, path, year, composer};
    cursor = cr.query(uri,columns,null,null,null);
    return cursor;
}

这将为您提供所有播放列表:

 public  Cursor getandroidPlaylistcursor(Context context,Cursor cursor)
 {
     ContentResolver resolver = context.getContentResolver();
     final Uri uri=MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
     final String id = MediaStore.Audio.Playlists._ID;
     final String name = MediaStore.Audio.Playlists.NAME;
     final String[]columns = {id,name};
     final String criteria = MediaStore.Audio.Playlists.NAME.length() + " > 0 " ;
     final Cursor crplaylists = resolver.query(uri, columns, criteria, null,name + "    `ASC");
     return crplaylists;
 }

注意不同 uri 的 MediaStore.Audio.Media 用于曲目,MediaStore.Audio.Albums 用于专辑详细信息,MediaStore.Audio.Playlists 用于播放列表。

cr.query 中的最后一个参数是排序顺序,我的播放列表显示了一个示例:名称 + "`ASC" 按播放列表名称排序。我希望这有帮助

我开发了一个应用程序,允许您管理/创建播放列表并使用这些技术。 https://play.google.com/store/apps/details?id=com.flyingdutchman.playlistmanager&hl=en 或在 Google Play 中搜索播放列表管理器。

于 2013-09-23T19:40:29.210 回答
0
    public ArrayList<SongBeen> getAlbumList() {

            ArrayList<SongBeen> albumList=new ArrayList<SongBeen>();
            ContentResolver musicResolver = activity.getContentResolver();
            Uri musicUri =    android.provider.MediaStore.Audio.**Albums**.EXTERNAL_CONTENT_URI;
            Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
            if(musicCursor!=null && musicCursor.moveToFirst()){

                int titleColumn = musicCursor.getColumnIndex
                        (android.provider.MediaStore.Audio.Media.TITLE);
                int idColumn = musicCursor.getColumnIndex
                        (android.provider.MediaStore.Audio.Media._ID);
                int artistColumn = musicCursor.getColumnIndex
                        (android.provider.MediaStore.Audio.Media.ARTIST);
                int albumColumn = musicCursor.getColumnIndex
                        (android.provider.MediaStore.Audio.Media.ALBUM);


                //add songs to list
                do {
                    SongBeen been=new SongBeen();


                    been.setId(musicCursor.getLong(idColumn));
                    been.setAlbum(musicCursor.getString(albumColumn));
                    been.setArtist(musicCursor.getString(artistColumn));


                    albumList.add(been);
            } 
                while (musicCursor.moveToNext());
            }
            return albumList;

        }

笔记:

改变专辑或任何其他我们能得到的东西

Uri musicUri = android.provider.MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;

于 2015-05-26T06:46:49.540 回答