首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android -为android创建音乐播放器,比如Google的Play music?

问Android -为android创建音乐播放器,比如Google的Play music?
EN

Stack Overflow用户
提问于 2016-03-05 05:29:00
回答 1查看 1.3K关注 0票数 0

我想为android创建一个音乐播放器,比如Play music,所以我从github引入了Google的环球音乐播放器,但是我不知道在哪里插入代码,这样它就可以从本地存储区读取音乐。我知道一切都提供背景音乐服务,通知酒吧等。但我不知道怎么从手机存储器里读取音乐文件。

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

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

EN

回答 1

Stack Overflow用户

发布于 2016-03-20 16:13:01

RECOMMENDATION

嗨!使用简单的音乐播放器查看我的存储库。歌曲从外部存储加载。支持预棒棒糖。

溶液

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

私有ArrayList歌曲=新的ArrayList<>();// Song是一些数据模型

代码语言:javascript
复制
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();
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35810459

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档