How to use a microphone on Android

I just started developing my first Android application, and it's hard for me to figure out how to start the microphone and listen to it, which is the main feature of my application.

I searched for Android docs and I cannot find much information about this.

Thanks in advance.

+6
source share
2 answers

Perhaps this may help (in fact, from Android docs):
Audio recording

  • Create a new instance of android.media.MediaRecorder .
  • Set the sound source using MediaRecorder.setAudioSource() . You probably want to use MediaRecorder.AudioSource.MIC .
  • Set the output file format using MediaRecorder.setOutputFormat() .
  • Name the output file using MediaRecorder.setOutputFile() .
  • Install the audio MediaRecorder.setAudioEncoder() using MediaRecorder.setAudioEncoder() .
  • Call MediaRecorder.prepare() on the MediaRecorder instance.
  • To start recording audio, call MediaRecorder.start() .
  • To stop capturing audio, call MediaRecorder.stop() .
  • When you are done with the MediaRecorder instance, call MediaRecorder.release() on it. Calling MediaRecorder.release() always recommended to immediately release the resource.

or:
Android Audio Tutorial

+10
source

You can use a custom recorder:

  final static int RQS_RECORDING = 1; Uri savedUri; Button buttonRecord; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test); buttonRecord = (Button) findViewById(R.id.record); buttonRecord.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent( MediaStore.Audio.Media.RECORD_SOUND_ACTION); startActivityForResult(intent, RQS_RECORDING); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub if (requestCode == RQS_RECORDING) { savedUri = data.getData(); Toast.makeText(MainActivity.this, "Saved: " + savedUri.getPath(), Toast.LENGTH_LONG).show(); } } 
0
source

Source: https://habr.com/ru/post/891916/


All Articles