因此,在我正在开发的应用程序中,我试图合并一个列表视图,以显示用户手机上的所有歌曲。我目前将它设置为显示所有音乐文件(如下面的代码所示),但我需要一种方法来过滤掉随机媒体文件中的实际歌曲(即录音或其他应用程序中使用的音频文件,如 facebook 或谷歌+)。我已经搜索并尝试了多种方法,但没有一种方法被证明是成功的。正如您在我的代码中看到的那样,我正在尝试获取歌曲的持续时间并确保它至少大约一分钟长。任何和所有的帮助将不胜感激!
谢谢,欧文
PS 只要问我是否需要查看更多我的代码,但我认为这应该足够了。
private void init_phone_music_grid() {
System.gc();
String[] proj = { MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.TITLE,
MediaStore.Video.Media.ARTIST };
musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
proj, null, null, null);
count = musiccursor.getCount();
musiclist = (ListView) findViewById(R.id.lvTrackList);
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 filename = musiccursor.getString(music_column_index);
try {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.reset();
}
mMediaPlayer.setDataSource(filename);
mMediaPlayer.prepare();
mMediaPlayer.start();
if(mMediaPlayer.isPlaying())
chrono.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) {
long size = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION);
if(size > 60000) {
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
musiccursor.moveToPosition(position);
id = musiccursor.getString(music_column_index);
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION);
musiccursor.moveToPosition(position);
if(!(musiccursor.getString(music_column_index).equalsIgnoreCase("<unknown>")))
id += " - " + musiccursor.getString(music_column_index);
tv.setText(id);
}
} else
tv = (TextView) convertView;
return tv;
}
}