Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>YOU CAN TAKE HELP FROM THIS CODE</p> <p><strong>AudioRecorderActivity.java</strong></p> <pre><code>public class AudioRecorderActivity 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, 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(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) { AppLog.logString("Error: " + what + ", " + extra); } }; private MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() {@Override public void onInfo(MediaRecorder mr, int what, int extra) { AppLog.logString("Warning: " + what + ", " + extra); } }; private View.OnClickListener btnClick = new View.OnClickListener() {@Override public void onClick(View v) { switch (v.getId()) { case R.id.btnStart: { AppLog.logString("Start Recording"); enableButtons(true); startRecording(); break; } case R.id.btnStop: { AppLog.logString("Start Recording"); enableButtons(false); stopRecording(); break; } case R.id.btnFormat: { displayFormatDialog(); break; } } } }; } </code></pre> <p><strong>AppLog.java</strong> </p> <pre><code>import android.util.Log; public class AppLog { private static final String APP_TAG = "AudioRecorder"; public static int logString(String message) { return Log.i(APP_TAG, message); } } </code></pre> <p><strong>main.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="20dip" &gt; &lt;ImageView android:layout_width="fill_parent" android:layout_height="wrap_content" android:scaleType="fitCenter" /&gt; &lt;LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" &gt; &lt;Button android:id="@+id/btnStart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1.0" android:text="@string/start_recording" /&gt; &lt;Button android:id="@+id/btnStop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1.0" android:text="@string/stop_recording" /&gt; &lt;Button android:id="@+id/btnFormat" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1.0" android:text="Format (mp4)" /&gt; &lt;/LinearLayout&gt; &lt;/LinearLayout&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload