Android recording implementation method (the most important is the trial method of MediaRecorder)
package cn.eoe.record;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.view.View.OnClickListener;public class MainActivity extends Activity {private OnClickListener btnClickListener = new OnClickListener() {@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btnStartRecord :startRecord();break;case R.id.btnStopRecord :stopRecord();break;}}};private void startRecord() {if (mp == null) {File dir = new File(Environment.getExternalStorageDirectory(), "sounds");if (!dir.exists()) {dir.mkdirs();}File soundFile = new File(dir, System.currentTimeMillis() + ".amr");if (!soundFile.exists()) {try {soundFile.createNewFile();} catch (IOException e) {e.printStackTrace();}}mp = new MediaRecorder();mp.setAudioSource(MediaRecorder.AudioSource.MIC);mp.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);mp.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);mp.setOutputFile(soundFile.getAbsolutePath());try {mp.prepare();mp.start();} catch (IllegalStateException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}private void stopRecord() {if (mp != null) {mp.stop();mp.release();mp = null;}}private MediaRecorder mp = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findViewById(R.id.btnStartRecord).setOnClickListener(btnClickListener);findViewById(R.id.btnStopRecord).setOnClickListener(btnClickListener);}}
The most important thing is the MediaRecorder operation. How to release it, how to initialize it, and then play the video. It is actually very simple.
The key code is here:
mp = new MediaRecorder();mp.setAudioSource(MediaRecorder.AudioSource.MIC);mp.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);mp.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);mp.setOutputFile(soundFile.getAbsolutePath());
When the recording is stopped, you need to stop it directly. Release the MediaRecorder () object and release it as null.
Stop first, then Release (), and then the value is blank!
The latest speech: technology requires constant learning and continuous efforts! Don't stop. Only science and technology are the primary productive forces. In the past two years, mining technology has not continued, resulting in poor technology. You have to work hard !!!
The technology is not very good, and I have been talked several times by the leaders, which has deepened my thoughts on myself. I cannot stop working hard. Come on !!!!