Android 带ListView的音乐播放器
2010-09-06 08:19:00 来源:WEB开发网//2. 初始化
mp = new MediaPlayer();
//3. 暂停
mp.pause();
//4. 继续
mp.start();
//5. 判断是否正在播放
mp.isPlaying()
//6.使用目标Uri
mp.release();
mp = MediaPlayer.create(this, uri);
//1. 定义
MediaPlayer mp;
//2. 初始化
mp = new MediaPlayer();
//3. 暂停
mp.pause();
//4. 继续
mp.start();
//5. 判断是否正在播放
mp.isPlaying()
//6.使用目标Uri
mp.release();
mp = MediaPlayer.create(this, uri);
5. 单击ListView中某个Item 会播放目标音乐资源
Java代码
public void playMusic(long arg3) throws IllegalArgumentException, IllegalStateException, IOException{
Uri uri = Uri.withAppendedPath(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
String.valueOf(arg3));
TextView tv = (TextView) findViewById(R.id.name);
tv.setText(uri.toString());
mp.release();
mp = MediaPlayer.create(this, uri);
mp.start();
}
list.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
try {
playMusic(arg3);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
public void playMusic(long arg3) throws IllegalArgumentException,
更多精彩
赞助商链接