今天在做一個簡單的錄音機的時候,錄音機老是無法正常執行個體化,不能使用,後來尋找問題,一個一個的試,最後確定了,原來是兩個設定寫反了,卻造成了,無法正常工作,真是鬱悶啊。找了一個教程看了一下,教程中也沒有提,這兩個不能寫反,就當給自己一個警告吧,也希望看到的人,不知道這個問題的人能記住。下面把代碼放到下面:
package com.RecordTest;import java.io.File;import java.io.FilenameFilter;import java.io.IOException;import java.util.ArrayList;import java.util.List;import android.app.ListActivity;import android.media.MediaRecorder;import android.os.Bundle;import android.os.Environment;import android.util.Log;import android.view.View;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.Toast;public class RecordTest extends ListActivity {Button btn1, btn2;int currertindex = 0;private MediaRecorder mediarecorder; // 建立錄音機File recordFile = null;File sdcardPath = null;// 錄音檔案目錄String Tempfile = "Record_temp_";List<String> fileList = new ArrayList<String>();/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);if (Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {sdcardPath = Environment.getExternalStorageDirectory();setTitle(sdcardPath.getName());} else {Toast.makeText(RecordTest.this, "沒有SD卡", 1000).show();}getRecordList();btn1 = (Button) findViewById(R.id.btn1);btn2 = (Button) findViewById(R.id.btn2);btn1.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubtry {recordFile = File.createTempFile(Tempfile, ".amr",sdcardPath);Log.i("aaaaa", "1");mediarecorder = new MediaRecorder();Log.i("aaaaa", "2");mediarecorder.setAudioSource(MediaRecorder.AudioSource.MIC); // 設定麥克風Log.i("aaaaa", "3");mediarecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);//設定輸出檔案格式Log.i("aaaaa", "4");mediarecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); // 設定編碼格式Log.i("aaaaa", "5");mediarecorder.setOutputFile(recordFile.getAbsolutePath()); // 使用絕對路徑進行儲存檔案Log.i("aaaaa", "6"); mediarecorder.prepare();mediarecorder.start();Toast.makeText(RecordTest.this, "開始", Toast.LENGTH_LONG).show();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}});btn2.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubtry {if (recordFile != null) {mediarecorder.stop();fileList.add(recordFile.getName()); ArrayAdapter<String> list = new ArrayAdapter<String>(RecordTest.this, R.layout.itemlist, R.id.listitem, fileList); setListAdapter(list); mediarecorder = null; Toast.makeText(RecordTest.this, "停止", Toast.LENGTH_LONG).show();}} catch (Exception e) {Log.i("eeeee", e.getMessage());}}});}void getRecordList() {// 首先檢測是否存在SDCardtry {File home = sdcardPath;fileList.clear();if (home.list(new Fileter()).length > 0) {for (File file : home.listFiles(new Fileter())) {fileList.add(file.getName());}ArrayAdapter<String> list = new ArrayAdapter<String>(this,R.layout.itemlist, R.id.listitem, fileList);setListAdapter(list);}} catch (Exception e) {Toast.makeText(RecordTest.this, e.getMessage(), 1000).show();}}class Fileter implements FilenameFilter {public boolean accept(File dir, String filename) {// TODO Auto-generated method stubreturn filename.equals(".amr");}}}
在使用過程中,需要加入操作SD卡和使用錄音裝置的許可權,即在AndroidManifest.xml中加入:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
然後就可以使用了,如果有問題,歡迎大家指正!