[英]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. 我想为Play音乐创建适用于Android的音乐播放器,因此我从github导入了Google的Google Universal音乐播放器,但我不知道要在哪里插入代码,以便从本地存储读取音乐。 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 通用音乐播放器-https: //github.com/googlesamples/android-UniversalMusicPlayer
I have made some simple apps in android but not created any app of this complexity. 我在android中做了一些简单的应用程序,但没有创建任何这种复杂性的应用程序。 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<>(); 私有ArrayList歌曲=新的ArrayList <>(); // Song is some data model // Song是一些数据模型
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.