码迷,mamicode.com
首页 > 其他好文 > 详细

音乐查找

时间:2017-11-15 21:54:18      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:iap   bundle   xtend   音频   set   lis   view   media   create   

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 + ‘\‘‘ +
                ‘}‘;
    }
}

音乐查找

标签:iap   bundle   xtend   音频   set   lis   view   media   create   

原文地址:http://www.cnblogs.com/98k98k/p/7840500.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!