Android:录音和保存音频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10905337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:24:24  来源:igfitidea点击:

Android: Voice Recording and saving audio

androidandroid-intentandroid-mediaplayervoice-recognitionandroid-audiomanager

提问by RagHaven

I am working on application that will record the voice of the user and save the file on the SD card and then allow the user to listen to the audio again.

我正在开发的应用程序将记录用户的声音并将文件保存在 SD 卡上,然后允许用户再次收听音频。

I am able to allow the user to record his voice using the RecognizerIntent, but I cant figure out how to save the audio file and allow the user to hear the audio. I would appreciate it if someone could help me out. I have displayed my code below:

我能够允许用户使用 RecognizerIntent 录制他的声音,但我无法弄清楚如何保存音频文件并允许用户听到音频。如果有人可以帮助我,我将不胜感激。我在下面显示了我的代码:

    // Setting up the onClickListener for Audio Button
    attachVoice = (Button) findViewById(R.id.AttachVoice_questionandanswer);
    attachVoice.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
        Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        voiceIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Please Speak");
        startActivityForResult(voiceIntent, VOICE_REQUEST);
        }
    });

protected void onActivityResult(int requestCode, int resultCode, Intent data) {


  if(requestCode == VOICE_REQUEST && resultCode == RESULT_OK){




}

采纳答案by K_Anas

There is an example of how to do audio capture using MediaRecorder in the Android Developer Documentation.

Android 开发人员文档中有一个如何使用 MediaRecorder 进行音频捕获的示例

I would recommend saving the files on the SD Card and then have your gallery code check the SD card to see which files to display. You can get the directory of the SD Card using the Environment.getExternalStorageDirectory()method. It would be best to save your files in a subdirectory of the SD Card root directory.

我建议将文件保存在 SD 卡上,然后让您的画廊代码检查 SD 卡以查看要显示哪些文件。您可以使用Environment.getExternalStorageDirectory()方法获取 SD 卡的目录。最好将文件保存在 SD 卡根目录的子目录中。

Make sure you give your applications the Permissionsit will need. At the very least it will need RECORD_AUDIOand WRITE_EXTERNAL_STORAGE.

确保为应用程序提供所需的权限。至少它需要RECORD_AUDIOWRITE_EXTERNAL_STORAGE

Also you have to see these tutorials:

您还必须查看这些教程:

http://www.androiddevblog.net/android/android-audio-recording-part-1

http://www.androiddevblog.net/android/android-audio-recording-part-1

http://www.androiddevblog.net/android/android-audio-recording-part-2

http://www.androiddevblog.net/android/android-audio-recording-part-2

回答by Kaarel

If you reallywant to record audio via the speech recognition API then you could use the RecognitionService.Callbackwhich has a method

如果您真的想通过语音识别 API 录制音频,那么您可以使用具有方法的RecognitionService.Callback

void bufferReceived(byte[] buffer)

This gives you access to the recorded audio buffer as speech is being recorded and recognized. (No information is provided about the sample rate though.) You can then save the obtained buffers into a file for a later playback. I think keyboard apps use this call to display the waveform of the recorded speech. You have to implement the UI yourself.

这使您可以在录制和识别语音时访问录制的音频缓冲区。(虽然没有提供有关采样率的信息。)然后您可以将获得的缓冲区保存到文件中以供以后播放。我认为键盘应用程序使用此调用来显示录制语音的波形。您必须自己实现 UI。

The bare RecognizerIntent.ACTION_RECOGNIZE_SPEECHjust returns a set of words/phrases without any audio.

RecognizerIntent.ACTION_RECOGNIZE_SPEECH只返回一组没有任何音频的单词/短语。