如何在Android中使用AudioRecord将录制的声音保存到文件?

时间:2016-12-08 12:44:24

标签: android

我正在开发一个android(compileSdkVersion 23)应用程序来使用AudioRecord录制音频,使用它的原因是在FFT后实时获取频率。

不仅这项工作,我需要保存录制的声音以检查声音(在此过程中,跟踪频率是不必要的。)

如何使用Android中的AudioRecord将录制的声音保存到文件中?

因此,我正确使用AudioRecord吗?

这是代码:

public class MainActivity extends Activity {

    int frequency = 8000;
    int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
    int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;

    AudioRecord audioRecord;
    RecordAudio recordTask;

    int blockSize;// = 256;
    boolean started = false;
    boolean CANCELLED_FLAG = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        blockSize = 256;

        final Button btRec = (Button) findViewById(R.id.btRec);

        btRec.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (started == true) {
                //started = false;
                    CANCELLED_FLAG = true;
                //recordTask.cancel(true);
                    try{
                        audioRecord.stop();
                    }
                    catch(IllegalStateException e){
                        Log.e("Stop failed", e.toString());

                    }
                    btRec.setText("Start");

      //          canvasDisplaySpectrum.drawColor(Color.BLACK);

                }

                else {
                    started = true;
                    CANCELLED_FLAG = false;
                    btRec.setText("Stop");
                    recordTask = new RecordAudio();
                    recordTask.execute();
                }
            }
        });
    }


    private class RecordAudio extends AsyncTask<Void, double[], Boolean> {

        @Override
        protected Boolean doInBackground(Void... params) {

            int bufferSize = AudioRecord.getMinBufferSize(frequency,
                    channelConfiguration, audioEncoding);
            audioRecord = new AudioRecord(
                MediaRecorder.AudioSource.DEFAULT, frequency,
                channelConfiguration, audioEncoding, bufferSize);
            int bufferReadResult;
            short[] buffer = new short[blockSize];
            double[] toTransform = new double[blockSize];
            try {
                audioRecord.startRecording();
            } catch (IllegalStateException e) {
                Log.e("Recording failed", e.toString());

            }
            while (started) {
                if (isCancelled() || (CANCELLED_FLAG == true)) {
                    started = false;
                    //publishProgress(cancelledResult);
                    Log.d("doInBackground", "Cancelling the RecordTask");
                    break;
                } else {
                    bufferReadResult = audioRecord.read(buffer, 0, blockSize);

                    for (int i = 0; i < blockSize && i < bufferReadResult; i++) {
                        toTransform[i] = (double) buffer[i] / 32768.0; // signed 16 bit
                }

                 //transformer.ft(toTransform);

                 //publishProgress(toTransform);

                }

            }
            return true;
        }
    }

}

1 个答案:

答案 0 :(得分:0)

你必须下载你的文件并保存在缓存中,而不是你必须检查cahce文件的任何请求,如果它可用,否则请求新文件

如需完整帮助,请查看我的答案之一Download and cache media files