Android recording method (the most important is the trial method of MediaRecorder), androidrecorder

Source: Internet
Author: User

Android recording method (the most important is the trial method of MediaRecorder), androidrecorder

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 !!!!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.