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

加载媒体库里的音频

时间:2017-11-12 20:49:05      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:string   files   prot   catch   res   get   extern   exception   list   

 /*加载媒体库里的音频*/
    public ArrayList<MusicMedia> scanAllAudioFiles(){
        //生成动态数组,并且转载数据
        ArrayList<MusicMedia> mylist = new ArrayList<MusicMedia>();

        /*查询媒体数据库
        参数分别为(路径,要查询的列名,条件语句,条件参数,排序)
        视频:MediaStore.Video.Media.EXTERNAL_CONTENT_URI
        图片;MediaStore.Images.Media.EXTERNAL_CONTENT_URI

         */
        Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
        //遍历媒体数据库
        if(cursor.moveToFirst()){
            while (!cursor.isAfterLast()) {
                //歌曲编号
                int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
                //歌曲标题
                String tilte = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
                //歌曲的专辑名:MediaStore.Audio.Media.ALBUM
                String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
                int albumId = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
                //歌曲的歌手名: MediaStore.Audio.Media.ARTIST
                String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
                //歌曲文件的路径 :MediaStore.Audio.Media.DATA
                String url = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
                //歌曲的总播放时长 :MediaStore.Audio.Media.DURATION
                int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
                //歌曲文件的大小 :MediaStore.Audio.Media.SIZE
                Long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));


                if (size >1024*800){//大于800K
                    MusicMedia musicMedia = new MusicMedia();
                    musicMedia.setId(id);
                    musicMedia.setArtist(artist);
                    musicMedia.setSize(size);
                    musicMedia.setTitle(tilte);
                    musicMedia.setTime(duration);
                    musicMedia.setUrl(url);
                    musicMedia.setAlbum(album);
                    musicMedia.setAlbumId(albumId);

                    mylist.add(musicMedia);

                }
                cursor.moveToNext();
            }
        }
        return mylist;
    }

 

 

  主函数

//播放对象
private MediaPlayer mediaPlayer;
//当前播放的索引
private int currentListItem=0;
private Adapter adapter;
private ListView listView;


private Button pause;
private Button next;
private Button last;
private ArrayList<MusicMedia> musicMedias;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mediaPlayer=new MediaPlayer();
    initView();
    musiclist();

}

private void musiclist() {

  adapter=new Adapter(musicMedias,this);
  listView.setAdapter(adapter);
  listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    currentListItem = i;
    mediaPlayer = MediaPlayer.create(MainActivity.this, Uri.parse(musicMedias.get(i).getUrl()));
    mediaPlayer.start();
}
});
}


private void initView() {
    pause = (Button) findViewById(R.id.pause);
    next = (Button) findViewById(R.id.next);
    last = (Button) findViewById(R.id.last);
    listView= (ListView) findViewById(R.id.lv);
    musicMedias = scanAllAudioFiles();
    pause.setOnClickListener(this);
    next.setOnClickListener(this);
    last.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.pause:
                if (mediaPlayer.isPlaying()){
                    mediaPlayer.pause();
                }else{
                    mediaPlayer.start();
                }
            break;
        case R.id.next:
            try {
                nextMusic();
            } catch (IOException e) {
                e.printStackTrace();
            }
            break;
        case R.id.last:
            try {
                lastMusic();
            } catch (IOException e) {
                e.printStackTrace();
            }
            break;
    }
}



//下一首
private void nextMusic() throws IOException {
    if (++currentListItem>=musicMedias.size()){
        currentListItem=0;
    }
    mediaPlayer.reset();
    mediaPlayer.setDataSource(musicMedias.get(currentListItem).getUrl());
    mediaPlayer.prepare();
    mediaPlayer.start();

}
//上一首
private void lastMusic() throws IOException {

        if (--currentListItem<=0){
            currentListItem=musicMedias.size()-1;
        }
    mediaPlayer.reset();
    mediaPlayer .setDataSource(musicMedias.get(currentListItem).getUrl());
    mediaPlayer.prepare();
    mediaPlayer.start();

}

 

加载媒体库里的音频

标签:string   files   prot   catch   res   get   extern   exception   list   

原文地址:http://www.cnblogs.com/SongYongQian/p/7822674.html

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