80,362
社区成员
发帖
与我相关
我的任务
分享
public class PlayRecordActivity extends Activity {
private ImageButton startBtn = null;
private ImageButton pauseBtn = null;
private ImageButton stopBtn = null;
private String filePath = null;
private MediaPlayer mediaPlayer = null;
private boolean isPlaying= false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.record_play);
findViews();
setListeners();
initData();
}
public void findViews(){
startBtn = (ImageButton) this.findViewById(R.id.record_play);
pauseBtn = (ImageButton) this.findViewById(R.id.record_pause);
stopBtn = (ImageButton) this.findViewById(R.id.record_stop);
}
public void setListeners(){
startBtn.setOnClickListener(new ButtonOnClickListener());
pauseBtn.setOnClickListener(new ButtonOnClickListener());
stopBtn.setOnClickListener(new ButtonOnClickListener());
}
public void initData(){
Intent intent = this.getIntent();
String fileName = intent.getExtras().getString("file_name");
filePath=AppConstant.saveDir+File.separator+fileName;
}
public void playRecord(){
System.out.println(filePath);
Intent intent = this.getIntent();
String fileName = intent.getExtras().getString("file_name");
filePath=AppConstant.saveDir+File.separator+fileName;
File f = new File(filePath);
System.out.println(f.length());
mediaPlayer = MediaPlayer.create(this, Uri.parse("file://" + filePath));
mediaPlayer.setLooping(false);
mediaPlayer.start();
isPlaying = true;
}
public void pauseRecord(){
if(isPlaying == true){
mediaPlayer.pause();
}else{
mediaPlayer.start();
}
isPlaying = isPlaying ? false : true;
}
public void stopRecord(){
if(isPlaying == true){
mediaPlayer.stop();
mediaPlayer.release();
}
}
public final class ButtonOnClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.record_play :
playRecord();
case R.id.record_pause:
pauseRecord();
case R.id.record_stop:
stopRecord();
}
}
}
}