Android - 为Android创建音乐播放器,如Google Play Play音乐?

时间:2016-03-05 05:29:00

标签: android android-music-player

我想创建一个像Play Music这样的Android音乐播放器,所以我从github导入了Google的环球音乐播放器,但我无法确定插入代码的位置,以便从本地存储中读取音乐。我知道一切都提供了背景音乐服务,通知栏等。但我无法弄清楚如何从手机存储中读取音乐文件。

环球音乐播放器 - https://github.com/googlesamples/android-UniversalMusicPlayer

我在android中制作了一些简单的应用程序但没有创建任何这种复杂的应用程序。我介于初学者和中级之间。请帮忙!

1 个答案:

答案 0 :(得分:0)

<强>建议

嗨!使用简单的音乐播放器查看my repository。歌曲是从外部存储装入的。支持Pre-Lollipop。

<强>解

作为解决歌曲加载问题的解决方案:您可以从外部存储加载歌曲,如下所示:

private ArrayList songs = new ArrayList&lt;&gt;(); //歌曲是一些数据模型

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();
    }