【问题标题】:how to get songs of particular album如何获取特定专辑的歌曲
【发布时间】:2023-04-04 13:51:02
【问题描述】:

我想获取特定专辑的歌曲,例如当我单击专辑时,我应该只获取该专辑的歌曲。我正在获取所有专辑,当我单击专辑时,我正在获取设备上存在的所有歌曲。不是该专辑的歌曲。所以请告诉我该怎么做。我尝试了许多 stackoverflow 答案,但没有得到答案。

public class albumSongs extends AppCompatActivity implements SearchView.OnQueryTextListener {

    ArrayList<albumInfo>albumSongList=new ArrayList<>();
    RecyclerView recyclerView2;
    public albumsongAdapter albumsongAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_album_songs);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("try");
        getSupportActionBar().setDisplayShowTitleEnabled(true);
        recyclerView2 = (RecyclerView) findViewById(R.id.recyclerView2);
        recyclerView2.setHasFixedSize(true);
        albumsongAdapter = new albumsongAdapter(albumSongList,this);
        SearchView searchView=(SearchView)findViewById(R.id.search);
        recyclerView2.setAdapter(albumsongAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView2.getContext(),
                linearLayoutManager.getOrientation());
        recyclerView2.setLayoutManager(linearLayoutManager);
        recyclerView2.addItemDecoration(dividerItemDecoration);
        getSongsPerAlbum("");
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        final MenuItem item = menu.findItem(R.id.search_view);
        final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
        searchView.setOnQueryTextListener(this);
        searchView.setQueryHint("search songs");
        MenuItemCompat.setOnActionExpandListener(item,new MenuItemCompat.OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionExpand(MenuItem menuItem) {
                // Return true to allow the action view to expand
                return true;
            }
            @Override
            public boolean onMenuItemActionCollapse(MenuItem menuItem) {
                // When the action view is collapsed, reset the query
                albumsongAdapter.setSearchResult(albumSongList);
                // Return true to allow the action view to collapse
                return true;
            }
        });
        return true;
    }
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    public boolean onQueryTextChange(String newText) {
        final List<albumInfo> filteredModelList = filter(albumSongList, newText);
        albumsongAdapter.setSearchResult(filteredModelList);
        return true;
    }


    private List<albumInfo> filter(ArrayList<albumInfo> models, String query) {
        query = query.toLowerCase();
        final List<albumInfo> filteredModelList = new ArrayList<>();
        for (albumInfo model : models) {
            final String text = model.getX().toLowerCase();
            if (text.contains(query)) {
                filteredModelList.add(model);
            }
        }
        return filteredModelList;
    }
    public void getSongsPerAlbum(String AlbumId) {
        final Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        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 albumid = MediaStore.Audio.Media.ALBUM_ID;
        final String composer = MediaStore.Audio.Media.COMPOSER;
        final String year = MediaStore.Audio.Media.YEAR;
        final String path = MediaStore.Audio.Media.DATA;
        final String date_added = MediaStore.Audio.Media.DATE_ADDED;
        String[] columns = new String[]{
                track_id,
                track_no,
                track_name,
                artist,
                duration,
                album,
                albumid,
                composer,
                year,
                path,
                date_added
        };
        // Get all tracks if no album id
        Cursor csr;
        if (AlbumId == null || AlbumId.length() < 1) {
            csr = this.getContentResolver().query(uri, columns,null,null,track_no);
        } else {
            // Doesn't work think media has to be scanned first before albums are generated
            csr = this.getContentResolver().query(uri, columns,album + "=?",new String[]{AlbumId},track_no);
        }

        for (String s: csr.getColumnNames()
                ) {
            Log.d("SONGCOLUMN","Column = " + s);
        }
        while (csr.moveToNext()) {
            String id = (csr.getString(csr.getColumnIndex(track_name)));
            String thisalbumId=(csr.getString(csr.getColumnIndex(path)));
            String v = (csr.getString(csr.getColumnIndex(albumid)));
            Log.d("SONG","Name of Song is " + csr.getString(csr.getColumnIndex(track_name)) +
                    " from Album " + csr.getString(csr.getColumnIndex(album))
            );

                       albumInfo s = new albumInfo(id, v, thisalbumId);
                       albumSongList.add(s);

        }
        csr.close();
        //  recyclerView2.setAdapter(albumsongAdapter);
    }


}

【问题讨论】:

  • 你在用奥利奥吗?
  • 我已经更新了答案,请检查并更新它是否有效..

标签: android media-queries android-mediaplayer


【解决方案1】:

将此与您的 notificationManagerInitialization 一起使用 使用这个openActivityNotification(Context context)函数,这是因为..在奥利奥..

  • 您需要创建一个 NotificationChannel 类型的频道
  • 然后将频道添加到通知管理器... 像我一样..nm.notify(NOTIFICATION_ID_OPEN_ACTIVITY,nc.build());

     public static void openActivityNotification(Context context)
     { 
       NotificationCompat.Builder nc = new 
                 NotificationCompat.Builder(this,"default");
    
           if (Build.VERSION.SDK_INT < 26) {
    
             nm =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    
    }
    else
       {
              nm =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationChannel channel = new NotificationChannel("default",
            "Channel name",
            NotificationManager.IMPORTANCE_DEFAULT);
    channel.setDescription("Channel description");
    
    if (notificationManager != null) {
        nm.createNotificationChannel(channel);
    }
    }
    Intent notifyIntent=new Intent(context, BlankFragment2.class);
    notifyIntent
        .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_
                                                                   TASK);
    
    PendingIntent pendingIntent=PendingIntent.getActivity(context,0,notifyIntent,PendingIntent.FLAG_UPDATE_CURRENT);
    nc.setSmallIcon(R.mipmap.ic_launcher);
    nc.setAutoCancel(true);
    nc.setContentTitle("notification demo");
    nc.setContentText("click please");
    nm.notify(NOTIFICATION_ID_OPEN_ACTIVITY,nc.build());
    
     }
    

编辑

if (Build.VERSION.SDK_INT < 26) {

             nm =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    }
    else
       {
        nm =(NotificationManager) 
                   context.getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationChannel channel = new NotificationChannel("default",
            "Channel name",
            NotificationManager.IMPORTANCE_DEFAULT);
    channel.setDescription("Channel description");

    if (notificationManager != null) {
        nm.createNotificationChannel(channel);
    }
    }

像这样初始化通知管理器而不是弃用的通知构建器使用这个...

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,"default");

【讨论】:

  • 您也必须为自定义大通知做同样的事情......。我也在为此更新..如果你不明白..
  • 对于音量控制以外的操作,不推荐使用流类型。请参阅 setSound() 的文档,了解如何使用 android.media.AudioAttributes 来限定您的播放用例。
  • 尝试更改通知ID,然后使用最新的notification.builder compat,因为我已经更新了..并通知..
  • 我无法将“默认”应用到 Builder。
  • 然后将其更改为其他内容.. "yourChannel"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多