ListContent类用来获取和设置歌曲的信息
public class ListContent {
private int imageId;
private String song;
private int duration;
private String songPath;
public ListContent() {
}
public ListContent(int imageId,String song,int duration,String songPath) {
this.imageId = imageId;
this.song = song;
this.duration = duration;
this.songPath = songPath;
}
//获得歌曲的图片,歌名,时长,路径
public int getImageId(){
return imageId;
}
public String getSong(){
return song;
}
public int getDuration(){
return duration;
}
//设置歌曲的图片,歌名,时长,路径
public String getSongPath() {
return songPath;
}
public int setImageId(int ImageId) {
return this.imageId = ImageId;
}
public String setSong(String song) {
return this.song = song;
}
public int setDuration(int duration) {
return this.duration = duration;
}
public String setSongPath(String songPath) {
return this.songPath;
}
}
GetMedia类用来获取手机中歌曲的标题,时长,路径
public class GetMedia {
public static List<ListContent> getSongInfo(Context context){
List<ListContent> songInfos = new ArrayList<ListContent>();
Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null,null,null,MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
//cursor.moveToFirst();
while(cursor.moveToNext()) {
ListContent songInfo = new ListContent();
//获取歌曲的标题
String songName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
//MediaStore.Audio.Media.DURATION 音频文件的持续时间,以毫秒为单位
int songDuration = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
String songPath = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));//获得歌曲的路径
int isMusic = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));
if(isMusic != 0) {
songInfo.setImageId(R.drawable.first_song);
songInfo.setSong(songName);
songInfo.setDuration(songDuration);
songInfo.setSongPath(songPath);
songInfos.add(songInfo);
}
}
cursor.close();
return songInfos;
}
}
主活动HomeActivity
public class HomeActivity extends Activity implements OnItemClickListener{
private List listContent;
private MediaPlayer mediaPlayer = new MediaPlayer();
private Button previous;
private Button repeat;
private Button play;
private Button shuffle;
private Button next;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_layout);
//静态方法应该使用ClassName.staticMethod()的形式使用
listContent = GetMedia.getSongInfo(HomeActivity.this);
SongListAdapter songListAdapter = new SongListAdapter(HomeActivity.this,R.layout.song_item,listContent);
ListView listView = (ListView) findViewById(R.id.home_list);
listView.setAdapter(songListAdapter);
listView.setOnItemClickListener(this);
previous = (Button) findViewById(R.id.previous);
repeat = (Button) findViewById(R.id.repeat);
play = (Button) findViewById(R.id.play);
shuffle = (Button) findViewById(R.id.previoshuffleus);
next = (Button) findViewById(R.id.next);
}
//监听listView
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ListContent songListContent = listContent.get(position);//获得被点击的对象
try {
mediaPlayer.setDataSource(songListContent.getSongPath());//指定音频的路径
//想打印出音频的路径,但日志没有显示
String mySong = songListContent.getSongPath();
Log.d("HomeActivity",mySong);
mediaPlayer.prepareAsync();
}catch (Exception e) {
e.printStackTrace();
}
mediaPlayer.start();//播放音乐
}
}