Skip to content

Instantly share code, notes, and snippets.

@Haldir65
Created September 9, 2016 10:55
Show Gist options
  • Save Haldir65/42a9bdbb83a246641450dd2f9bb16ca5 to your computer and use it in GitHub Desktop.
Save Haldir65/42a9bdbb83a246641450dd2f9bb16ca5 to your computer and use it in GitHub Desktop.
/**
* Created by Harris on 2016/7/31.
*/
/**
*
* @ClassName: com.example.mediastore.Song
* @Description: 歌曲实体类
* @author zhaokaiqiang
* @date 2014-12-4 上午11:49:59
*
*/
public class Song {
private String fileName;
private String title;
private int duration;
private String singer;
private String album;
private String year;
private String type;
private String size;
private String fileUrl;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
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 String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String getFileUrl() {
return fileUrl;
}
public void setFileUrl(String fileUrl) {
this.fileUrl = fileUrl;
}
public Song() {
super();
}
public Song(String fileName, String title, int duration, String singer,
String album, String year, String type, String size, String fileUrl) {
super();
this.fileName = fileName;
this.title = title;
this.duration = duration;
this.singer = singer;
this.album = album;
this.year = year;
this.type = type;
this.size = size;
this.fileUrl = fileUrl;
}
@Override
public String toString() {
return "Song [fileName=" + fileName + ", title=" + title
+ ", duration=" + duration + ", singer=" + singer + ", album="
+ album + ", year=" + year + ", type=" + type + ", size="
+ size + ", fileUrl=" + fileUrl + "]";
}
}
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import com.me.harris.ganstuff.model.Song;
import java.util.ArrayList;
/**
*
* @ClassName: com.example.mediastore.AudioUtils
* @Description: 只是一个用来获取手机上所有音乐文件的帮助类
* @author zhaokaiqiang
* @date 2014-12-4 上午11:39:45
*
*/
public class AudioUtils {
/**
* 获取sd卡所有的音乐文件
*
* @return
* @throws Exception
*/
public static ArrayList<Song> getAllSongs(Context context) {
ArrayList<Song> songs = null;
Cursor cursor = context.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.YEAR,
MediaStore.Audio.Media.MIME_TYPE,
MediaStore.Audio.Media.SIZE,
MediaStore.Audio.Media.DATA },
MediaStore.Audio.Media.MIME_TYPE + "=? or "
+ MediaStore.Audio.Media.MIME_TYPE + "=?",
new String[] { "audio/mpeg", "audio/x-ms-wma" }, null);
songs = new ArrayList<Song>();
if (cursor.moveToFirst()) {
Song song = null;
do {
song = new Song();
// 文件名
song.setFileName(cursor.getString(1));
// 歌曲名
song.setTitle(cursor.getString(2));
// 时长
song.setDuration(cursor.getInt(3));
// 歌手名
song.setSinger(cursor.getString(4));
// 专辑名
song.setAlbum(cursor.getString(5));
// 年代
if (cursor.getString(6) != null) {
song.setYear(cursor.getString(6));
} else {
song.setYear("未知");
}
// 歌曲格式
if ("audio/mpeg".equals(cursor.getString(7).trim())) {
song.setType("mp3");
} else if ("audio/x-ms-wma".equals(cursor.getString(7).trim())) {
song.setType("wma");
}
// 文件大小
if (cursor.getString(8) != null) {
float size = cursor.getInt(8) / 1024f / 1024f;
song.setSize((size + "").substring(0, 4) + "M");
} else {
song.setSize("未知");
}
// 文件路径
if (cursor.getString(9) != null) {
song.setFileUrl(cursor.getString(9));
}
songs.add(song);
} while (cursor.moveToNext());
cursor.close();
}
return songs;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment