下面列出了怎么用android.provider.MediaStore.Audio.Genres的API类实例代码及写法,或者点击链接到github查看源代码。
@NonNull
private static List<Genre> getGenresFromCursor(@NonNull final Context context, @Nullable final Cursor cursor) {
final List<Genre> genres = new ArrayList<>();
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
Genre genre = getGenreFromCursor(context, cursor);
if (genre.songCount > 0) {
genres.add(genre);
} else {
// try to remove the empty genre from the media store
try {
context.getContentResolver().delete(Genres.EXTERNAL_CONTENT_URI, Genres._ID + " == " + genre.id, null);
} catch (Exception e) {
e.printStackTrace();
// nothing we can do then
}
}
} while (cursor.moveToNext());
}
cursor.close();
}
return genres;
}
@NonNull
private static ArrayList<Genre> getGenresFromCursor(@NonNull final Context context, @Nullable final Cursor cursor) {
final ArrayList<Genre> genres = new ArrayList<>();
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
Genre genre = getGenreFromCursor(context, cursor);
if (genre.songCount > 0) {
genres.add(genre);
} else {
// try to remove the empty genre from the media store
try {
context.getContentResolver().delete(Genres.EXTERNAL_CONTENT_URI, Genres._ID + " == " + genre.id, null);
} catch (Exception e) {
e.printStackTrace();
// nothing we can do then
}
}
} while (cursor.moveToNext());
}
cursor.close();
}
return genres;
}
@NonNull
private static ArrayList<Genre> getGenresFromCursor(@NonNull final Context context, @Nullable final Cursor cursor) {
final ArrayList<Genre> genres = new ArrayList<>();
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
Genre genre = getGenreFromCursor(context, cursor);
if (genre.songCount > 0) {
genres.add(genre);
} else {
// try to remove the empty genre from the media store
try {
context.getContentResolver().delete(Genres.EXTERNAL_CONTENT_URI, Genres._ID + " == " + genre.id, null);
} catch (Exception e) {
e.printStackTrace();
// nothing we can do then
}
}
} while (cursor.moveToNext());
}
cursor.close();
}
return genres;
}
public void setupFragmentData(){
mAdapter = new GenreListAdapter(getActivity(), R.layout.music_listview_items, null,
new String[] {}, new int[] {}, 0);
mProjection = new String[] {
BaseColumns._ID, MediaColumns.TITLE, AudioColumns.ALBUM,
AudioColumns.ARTIST
};
StringBuilder where = new StringBuilder();
where.append(AudioColumns.IS_MUSIC + "=1").append(
" AND " + MediaColumns.TITLE + " != ''");
mWhere = where.toString();
mSortOrder = Genres.Members.DEFAULT_SORT_ORDER;
mUri = Genres.Members.getContentUri(EXTERNAL, getArguments().getLong(BaseColumns._ID));
mFragmentGroupId = 3;
mType = TYPE_GENRE;
mTitleColumn = MediaColumns.TITLE;
}
@NonNull
private static List<Genre> getGenresFromCursor(@NonNull final Context context, @Nullable final Cursor cursor) {
final List<Genre> genres = new ArrayList<>();
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
Genre genre = getGenreFromCursor(context, cursor);
if (genre.songCount > 0) {
genres.add(genre);
} else {
// try to remove the empty genre from the media store
try {
context.getContentResolver().delete(Genres.EXTERNAL_CONTENT_URI, Genres._ID + " == " + genre.id, null);
} catch (Exception e) {
e.printStackTrace();
// nothing we can do then
}
}
} while (cursor.moveToNext());
}
cursor.close();
}
return genres;
}
@Nullable
private static Cursor makeGenreSongCursor(@NonNull final Context context, int genreId) {
try {
return context.getContentResolver().query(
Genres.Members.getContentUri("external", genreId),
SongLoader.BASE_PROJECTION, SongLoader.BASE_SELECTION, null, PreferenceUtil.getInstance(context).getSongSortOrder());
} catch (SecurityException e) {
return null;
}
}
@Nullable
private static Cursor makeGenreCursor(@NonNull final Context context) {
final String[] projection = new String[]{
Genres._ID,
Genres.NAME
};
try {
return context.getContentResolver().query(
Genres.EXTERNAL_CONTENT_URI,
projection, null, null, PreferenceUtil.getInstance(context).getGenreSortOrder());
} catch (SecurityException e) {
return null;
}
}
@Nullable
private static Cursor makeGenreSongCursor(@NonNull final Context context, int genreId) {
try {
return context.getContentResolver().query(
Genres.Members.getContentUri("external", genreId),
SongLoader.BASE_PROJECTION, SongLoader.BASE_SELECTION, null, PreferenceUtil.getInstance(context).getSongSortOrder());
} catch (SecurityException e) {
return null;
}
}
@Nullable
private static Cursor makeGenreCursor(@NonNull final Context context) {
final String[] projection = new String[]{
Genres._ID,
Genres.NAME
};
try {
return context.getContentResolver().query(
Genres.EXTERNAL_CONTENT_URI,
projection, null, null, PreferenceUtil.getInstance(context).getGenreSortOrder());
} catch (SecurityException e) {
return null;
}
}
public static Cursor getGenreCursorForArtist(final Context context, long artistId) {
String query = " _id in (select genre_id from audio_genres_map where audio_id in (select _id from audio_meta where "+SongLoader.BASE_SELECTION+" AND artist_id = "+artistId+"))" ;
final String[] projection = new String[]{
Genres._ID,
Genres.NAME
};
try {
return context.getContentResolver().query(
Genres.EXTERNAL_CONTENT_URI,
projection, query, null, SortOrder.GenreSortOrder.GENRE_A_Z/*PreferenceUtil.getInstance(context).getGenreSortOrder()*/);
} catch (SecurityException e) {
return null;
}
}
@Nullable
private static Cursor makeGenreSongCursor(@NonNull final Context context, int genreId) {
try {
return context.getContentResolver().query(
Genres.Members.getContentUri("external", genreId),
SongLoader.BASE_PROJECTION, SongLoader.BASE_SELECTION, null, PreferenceUtil.getInstance().getSongSortOrder());
} catch (SecurityException e) {
return null;
}
}
@Nullable
private static Cursor makeGenreCursor(@NonNull final Context context) {
final String[] projection = new String[]{
Genres._ID,
Genres.NAME
};
try {
return context.getContentResolver().query(
Genres.EXTERNAL_CONTENT_URI,
projection, null, null, PreferenceUtil.getInstance().getGenreSortOrder());
} catch (SecurityException e) {
return null;
}
}
/**
* Initiate ViewPager and PagerAdapter
*/
private void initPager() {
// Initiate PagerAdapter
PagerAdapter mPagerAdapter = new PagerAdapter(getSupportFragmentManager());
if (ApolloUtils.isArtist(mimeType))
// Show all albums for an artist
mPagerAdapter.addFragment(new ArtistAlbumsFragment(bundle));
// Show the tracks for an artist or album
if(Playlists.CONTENT_TYPE.equals(mimeType)){
mPagerAdapter.addFragment(new PlaylistListFragment(bundle));
}
else if(Genres.CONTENT_TYPE.equals(mimeType)){
mPagerAdapter.addFragment(new GenreListFragment(bundle));
}
else if(ApolloUtils.isArtist(mimeType)){
mPagerAdapter.addFragment(new ArtistListFragment(bundle));
}
else if(Audio.Albums.CONTENT_TYPE.equals(mimeType)){
mPagerAdapter.addFragment(new AlbumListFragment(bundle));
}
// Set up ViewPager
mViewPager = (ViewPager)findViewById(R.id.viewPager);
mViewPager.setPageMargin(getResources().getInteger(R.integer.viewpager_margin_width));
mViewPager.setPageMarginDrawable(R.drawable.music_viewpager_margin);
mViewPager.setOffscreenPageLimit(mPagerAdapter.getCount());
mViewPager.setAdapter(mPagerAdapter);
mViewPager.setOnPageChangeListener(new PageListener());
}
/**
* Set the correct title
*/
private void setTitle() {
String name;
long id;
if (Playlists.CONTENT_TYPE.equals(mimeType)) {
id = bundle.getLong(BaseColumns._ID);
switch ((int)id) {
case (int)PLAYLIST_QUEUE:
setTitle(R.string.nowplaying);
return;
case (int)PLAYLIST_FAVORITES:
setTitle(R.string.favorite);
return;
default:
if (id < 0) {
setTitle(R.string.app_name);
return;
}
}
name = MusicUtils.getPlaylistName(this, id);
} else if (Audio.Artists.CONTENT_TYPE.equals(mimeType)) {
id = bundle.getLong(BaseColumns._ID);
name = getString (R.string.artist_page_title)+MusicUtils.getArtistName(this, id, true);
} else if (Audio.Albums.CONTENT_TYPE.equals(mimeType)) {
id = bundle.getLong(BaseColumns._ID);
name = getString (R.string.album_page_title)+MusicUtils.getAlbumName(this, id, true);
} else if (Genres.CONTENT_TYPE.equals(mimeType)) {
id = bundle.getLong(BaseColumns._ID);
name = MusicUtils.parseGenreName(this, MusicUtils.getGenreName(this, id, true));
} else {
setTitle(R.string.app_name);
return;
}
setTitle(name);
}
@Nullable
private static Cursor makeGenreSongCursor(@NonNull final Context context, int genreId) {
try {
return context.getContentResolver().query(
Genres.Members.getContentUri("external", genreId),
SongLoader.BASE_PROJECTION, SongLoader.BASE_SELECTION, null, PreferenceUtil.getInstance(context).getSongSortOrder());
} catch (SecurityException e) {
return null;
}
}
@Nullable
private static Cursor makeGenreCursor(@NonNull final Context context) {
final String[] projection = new String[]{
Genres._ID,
Genres.NAME
};
try {
return context.getContentResolver().query(
Genres.EXTERNAL_CONTENT_URI,
projection, null, null, PreferenceUtil.getInstance(context).getGenreSortOrder());
} catch (SecurityException e) {
return null;
}
}