Step 6: Learn android multimedia MediaRecorder recording

Source: Internet
Author: User

Step 6: Learn android multimedia MediaRecorder recording

MediaRecorder is the recording
To view the API first, you can see the lifecycle of the MediaRecorder.

The API not only provides a lifecycle diagram, but also provides examples as follows:

MediaRecorder recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); recorder.setOutputFile(PATH_NAME); recorder.prepare(); recorder.start();   // Recording is now started ... recorder.stop(); recorder.reset();   // You can reuse the object by going back to setAudioSource() step recorder.release(); // Now the object cannot be reused

After viewing the API, we can try to write it;
[1] UI: two buttons, one playing and one stopping
[2] Click events
[3] Do not forget to add permissions


   
   
  

[4] Get MediaRecorder and set parameters

Recorder = new MediaRecorder (); // audio source recorder. setAudioSource (MediaRecorder. audioSource. MIC); // set the output format recorder. setOutputFormat (MediaRecorder. outputFormat. THREE_GPP); // Audio Encoding recorder. setAudioEncoder (MediaRecorder. audioEncoder. AMR_NB); // The setting is saved under sdcard named myrecord.3gp recorder. setOutputFile (Environment. getExternalStorageDirectory () +/myrecord.3gp );

[5] Start recording

 recorder.prepare();                    recorder.start();

[6] Stop recording

 recorder.stop();                recorder.reset();                recorder.release();

This has been fully implemented.

The complete source code is as follows:

public class MainActivity extends Activity implements View.OnClickListener{    private Button mButtonStart;    private Button mButtonStop;    private MediaRecorder recorder;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mButtonStart= (Button) findViewById(R.id.button_start);        mButtonStop= (Button) findViewById(R.id.button_stop);        mButtonStop.setOnClickListener(this);        mButtonStart.setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()){            case R.id.button_start:                recorder=new MediaRecorder();                recorder.setAudioSource(MediaRecorder.AudioSource.MIC);                recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);                recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);                recorder.setOutputFile(Environment.getExternalStorageDirectory() + /myrecord.3gp);                try {                    recorder.prepare();                    recorder.start();                } catch (IOException e) {                    e.printStackTrace();                }                break;            case R.id.button_stop:                recorder.stop();                recorder.reset();                recorder.release();                break;            default:break;        }    }}

 

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.