如何在记录之后创建.wav文件--Android Studio

时间:2016-08-21 09:25:30

标签: android audio createfile recorder

我尝试制作一个简单的录音机,当我开始操作时,我得到了fileNotFoundException。如何在特定文件夹中创建.wav文件,让我们说在外部卡上,这样我以后可以访问该文件夹来收听存储在那里的记录。

到目前为止,这是我的代码,我使用了library记录活动:

    public class MainActivity extends Activity {
    String filePath;
    int color ;
    int requestCode = 0;
    Button recordButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recordButton = (Button) findViewById(R.id.recordButton);

        recordButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                filePath = Environment.getExternalStorageDirectory() + "/recorded_audio.wav";
                color = getResources().getColor(R.color.colorPrimaryDark);
                AndroidAudioRecorder.with(MainActivity.this)
                        .setFilePath(filePath)
                        .setColor(color)
                        .setRequestCode(requestCode)
                        .record();
            }
        });


    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
                Toast.makeText(MainActivity.this, "Resoult ok", Toast.LENGTH_SHORT).show();
                // Great! User has recorded and saved the audio file
            } else if (resultCode == RESULT_CANCELED) {
                Toast.makeText(MainActivity.this, "Resoult not-ok", Toast.LENGTH_SHORT).show();
                // Oops! User has canceled the recording
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

答案是我必须在AndroidMainifers.xml中添加这些权限:

<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

答案 1 :(得分:1)

Android内置麦克风,您可以通过它捕获音频并将其存储或在手机中播放。有很多方法可以做到这一点,但最常见的方法是通过MediaRecorder类。

Android提供MediaRecorder类来录制音频或视频。要使用MediaRecorder类,您将首先创建MediaRecorder类的实例。其语法如下。

MediaRecorder myAudioRecorder = new MediaRecorder();

现在您将设置源,输出和编码格式以及输出文件。他们的语法如下。

myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(outputFile);

在指定音频源和格式及其输出文件后,我们可以调用两种基本方法准备并开始录制音频。

myAudioRecorder.prepare();
myAudioRecorder.start();

在您的代码中,只需执行以下更改: -

 outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp";;

      myAudioRecorder=new MediaRecorder();
      myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
      myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
      myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
      myAudioRecorder.setOutputFile(outputFile);

      record.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            try {
               myAudioRecorder.prepare();
               myAudioRecorder.start();
            }

            catch (IllegalStateException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
            }

            catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
            }

            record.setEnabled(false);
            stop.setEnabled(true);

            Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();
         }
      });

希望它有助于将权限也放在你的android清单文件中。

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
   <uses-permission android:name="android.permission.RECORD_AUDIO" />