媒体播放器:专辑并显示其中的歌曲

发布于 2024-12-29 22:03:25 字数 3159 浏览 2 评论 0原文

当我们点击专辑时,它应该显示专辑中的歌曲列表,我尝试这样做,但它显示了 SD 卡 中的所有歌曲。

private OnItemClickListener musicgridlistener1 = new OnItemClickListener(){
    public void onItemClick(AdapterView parent, View v, int position,long id)
    {
        System.gc();
        album_column_index1 = musiccursor
                .getColumnIndexOrThrow(MediaStore.Audio.AlbumColumns.ALBUM_ART);
        musiccursor.moveToPosition(position);
        MUSICfilename = musiccursor.getString(album_column_index1);

        Log.i("=======this is file name of album========",""+musiccursor.getCount()+" "+MUSICfilename+"  "+position);

        String[] ablummusicproj = {MediaStore.Audio.Media.DATA,
                MediaStore.Audio.AlbumColumns.ALBUM,
                MediaStore.Audio.AlbumColumns.ARTIST,
                MediaStore.Audio.Media.DISPLAY_NAME};
        final String sortOrder = MediaStore.Audio.Media.ALBUM_KEY + " COLLATE LOCALIZED ASC";
        //final String sortOrder= MediaStore.Audio.Media.ALBUM_KEY + " ASC, "
               // + MediaStore.Audio.Media.TRACK + " ASC, "
              //  + MediaStore.Audio.Media.TITLE_KEY + " ASC";

        Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        musicAlbum = getBaseContext().getContentResolver().query(uri, ablummusicproj, null, null, sortOrder);
        count = musicAlbum.getCount();
            Log.i("++++++++++ value of album cursor is ",""+count);
        gridmusic = (GridView) findViewById(R.id.gridmusic);
        madAdapter_internal = new MusicalbumAdapter_internal(getApplicationContext());
        gridmusic.setAdapter(madAdapter_internal);
        gridmusic.setOnItemClickListener(musicgridlistener);
        musiccursor=musicAlbum;
    }
};

MusicalbumAdapter_internal

public class MusicalbumAdapter_internal extends BaseAdapter {
    private Context maContext;

    public MusicalbumAdapter_internal(Context c) {
        maContext = c;
        Log.i("++++++++++++++++++", "this is context");
    }

    public int getCount() {
        return count;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        System.gc();

        TextView tv1 = new TextView(maContext.getApplicationContext());
        String id = null;
        if (convertView == null) {

                music_column_index = musicAlbum.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.DISPLAY_NAME);


                musicAlbum.moveToPosition(position);
            id = musicAlbum.getString(music_column_index);
            //music_column_index = musiccursor
                    //.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
            musicAlbum.moveToPosition(position);
            //id += " Size(KB):" + musiccursor.getString(music_column_index);
            tv1.setLayoutParams(new GridView.LayoutParams(100, 100));
            tv1.setText(id);

        } else
            tv1 = (TextView) convertView;
        return tv1;
    }
}

When we click on albums than it should display list of songs in album, I try like this but it displays all the songs in SD card.

private OnItemClickListener musicgridlistener1 = new OnItemClickListener(){
    public void onItemClick(AdapterView parent, View v, int position,long id)
    {
        System.gc();
        album_column_index1 = musiccursor
                .getColumnIndexOrThrow(MediaStore.Audio.AlbumColumns.ALBUM_ART);
        musiccursor.moveToPosition(position);
        MUSICfilename = musiccursor.getString(album_column_index1);

        Log.i("=======this is file name of album========",""+musiccursor.getCount()+" "+MUSICfilename+"  "+position);

        String[] ablummusicproj = {MediaStore.Audio.Media.DATA,
                MediaStore.Audio.AlbumColumns.ALBUM,
                MediaStore.Audio.AlbumColumns.ARTIST,
                MediaStore.Audio.Media.DISPLAY_NAME};
        final String sortOrder = MediaStore.Audio.Media.ALBUM_KEY + " COLLATE LOCALIZED ASC";
        //final String sortOrder= MediaStore.Audio.Media.ALBUM_KEY + " ASC, "
               // + MediaStore.Audio.Media.TRACK + " ASC, "
              //  + MediaStore.Audio.Media.TITLE_KEY + " ASC";

        Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        musicAlbum = getBaseContext().getContentResolver().query(uri, ablummusicproj, null, null, sortOrder);
        count = musicAlbum.getCount();
            Log.i("++++++++++ value of album cursor is ",""+count);
        gridmusic = (GridView) findViewById(R.id.gridmusic);
        madAdapter_internal = new MusicalbumAdapter_internal(getApplicationContext());
        gridmusic.setAdapter(madAdapter_internal);
        gridmusic.setOnItemClickListener(musicgridlistener);
        musiccursor=musicAlbum;
    }
};

MusicalbumAdapter_internal Class

public class MusicalbumAdapter_internal extends BaseAdapter {
    private Context maContext;

    public MusicalbumAdapter_internal(Context c) {
        maContext = c;
        Log.i("++++++++++++++++++", "this is context");
    }

    public int getCount() {
        return count;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        System.gc();

        TextView tv1 = new TextView(maContext.getApplicationContext());
        String id = null;
        if (convertView == null) {

                music_column_index = musicAlbum.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.DISPLAY_NAME);


                musicAlbum.moveToPosition(position);
            id = musicAlbum.getString(music_column_index);
            //music_column_index = musiccursor
                    //.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
            musicAlbum.moveToPosition(position);
            //id += " Size(KB):" + musiccursor.getString(music_column_index);
            tv1.setLayoutParams(new GridView.LayoutParams(100, 100));
            tv1.setText(id);

        } else
            tv1 = (TextView) convertView;
        return tv1;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世界等同你 2025-01-05 22:03:25

当你获取专辑时,你必须存储 album_key 。

mediaStoreURI = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            String selection = MediaStore.Audio.Media.ALBUM_KEY + "=?";
            String[] projection = new String[] { MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA,
                    MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.ALBUM_ID, MediaStore.Audio.Media.ARTIST,
                    "strftime(\"%H-%M-%S\", (" + MediaStore.Audio.Media.DURATION + "/1000), \"unixepoch\") as time" };
            String[] selectionArgs = new String[] { ((MediaContainerUSB) currentMediaObject).getUSBGroupingKey() };
            String sortOrder = MediaStore.Audio.Media.TRACK;

album_key 是为 SD 卡中的所有专辑生成的,并存储在不同的表中。

you would have to store the album_key when u get the albums.

mediaStoreURI = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            String selection = MediaStore.Audio.Media.ALBUM_KEY + "=?";
            String[] projection = new String[] { MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA,
                    MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.ALBUM_ID, MediaStore.Audio.Media.ARTIST,
                    "strftime(\"%H-%M-%S\", (" + MediaStore.Audio.Media.DURATION + "/1000), \"unixepoch\") as time" };
            String[] selectionArgs = new String[] { ((MediaContainerUSB) currentMediaObject).getUSBGroupingKey() };
            String sortOrder = MediaStore.Audio.Media.TRACK;

the album_key is generated for ever album in the sdcard and stored in a different table as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文