1
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class AudioRecordingActivity extends Activity {
private static final String AUDIO_RECORDER_FILE_EXT_3GP = ".3gp";
private static final String AUDIO_RECORDER_FILE_EXT_MP4 = ".mp4";
private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";
private MediaRecorder recorder = null;
private int currentFormat = 0;
private int output_formats[] = { MediaRecorder.OutputFormat.MPEG_4 };
private String file_exts[] = { AUDIO_RECORDER_FILE_EXT_MP4};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setButtonHandlers();
enableButtons(false);
//setFormatButtonCaption();
}
private void setButtonHandlers() {
((Button) findViewById(R.id.btnStart)).setOnClickListener(btnClick);
((Button) findViewById(R.id.btnStop)).setOnClickListener(btnClick);
//((Button) findViewById(R.id.btnFormat)).setOnClickListener(btnClick);
}
private void enableButton(int id, boolean isEnable) {
((Button) findViewById(id)).setEnabled(isEnable);
}
private void enableButtons(boolean isRecording) {
enableButton(R.id.btnStart, !isRecording);
//enableButton(R.id.btnFormat, !isRecording);
enableButton(R.id.btnStop, isRecording);
}
/*private void setFormatButtonCaption() {
((Button) findViewById(R.id.btnFormat))
.setText(getString(R.string.audio_format) + " ("
+ file_exts[currentFormat] + ")");
}*/
private String getFilename() {
String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath, AUDIO_RECORDER_FOLDER);
if (!file.exists()) {
file.mkdirs();
}
return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + file_exts[currentFormat]);
}
private void startRecording() {
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(output_formats[currentFormat]);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(getFilename());
recorder.setOnErrorListener(errorListener);
recorder.setOnInfoListener(infoListener);
try {
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void stopRecording() {
if (null != recorder) {
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
}
}
private void displayFormatDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
String formats[] = { "MPEG 4", "3GPP" };
builder.setTitle(getString(R.string.choose_format_title))
.setSingleChoiceItems(formats, currentFormat,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
currentFormat = which;
//setFormatButtonCaption();
dialog.dismiss();
}
}).show();
}
private MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {
@Override
public void onError(MediaRecorder mr, int what, int extra) {
Toast.makeText(AudioRecordingActivity.this,
"Error: " + what + ", " + extra, Toast.LENGTH_SHORT).show();
}
};
private MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() {
@Override
public void onInfo(MediaRecorder mr, int what, int extra) {
Toast.makeText(AudioRecordingActivity.this,
"Warning: " + what + ", " + extra, Toast.LENGTH_SHORT)
.show();
}
};
private View.OnClickListener btnClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnStart: {
Toast.makeText(AudioRecordingActivity.this, "Start Recording",
Toast.LENGTH_SHORT).show();
enableButtons(true);
startRecording();
break;
}
case R.id.btnStop: {
Toast.makeText(AudioRecordingActivity.this, "Stop Recording",
Toast.LENGTH_SHORT).show();
enableButtons(false);
stopRecording();
break;
}
/*case R.id.btnFormat: {
displayFormatDialog();
break;
}*/
}
}
};
}
上面的代碼記錄音頻並將其存儲在SD卡上點擊一個按鈕開始和停止錄製。自動錄製音頻
我想製作一個錄音機應用程序,它可以在啓動應用程序時自動開始錄製音頻大約30秒。是否可以這樣做?如果是的話那怎麼樣?
我發現這一點的同時在互聯網上搜索setMaxDuration(INT的毫秒)。你所建議的和這個之間有什麼區別? – 2013-03-19 08:59:56
我覺得沒有太大的區別。運行延遲任務可能會更好。首先想到的第一個例子是需要額外的檢查(除了記錄已經停止)。如果你沒有這樣的要求,你應該在這種情況下使用'setMaxDuration'而不是我的解決方案,因爲它使得代碼更易讀,更容易維護。 – 2013-03-19 09:16:10
上一條評論中延遲任務案例的附加說明:您可能需要檢查用戶在達到30秒標記時是否說完,如果不是,則可能需要繼續錄製,例如額外5秒鐘之後重新檢查。使用'setMaxDuration'時,您將無法輕鬆完成此操作。 – 2013-03-19 09:19:17