以编程方式访问库中的音乐文件 (Android)
Access Music files from library programmatically (Android)
我想访问 Android phone(不是 sdcard)中的音乐库并将其显示在列表中。我想获取与每个文件相关的所有信息,如持续时间、艺术家等。我将如何以编程方式执行此操作?
这就是我为我的一个案例定制的方式:
public class MusicActivity extends FragmentActivity {
ListView musiclist;
Cursor musiccursor;
int music_column_index;
int count;
MediaPlayer mMediaPlayer;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init_phone_music_grid();
}
private void init_phone_music_grid() {
System.gc();
String[] proj = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Video.Media.SIZE };
musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
proj, null, null, null);
count = musiccursor.getCount();
musiclist = (ListView)findViewById(R.id.sample);
musiclist.setAdapter(new MusicAdapter(getApplicationContext()));
musiclist.setOnItemClickListener(musicgridlistener);
mMediaPlayer = new MediaPlayer();
}
private OnItemClickListener musicgridlistener = new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,
long id) {
System.gc();
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
musiccursor.moveToPosition(position);
String filepath = musiccursor.getString(music_column_index);
finish();
try {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.reset();
}
mMediaPlayer.setDataSource(filepath);
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (Exception e) {
}
}
};
public class MusicAdapter extends BaseAdapter {
private Context mContext;
public MusicAdapter(Context c) {
mContext = c;
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
System.gc();
TextView tv = new TextView(mContext.getApplicationContext());
String id = null;
if (convertView == null) {
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
musiccursor.moveToPosition(position);
id = musiccursor.getString(music_column_index);
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
musiccursor.moveToPosition(position);
//id += " Size(KB):" + musiccursor.getString(music_column_index);
tv.setText(id);
} else
tv = (TextView) convertView;
return tv;
}
}
}
private void musics(){
Uri uri = MediaStore.Audio.Media.INTERNAL_CONTENT_URI;
ContentResolver cr = getContentResolver();
String[] columns = {MediaStore.Audio.Media.DISPLAY_NAME};//add which column you need here
Cursor cursor = cr.query(uri, columns, null, null, null);
if(cursor == null){
return;
}
if(cursor.getCount() > 0){
cursor.moveToFirst();
do{
//read your information here
String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
}while(cursor.moveToNext());
}
cursor.close();
}
我想访问 Android phone(不是 sdcard)中的音乐库并将其显示在列表中。我想获取与每个文件相关的所有信息,如持续时间、艺术家等。我将如何以编程方式执行此操作?
这就是我为我的一个案例定制的方式:
public class MusicActivity extends FragmentActivity {
ListView musiclist;
Cursor musiccursor;
int music_column_index;
int count;
MediaPlayer mMediaPlayer;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init_phone_music_grid();
}
private void init_phone_music_grid() {
System.gc();
String[] proj = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Video.Media.SIZE };
musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
proj, null, null, null);
count = musiccursor.getCount();
musiclist = (ListView)findViewById(R.id.sample);
musiclist.setAdapter(new MusicAdapter(getApplicationContext()));
musiclist.setOnItemClickListener(musicgridlistener);
mMediaPlayer = new MediaPlayer();
}
private OnItemClickListener musicgridlistener = new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,
long id) {
System.gc();
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
musiccursor.moveToPosition(position);
String filepath = musiccursor.getString(music_column_index);
finish();
try {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.reset();
}
mMediaPlayer.setDataSource(filepath);
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (Exception e) {
}
}
};
public class MusicAdapter extends BaseAdapter {
private Context mContext;
public MusicAdapter(Context c) {
mContext = c;
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
System.gc();
TextView tv = new TextView(mContext.getApplicationContext());
String id = null;
if (convertView == null) {
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
musiccursor.moveToPosition(position);
id = musiccursor.getString(music_column_index);
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
musiccursor.moveToPosition(position);
//id += " Size(KB):" + musiccursor.getString(music_column_index);
tv.setText(id);
} else
tv = (TextView) convertView;
return tv;
}
}
}
private void musics(){
Uri uri = MediaStore.Audio.Media.INTERNAL_CONTENT_URI;
ContentResolver cr = getContentResolver();
String[] columns = {MediaStore.Audio.Media.DISPLAY_NAME};//add which column you need here
Cursor cursor = cr.query(uri, columns, null, null, null);
if(cursor == null){
return;
}
if(cursor.getCount() > 0){
cursor.moveToFirst();
do{
//read your information here
String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
}while(cursor.moveToNext());
}
cursor.close();
}