简体   繁体   中英

Android - Create music player for android like Play Music from Google?

I want to create a music player for android like Play Music so i imported Universal Music Player by Google from github, but i can't figure out where to insert code so that it reads music from local storage. I know everything is provided Background Music Service, Notification Bar etc.. But i can't figure out how to read music files from the phone storage.

Universal Music Player - https://github.com/googlesamples/android-UniversalMusicPlayer

I have made some simple apps in android but not created any app of this complexity. And i am somewhere between beginner and intermediate. Please help!

RECOMMENDATION

Hi! Check out my repository with simple music player. Songs are loaded from external storage. Pre-Lollipop is supported.

SOLUTION

As a solution for your problem with songs loading: you can load songs from external storage like this:

private ArrayList songs = new ArrayList<>(); // Song is some data model

public void getSongList() {
        ContentResolver musicResolver = getContentResolver();
        Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        Uri artworkUri = Uri.parse("content://media/external/audio/albumart");

        Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);

        if (musicCursor != null && musicCursor.moveToFirst()) {
            // song title
            int titleColumn = musicCursor.getColumnIndex
                    (MediaStore.Audio.Media.TITLE);
            // song unique id
            int idColumn = musicCursor.getColumnIndex
                    (MediaStore.Audio.Media._ID);
            // song's artist
            int artistColumn = musicCursor.getColumnIndex
                    (MediaStore.Audio.Media.ARTIST);
            // path to album art
            int albumIdColumn = musicCursor.getColumnIndex
                    (MediaStore.Audio.Media.ALBUM_ID);

            do {
                long thisId = musicCursor.getLong(idColumn);
                String thisTitle = musicCursor.getString(titleColumn);
                String thisArtist = musicCursor.getString(artistColumn);
                long albumId = musicCursor.getLong(albumIdColumn);
                String albumArt = "";

                try {
                    albumArt = (ContentUris.withAppendedId(artworkUri, albumId)).toString();
                    Log.d(TAG, "TEST Album art: " + albumArt);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                // Adding new song to ArrayList
                songs.add(new Song(thisId, thisTitle, thisArtist, albumArt));
            }
            while (musicCursor.moveToNext());
        }

        if (musicCursor != null)
            musicCursor.close();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM