android:MediaPlayer
(一)
private MediaPlayer mediaplayer =null;
mediaplayer = MediaPlayer.create(Player.this,Uri.parse("file://"+path)); //Player是现在这个activity,path是路径
mediaplayer.start(); //开始播放
mediaplayer.pause(); //暂定
mediaplayer.stop(); //停止
mediaplayer.release(); //释放资源
path的获取:
String SDCardRoot = Environment.getExternalStorageDirectory().getAbsolutePath();
String path = SDCardRoot + File.separator + "music" + File.separator+ name_click;
(二)
private MediaPlayer mediaplayer =null;
mediaplayer = new mediaplayer();
//播放歌曲
private void playmusic(int positions){
String path = Environment.getExternalStorageDirectory() + "/"
+ "music/" + list_names.get(positions);
try {
mediaplayer.reset(); //reset重新设置播放器引擎
mediaplayer.setDataSource(path);
System.out.println(path + "2");
mediaplayer.prepare(); //new MediaPlayer()必须得有
mediaplayer.start();
mediaplayer.setOnCompletionListener(new OnCompletionListener() { //歌曲播放完监听器
public void onCompletion(MediaPlayer mp) {
nextmusic();
}
});
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//播放下一首歌曲
private void nextmusic() {
if(position<list_names.size()){
playmusic(++position);
}
else {
position=0;
playmusic(position);
}
}
在mp.pausr();再mp.start();后面遇到mp.preper()会报错,这是由于播放引擎的工作状态引起的,必须先进行mp.stop();然后再操作,在进行播放完之后也不行设置mp.reset();