Android多媒体分析-通过MediaStore获取Audio信息

   	public void getAlldata() {
		ContentResolver cr = getApplication().getContentResolver();
		if (cr == null) {
			return;
		}
		// 获取所有歌曲
		Cursor cursor = cr.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
				null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
		if (null == cursor) {
			return;
		}
		Music music;
		List<Music> list = new ArrayList<MainActivity.Music>();
		if (cursor.moveToFirst()) {
			do {
				
				//歌曲名
				String title = cursor.getString(cursor
						.getColumnIndex(MediaStore.Audio.Media.TITLE));
				
				//歌手
				String singer = cursor.getString(cursor
						.getColumnIndex(MediaStore.Audio.Media.ARTIST));
				
				//专辑
				String album = cursor.getString(cursor
						.getColumnIndex(MediaStore.Audio.Media.ALBUM));
				
				//长度
				long size = cursor.getLong(cursor
						.getColumnIndex(MediaStore.Audio.Media.SIZE));
				
				//时长
				int duration = cursor.getInt(cursor
						.getColumnIndex(MediaStore.Audio.Media.DURATION));
				
				//路径
				String url = cursor.getString(cursor
						.getColumnIndex(MediaStore.Audio.Media.DATA));
				
				//显示的文件名
				String _display_name = cursor.getString(cursor
						.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
				
				//类型
				String mime_type = cursor.getString(cursor
						.getColumnIndex(MediaStore.Audio.Media.MIME_TYPE));
				music = new Music();
				music.setAlbum(album);
				music.setDuration(duration);
				music.setSinger(singer);
				music.setSize(size);
				music.setTitle(title);
				music.setUrl(url);
				music.set_display_name(_display_name);
				music.setMime_type(mime_type);
				list.add(music);
			} while (cursor.moveToNext());
		}
		System.out.println(list.size());
	}
	class Music
	{
		private  String title;
		private  String singer;
		private  String album;
		private  long size;
		private  int duration;
		private  String url;
		private  String _display_name;
		private String mime_type;
		
		public String get_display_name() {
			return _display_name;
		}
		public void set_display_name(String _display_name) {
			this._display_name = _display_name;
		}
		public String getMime_type() {
			return mime_type;
		}
		public void setMime_type(String mime_type) {
			this.mime_type = mime_type;
		}
		public String getTitle() {
			return title;
		}
		public void setTitle(String title) {
			this.title = title;
		}
		public String getSinger() {
			return singer;
		}
		public void setSinger(String singer) {
			this.singer = singer;
		}
		public String getAlbum() {
			return album;
		}
		public void setAlbum(String album) {
			this.album = album;
		}
		public long getSize() {
			return size;
		}
		public void setSize(long size) {
			this.size = size;
		}
		public int getDuration() {
			return duration;
		}
		public void setDuration(int duration) {
			this.duration = duration;
		}
		public String getUrl() {
			return url;
		}
		public void setUrl(String url) {
			this.url = url;
		}
		@Override
		public String toString() {
			return "Music [title=" + title + ", singer=" + singer + ", album="
					+ album + ", size=" + size + ", duration=" + duration
					+ ", url=" + url + ", _display_name=" + _display_name
					+ ", mime_type=" + mime_type + "]";
		}
		
	}


 

 

 

 

 

发布了14 篇原创文章 · 获赞 5 · 访问量 5万+
展开阅读全文

为什么用AndroidMediaStore查不到SD卡的音乐,权限里已经设置了

11-25

``` import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.content.ContentResolver; import android.database.Cursor; import android.os.Bundle; import android.provider.MediaStore; import android.widget.ListView; import android.widget.SimpleAdapter; public class MainActivity extends Activity { private ListView list; private List<Map<String, String>> musicList = new ArrayList<Map<String, String>>(); private SimpleAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_main); //musicList=loadSongs(); list=(ListView) super.findViewById(R.id.listView1); adapter = new SimpleAdapter(getApplicationContext(), musicList, R.layout.item, new String[] { "name", "artist", "time", "data", "id" }, new int[] { R.id.name, R.id.songer, R.id.time, R.id.path, R.id.musicRating }); list.setAdapter(adapter); } public List<Map<String, String>> getmusic(){ ContentResolver musicResolver = this.getContentResolver(); Cursor cursor = /*getContentResolver()*/ musicResolver .query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[] { MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.DISPLAY_NAME }, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER); if (cursor != null && cursor.moveToFirst()) { do { /*String mName = cursor.getString(0); long mTime = cursor.getLong(1); String mAlbum = cursor.getString(2); String mArtist = cursor.getString(3); long mId = cursor.getLong(4); String mData = cursor.getString(5); */ String mName =cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)); int mTime=cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)); String mAlbum=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM)); String mArtist=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)); int mId=cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID)); String mData=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)); String mDisplay = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME)); HashMap a = new HashMap(); a.put("name", mName); a.put("time", mTime); //a.put("time1", mTime); a.put("album", mAlbum); a.put("artist", mArtist); a.put("id", mId); a.put("data", mData); a.put("display", mDisplay); musicList.add(a);//ArrayList System.out.println(mName + "-->" + mTime + "---->>--" + mData); } while (cursor.moveToNext()); } if (!cursor.isClosed()) { cursor.close(); } return musicList; } } ``` 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览