音乐查找
Posted 红领巾传递人
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了音乐查找相关的知识,希望对你有一定的参考价值。
Main:
package net.bwie.mediaplayer;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ListView;
import net.bwie.mediaplayer.bean.MediaInfo;
import java.util.ArrayList;
import java.util.List;
/**
* 1、查询全部歌曲信息并展示(ContentProvider数据库)
* 2、点击对应歌曲播放/暂停
* 3、播放/暂停按钮。上一首/下一首
* 注意:设置外部存储权限
* build.gradle中targetSdkVersion 22
*/
public class MainActivity extends AppCompatActivity {
protected ListView mListView;
private List<MediaInfo> mMediaInfoList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
initView();
mMediaInfoList = getDatas();
}
private void initView() {
mListView = (ListView) findViewById(R.id.list_view);
}
// 获取系统媒体数据库中的音频多媒体信息
private List<MediaInfo> getDatas() {
List<MediaInfo> list = new ArrayList<>();
// 使用内容解析者访问系统提供的数据库
Cursor cursor = getContentResolver()
.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
null,
null,
null,
MediaStore.Audio.Media.DEFAULT_SORT_ORDER);// 默认排序顺序
// 如果游标读取时还有下一个数据,读取
int idIndex = cursor.getColumnIndex(MediaStore.Audio.Media._ID);//获取列名对应的索引
int titleIndex = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE);// 标题
int artistIndex = cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);// 艺术家
int uriIndex = cursor.getColumnIndex(MediaStore.Audio.Media.DATA);// 文件路径
while (cursor.moveToNext()) {
// 根据索引值获取对应列名中的数值
long _id = cursor.getLong(idIndex);
String title = cursor.getString(titleIndex);
String artist = cursor.getString(artistIndex);
String uri = cursor.getString(uriIndex);
MediaInfo mediaInfo = new MediaInfo(_id, uri, title, artist);
list.add(mediaInfo);
}
for (MediaInfo mediaInfo : list) {
Log.d("1507", "" + mediaInfo.toString());
}
return list;
}
}
类:
package net.bwie.mediaplayer.bean;
public class MediaInfo {
private long _id;
private String uri;// 路径
private String title;
private String artist;// 艺术家
public MediaInfo(long _id, String uri, String title, String artist) {
this._id = _id;
this.uri = uri;
this.title = title;
this.artist = artist;
}
public long get_id() {
return _id;
}
public void set_id(long _id) {
this._id = _id;
}
public String getUri() {
return uri;
}
public void setUri(String uri) {
this.uri = uri;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
@Override
public String toString() {
return "MediaInfo{" +
"_id=" + _id +
", uri=‘" + uri + ‘\‘‘ +
", title=‘" + title + ‘\‘‘ +
", artist=‘" + artist + ‘\‘‘ +
‘}‘;
}
}
以上是关于音乐查找的主要内容,如果未能解决你的问题,请参考以下文章