2.MediaRecorderExample.java
package tw.nicky; import java.io.File; import java.io.IOException; import android.app.Activity; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.widget.Button; public class MediaRecorderExample extends Activity { private Button recordButn; private Button stopButn; private MediaRecorder mediaRecorder = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); recordButn = (Button) findViewById(R.id.recordButn); stopButn = (Button) findViewById(R.id.stopButn); //錄音 recordButn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //設定錄音檔名 String fileName = "record.amr"; try { File SDCardpath = Environment.getExternalStorageDirectory(); File myDataPath = new File( SDCardpath.getAbsolutePath() + "/download" ); if( !myDataPath.exists() ) myDataPath.mkdirs(); File recodeFile = new File(SDCardpath.getAbsolutePath() + "/download/"+fileName); mediaRecorder = new MediaRecorder(); //設定音源 mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); //設定輸出檔案的格式 mediaRecorder .setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR); //設定編碼格式 mediaRecorder .setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); //設定錄音檔位置 mediaRecorder .setOutputFile(recodeFile.getAbsolutePath()); mediaRecorder.prepare(); //開始錄音 mediaRecorder.start(); } catch (IOException e) { e.printStackTrace(); } } }); //停止錄音 stopButn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if(mediaRecorder != null) { mediaRecorder.stop(); mediaRecorder.release(); mediaRecorder = null; } } }); } }3. main.xml(Layout)
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout android:id="@+id/widget0" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" > <Button android:id="@+id/recordButn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="錄音" android:layout_x="68px" android:layout_y="44px" > </Button> <Button android:id="@+id/stopButn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止" android:layout_x="170px" android:layout_y="43px" > </Button> </AbsoluteLayout>
4. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="tw.nicky" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MediaRecorderExample" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission> </manifest>
5. 執行畫面
請問一下~錄音完成後~如何在同一個頁面播放剛剛錄製的語音檔?? 路徑要怎麼設?
回覆刪除請問在執行
回覆刪除//設定音源
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
的時候,出現錯誤訊息視窗,這樣要如何排除??