1. 要進行錄音需先取得錄音的權限,加上需將錄音檔寫到記憶卡,因此需在AndroidManifest.xml允許存取android.permission.RECORD_AUDIO及android.permission.WRITE_EXTERNAL_STORAGE這二個權限。
2.MediaRecorderExample.java
01.package tw.nicky;02.import java.io.File;03.import java.io.IOException;04.import android.app.Activity;05.import android.media.MediaRecorder;06.import android.os.Bundle;07.import android.os.Environment;08.import android.view.View;09.import android.widget.Button;10. 11.public class MediaRecorderExample extends Activity {12. private Button recordButn;13. private Button stopButn;14. private MediaRecorder mediaRecorder = null;15. 16. @Override17. public void onCreate(Bundle savedInstanceState) {18. super.onCreate(savedInstanceState);19. setContentView(R.layout.main);20. recordButn = (Button) findViewById(R.id.recordButn);21. stopButn = (Button) findViewById(R.id.stopButn);22. 23. //錄音24. recordButn.setOnClickListener(new View.OnClickListener() {25. public void onClick(View v) {26. //設定錄音檔名27. String fileName = "record.amr";28. try {29. File SDCardpath = Environment.getExternalStorageDirectory();30. File myDataPath = new File( SDCardpath.getAbsolutePath() + "/download" );31. if( !myDataPath.exists() ) myDataPath.mkdirs();32. File recodeFile = new File(SDCardpath.getAbsolutePath() + "/download/"+fileName);33. 34. mediaRecorder = new MediaRecorder();35. 36. //設定音源37. mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);38. //設定輸出檔案的格式39. mediaRecorder40. .setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);41. //設定編碼格式42. mediaRecorder43. .setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);44. //設定錄音檔位置45. mediaRecorder46. .setOutputFile(recodeFile.getAbsolutePath());47. 48. mediaRecorder.prepare();49. 50. //開始錄音51. mediaRecorder.start();52. } catch (IOException e) {53. e.printStackTrace();54. }55. }56. });57. 58. //停止錄音59. stopButn.setOnClickListener(new View.OnClickListener() {60. public void onClick(View v) {61. if(mediaRecorder != null) {62. mediaRecorder.stop();63. mediaRecorder.release();64. mediaRecorder = null;65. }66. }67. });68. 69. }70.}
3. main.xml(Layout)
01.<?xml version="1.0" encoding="utf-8"?>02.<AbsoluteLayout03.android:id="@+id/widget0"04.android:layout_width="fill_parent"05.android:layout_height="fill_parent"06.xmlns:android="http://schemas.android.com/apk/res/android"07.>08.<Button09.android:id="@+id/recordButn"10.android:layout_width="wrap_content"11.android:layout_height="wrap_content"12.android:text="錄音"13.android:layout_x="68px"14.android:layout_y="44px"15.>16.</Button>17.<Button18.android:id="@+id/stopButn"19.android:layout_width="wrap_content"20.android:layout_height="wrap_content"21.android:text="停止"22.android:layout_x="170px"23.android:layout_y="43px"24.>25.</Button>26.</AbsoluteLayout>
4. AndroidManifest.xml
01.<?xml version="1.0" encoding="utf-8"?>02.<manifest xmlns:android="http://schemas.android.com/apk/res/android"03. package="tw.nicky"04. android:versionCode="1"05. android:versionName="1.0">06. <application android:icon="@drawable/icon" android:label="@string/app_name">07. <activity android:name=".MediaRecorderExample"08. android:label="@string/app_name">09. <intent-filter>10. <action android:name="android.intent.action.MAIN" />11. <category android:name="android.intent.category.LAUNCHER" />12. </intent-filter>13. </activity>14. </application>15. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>16. <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>17.</manifest>
文章標籤
全站熱搜
