編輯:關於 Android 程式設計
上一篇介紹了以意圖(Intent)錄音的方式,本文則改用 MediaRecorder 類別來錄音,相較之下能提供更高的彈性與控制力,例如自訂音訊來源、輸出格式與編碼器。
完成後的效果如下:

畫面包含一個狀態文字與四個按鈕,分別負責開始錄音、停止錄音、播放錄音與結束:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/statusTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Status"
android:gravity="center_horizontal" />
<Button
android:id="@+id/button_startRecording"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="開始錄音" />
<Button
android:id="@+id/button_stopRecording"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="停止錄音" />
<Button
android:id="@+id/button_playRecording"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="播放錄音" />
<Button
android:id="@+id/button_finish"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="完成" />
</LinearLayout>
按下開始錄音時,建立 MediaRecorder 並依序設定音訊來源、輸出格式與編碼器,再指定輸出檔案後即可開始。停止時則釋放 MediaRecorder,並以 MediaPlayer 載入錄音檔以便播放。
public class MainActivity extends Activity implements OnClickListener, OnCompletionListener {
private TextView statusTextView;
private Button button_startRecording, button_stopRecording, button_playRecording, button_finish;
private MediaRecorder recorder;
private MediaPlayer player;
private File audioFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
statusTextView = (TextView) findViewById(R.id.statusTextView);
statusTextView.setText("Ready");
button_startRecording = (Button) findViewById(R.id.button_startRecording);
button_playRecording = (Button) findViewById(R.id.button_playRecording);
button_stopRecording = (Button) findViewById(R.id.button_stopRecording);
button_finish = (Button) findViewById(R.id.button_finish);
button_startRecording.setOnClickListener(this);
button_playRecording.setOnClickListener(this);
button_stopRecording.setOnClickListener(this);
button_finish.setOnClickListener(this);
button_playRecording.setEnabled(false);
button_stopRecording.setEnabled(false);
player = new MediaPlayer();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_startRecording:
try {
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
File path = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + "/files/");
path.mkdir();
audioFile = File.createTempFile("recording", ".3gp", path);
recorder.setOutputFile(audioFile.getAbsolutePath());
recorder.prepare();
recorder.start();
statusTextView.setText("Recording");
button_playRecording.setEnabled(false);
button_stopRecording.setEnabled(true);
button_startRecording.setEnabled(false);
} catch (Exception e) {
e.printStackTrace();
}
break;
case R.id.button_playRecording:
player.start();
statusTextView.setText("playing");
button_playRecording.setEnabled(false);
button_stopRecording.setEnabled(false);
button_startRecording.setEnabled(false);
break;
case R.id.button_stopRecording:
try {
recorder.stop();
recorder.release();
player.setOnCompletionListener(this);
player.setDataSource(audioFile.getAbsolutePath());
player.prepare();
statusTextView.setText("Ready to Play!!");
button_playRecording.setEnabled(true);
button_stopRecording.setEnabled(false);
} catch (Exception e) {
e.printStackTrace();
}
break;
case R.id.button_finish:
finish();
break;
default:
break;
}
}
@Override
public void onCompletion(MediaPlayer mp) {
button_playRecording.setEnabled(true);
button_startRecording.setEnabled(true);
button_stopRecording.setEnabled(false);
statusTextView.setText("Ready...");
}
}
透過 MediaRecorder 搭配 MediaPlayer,就能輕鬆完成錄音與播放的完整流程。別忘了在 AndroidManifest.xml 中加上錄音(RECORD_AUDIO)與外部儲存寫入權限。
用 AOP 打造 Android 遠程調用 SDK:動態代理實戰解析
「什麼是 Spring AOP?」——這道面試題一出,我腦海裡瞬間湧現出事務管理、SDK、程式碼監控、Spring Remoting 一大堆概念,反而一時語塞。後來想想,
Picasso 圖片加載庫詳解:二次封裝 PicassoUtils 打造微信精選
官網:https://square.github.io/picasso/ Picasso(學名畢加索)是 Square 公司開源的一款 Android 圖形緩存庫,使
用 Vitamio 打造萬能播放器(六):在線播放之播放列表
前言 新版本的 VPlayer 已由設計階段轉入開發階段,預計開發周期為一個月,這也意味著新版 Vitamio 將隨之發布。開發者們可以像本系列文章一樣,先著手開發其他功
Android-async-http 網絡請求框架源碼全解析
android-async-http 是一個基於 Apache HttpClient 的異步網絡請求庫,用起来非常輕鬆。所有網絡操作都在非 UI 線程完成,結果通過回調方