// 在錄音過程中顯示麥克風音量
private class MicLevelTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... args) {
while (isRecording) {
publishProgress();
try {
Thread.sleep(200);
}
catch (InterruptedException e) {
Log.d("RecordActivity", e.toString());
}
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
record_volumn.setProgress((int) myRecoder.getAmplitudeEMA());
}
}
裡面主要用到MediaPlayer這個元件,所以先宣告他。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play2);
Intent intent = getIntent();
String fileName = intent.getStringExtra("fileName");
// 建立指定資源的MediaPlayer物件
Uri uri = Uri.parse(fileName);
mediaPlayer = MediaPlayer.create(this, uri);
}
public void onSubmit(View view) {
// 結束Activity元件
finish();
}
public void clickPlay(View view) {
mediaPlayer.start();
}
public void clickPause(View view) {
mediaPlayer.pause();
}
public void clickStop(View view) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
// 回到開始的位置
mediaPlayer.seekTo(0);
}
@Override
protected void onStop() {
if (mediaPlayer.isPlaying()) {
// 停止播放
mediaPlayer.stop();
}
// 清除MediaPlayer物件
mediaPlayer.release();
super.onStop();
}