Activity中的代码, 这里用Intent 传递过去文件名的名称
Intent playit = new Intent(getApplication(),PlayAuido.class);
if(!playstate){
//开始试听
item_play.setVisibility(View.VISIBLE);
Bundle b = new Bundle();
b.putString("path", recorder.getName());
playit.putExtras(b);
this.startService(playit);
playstate=true;
}else{ //停止试听
item_play.setVisibility(View.INVISIBLE);
this.stopService(playit);
playstate=false;
}
下面是service中的代码
public class PlayAuido extends Service {
private String recordname;
private MediaPlayer player;
@Override
public void onDestroy() { //service摧毁的时候调用
player.stop();
player.release();
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
Bundle bundle = intent.getExtras();
recordname=bundle.getString("path"); //获得要播放的录音文件的名字
String path = "file:///"+Environment.getExternalStorageDirectory()
+ "/dingdingrecorder/"+recordname;
Uri uri = Uri.parse(path);
player = MediaPlayer.create(getApplication(), uri);
player.start();
super.onStart(intent, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}