如何获得特定专辑的歌曲

时间:2018-02-17 11:29:00

标签: android media-queries android-mediaplayer

我想获得特定专辑的歌曲,就像我点击专辑时我应该只获得该专辑的歌曲。我得到所有专辑,当我点击专辑时,我得到设备上的所有歌曲。该专辑中没有。所以请告诉我该怎么做。我尝试了很多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);
    }


}

1 个答案:

答案 0 :(得分:0)

将其与notificationManagerInitialization一起使用 使用此nm.notify(NOTIFICATION_ID_OPEN_ACTIVITY,nc.build());函数,这是因为..在oreo ..

  • 您需要创建NotificationChannel类型的频道
  • 然后将频道添加到通知管理器... 就像我做的那样.. 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");

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

^[a-zA-Z][-_.a-zA-Z0-9]{5,29}@g(oogle)?mail.com$