pacote com.record.android;
importar java.io.File;
importar java.io.IOException;
Importar android.app.Activity;
android.app.AlertDialog de importação;
android.content.DialogInterface de importação;
android.media.MediaRecorder de importação;
android.os.Bundle de importação;
android.os.Environment de importação;
android.view.View de importação;
android.widget.Button de importação;
public class RecordSamples extends Activity {
privada estática final String AUDIO_RECORDER_FILE_EXT_3GP = ". 3gp";
privada estática final String AUDIO_RECORDER_FILE_EXT_MP4 = ". mp4";
privada estática final String AUDIO_RECORDER_FOLDER = "AudioRecorder";
gravador de MediaRecorder privado = null;
private int currentFormat = 0;
private int output_formats [] = {MediaRecorder.OutputFormat.MPEG_4, MediaRecorder.OutputFormat.THREE_GPP};
private String file_exts [] = {AUDIO_RECORDER_FILE_EXT_MP4, AUDIO_RECORDER_FILE_EXT_3GP};
@ 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 (id int, boolean isEnable) {
((Button)findViewById(id)).setEnabled(isEnable);
}
enableButtons void privada (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();
Arquivo arquivo = novo File(filepath,AUDIO_RECORDER_FOLDER);
if(!File.Exists()) {
file.Mkdirs();
}
retornar (file.getAbsolutePath() + "/" + System.currentTimeMillis() + file_exts[currentFormat]);
}
private void startRecording() {
gravador = novo 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);
Tente {
Recorder.Prepare();
Recorder.Start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void stopRecording() {
se (null! = gravador) {
Recorder.Stop();
Recorder.Reset();
Recorder.Release();
gravador = null;
}
}
private void displayFormatDialog() {
Construtor de AlertDialog.Builder = novo AlertDialog.Builder(this);
Formatos de Cadeia de caracteres [] = {"MPEG 4", "3GPP"};
Builder.setTitle(GetString(R.String.choose_format_title))
.setSingleChoiceItems (formatos, currentFormat, Nova DialogInterface.OnClickListener() {
@ Override
public void onClick (DialogInterface diálogo, int quais) {
currentFormat = qual;
setFormatButtonCaption();
dialog.dismiss();
}
})
.Show();
}
privado MediaRecorder.OnErrorListener errorListener = novo MediaRecorder.OnErrorListener() {
@ Override
public void onError (MediaRecorder Senhor, int que, int extra) {
AppLog.logString ("erro:" + que + "," + extra);
}
};
privado MediaRecorder.OnInfoListener infoListener = novo MediaRecorder.OnInfoListener() {
@ Override
public void onInfo (MediaRecorder Senhor, int que, int extra) {
AppLog.logString ("aviso:" + que + "," + extra);
}
};
seguido por eu usei onclicklistenter e chamado startRecording();,stopRecording(); d não está funcionando... Ele está mostrando erro como "o aplicativo android parou inesperadamente".