开发者

How to use the microphone on Android

I have just started to develop my first Android app, and I am having a hard time figuring out how to start the micropho开发者_如何学Gone and have it listen, which is a main feature of my app.

I've searched the Android docs and I can't find much info on this.

Thanks in advance.


Maybe this can help (actually from the Android docs):
Audio Capture

  1. Create a new instance of android.media.MediaRecorder.
  2. Set the audio source using MediaRecorder.setAudioSource(). You will probably want to use MediaRecorder.AudioSource.MIC.
  3. Set output file format using MediaRecorder.setOutputFormat().
  4. Set output file name using MediaRecorder.setOutputFile().
  5. Set the audio encoder using MediaRecorder.setAudioEncoder().
  6. Call MediaRecorder.prepare() on the MediaRecorder instance.
  7. To start audio capture, call MediaRecorder.start().
  8. To stop audio capture, call MediaRecorder.stop().
  9. When you are done with the MediaRecorder instance, call MediaRecorder.release() on it. Calling MediaRecorder.release() is always recommended to free the resource immediately.

or:
Android Audio Recording Tutorial


You can use 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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜