/** 根据Id寻找歌曲 */ public static Song findSongById(int songId) { for (Song s : mSongLib) { if (s.getmSongId() == songId) { return s; } } return null; }
/** 获取一个类型下的所有歌曲 */ public static ArrayList<Song> getGenreSongs(Genre genre) { ArrayList<Song> songs = new ArrayList<>(); for (Song s : mSongLib) { if (s.getmGenreId() == genre.getmGenreId()) { songs.add(s); } } return songs; }
/** 获取该歌手的所有歌曲 */ public static ArrayList<Song> getArtistSong(Artist artist) { ArrayList<Song> songs = new ArrayList<>(); for (Song s : mSongLib) { if (s.getmArtistId() == artist.getmArtistId()) { songs.add(s); } } return songs; }
/** * 获取专辑下的所有歌曲 * * @return 同一专辑的所有歌 */ public static ArrayList<Song> getAlbumSongs(Album album) { ArrayList<Song> songs = new ArrayList<>(); for (Song s : mSongLib) { if (s.getmAlbumId() == album.getmAlbumId()) { songs.add(s); } } return songs; }
/** 扫描类型, 并将Song与Genre关联 */ public static ArrayList<Genre> scanGenres(Context context) { LogUtils.v(TAG, "scanGenres() called"); ArrayList<Genre> genres = new ArrayList<>(); Cursor cursor = context .getContentResolver() .query( MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI, genreProjection, null, null, MediaStore.Audio.Genres.NAME + " ASC"); LogUtils.v(TAG, cursor.getCount() + ""); for (int i = 0; i < cursor.getCount(); i++) { cursor.moveToPosition(i); int genreId = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Genres._ID)); String genreName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Genres.NAME)); // LogUtils.v(TAG, "get genres : id = " + genreId + " name = " + genreName); Genre genre = new Genre(genreId, genreName); genres.add(genre); // 将歌曲与类型关联 Cursor genreCursor = context .getContentResolver() .query( MediaStore.Audio.Genres.Members.getContentUri("external", genreId), new String[] {MediaStore.Audio.Media._ID}, MediaStore.Audio.Media.IS_MUSIC + " != 0 ", null, null); genreCursor.moveToFirst(); for (int j = 0; j < genreCursor.getCount(); j++) { genreCursor.moveToPosition(j); int index = genreCursor.getColumnIndex(MediaStore.Audio.Media._ID); final Song song = findSongById(genreCursor.getInt(index)); if (song != null) { song.setmGenreId(genreId); } } genreCursor.close(); } cursor.close(); return genres; }
/** 向歌单添加音乐 */ public static void addSongToPlaylist( final Context context, final PlayList playlist, final Song song) { Cursor cur = context .getContentResolver() .query( MediaStore.Audio.Playlists.Members.getContentUri( "external", playlist.getmPlayListId()), null, null, null, MediaStore.Audio.Playlists.Members.TRACK + " ASC"); long count = 0; if (cur.moveToLast()) { count = cur.getLong(cur.getColumnIndex(MediaStore.Audio.Playlists.Members.TRACK)); } cur.close(); ContentValues values = new ContentValues(); values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, count + 1); values.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, song.getmSongId()); Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlist.getmPlayListId()); ContentResolver resolver = context.getContentResolver(); resolver.insert(uri, values); resolver.notifyChange(Uri.parse("content://media"), null); }
/** 获取歌曲的专辑,每首歌只有一个专辑 */ public static ArrayList<Album> getSongAlbum(Song song) { ArrayList<Album> albums = new ArrayList<>(); for (Album a : mAlbumLib) { if (a.getmAlbumId() == song.getmAlbumId()) { albums.add(a); break; } } return albums; }