Back to project page LocalMusicPlayer.
The source code is released under:
MIT License
If you think the Android project LocalMusicPlayer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.yprikhodko.localmusicplayer; /*from w ww .jav a 2 s.c o m*/ import android.app.Activity; import android.content.ContentResolver; import android.content.ContentUris; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.support.v4.app.Fragment; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; import com.sothree.slidinguppanel.SlidingUpPanelLayout; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; /** * Fragment for displaying songs and music player */ public class SongsFragment extends Fragment { private RecyclerView songView; private ArrayList<Song> songList; /** * The fragment argument representing the section number for this * fragment. */ private static final String ARG_SECTION_NUMBER = "section_number"; /** * Returns a new instance of this fragment for the given section * number. */ public static SongsFragment newInstance(int sectionNumber) { SongsFragment fragment = new SongsFragment(); Bundle args = new Bundle(); args.putInt(ARG_SECTION_NUMBER, sectionNumber); fragment.setArguments(args); return fragment; } public SongsFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_start, container, false); songList = ((StartActivity) getActivity()).getSongs(); songView = (RecyclerView) rootView.findViewById(R.id.song_list); songView.setHasFixedSize(true); GridLayoutManager grid = new GridLayoutManager(getActivity(), 2); grid.setOrientation(LinearLayoutManager.VERTICAL); songView.setLayoutManager(grid); getSongList(); // sort the tracks Collections.sort(songList, new Comparator<Song>() { public int compare(Song a, Song b) { return a.getTitle().compareTo(b.getTitle()); } }); ((StartActivity) getActivity()).setSongs(songList); SongAdapter songAdt = new SongAdapter(songList, getActivity()); songView.setAdapter(songAdt); songView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener(){ @Override public void onItemClick(View view, int position) { MusicService musicService = ((StartActivity) getActivity()).getMusicService(); musicService.setSong(position); musicService.togglePlay(); } })); return rootView; } @Override public void onAttach(Activity activity) { super.onAttach(activity); } public void getSongList() { //retrieve song info ContentResolver musicResolver = getActivity().getContentResolver(); String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0"; Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; Cursor musicCursor = musicResolver.query(musicUri, null, selection, null, null); if (musicCursor != null && musicCursor.moveToFirst()) { //get columns int titleColumn = musicCursor.getColumnIndex (MediaStore.Audio.Media.TITLE); int idColumn = musicCursor.getColumnIndex (MediaStore.Audio.Media._ID); int artistColumn = musicCursor.getColumnIndex (MediaStore.Audio.Media.ARTIST); int albumColumn = musicCursor.getColumnIndex (MediaStore.Audio.Media.ALBUM_ID); //add songs to list do { long thisId = musicCursor.getLong(idColumn); long albumId = musicCursor.getLong(albumColumn); String thisTitle = musicCursor.getString(titleColumn); String thisArtist = musicCursor.getString(artistColumn); final Uri ART_CONTENT_URI = Uri.parse("content://media/external/audio/albumart"); Uri albumArtUri = ContentUris.withAppendedId(ART_CONTENT_URI, albumId); songList.add(new Song(thisId, thisTitle, thisArtist, albumArtUri)); } while (musicCursor.moveToNext()); musicCursor.close(); } } }