2

我正在使用一个教程录制语音,并且为了录制我正在使用课程AudioRecord,因为我想显示带有一些噪音水平的进度条。我正在提供将存储音频数据的文件。随着获取音频数据,文件的大小正在增加。但是当我手动播放该录制的文件但它不播放任何东西时。谁能告诉我我错过了什么?

private class RecordAudio extends AsyncTask<Void, Void, Void> {

        boolean alertshown = false;
        boolean smsdone = false;
        boolean calldone = false;
        int blocksize = 256;
        @Override
        protected Void doInBackground(Void... params) {
            //isRecording = true;
            try {
                BufferedOutputStream dos = new BufferedOutputStream(new FileOutputStream(babyNoiseFile));
                DataOutputStream dosTemp = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(babyNoiseTempFile)));


                int bufferSize = AudioRecord.getMinBufferSize(frequency,channelConfiguration, audioEncoding);
                AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency,channelConfiguration, audioEncoding, bufferSize);

                short[] buffer = new short[bufferSize];
                audioRecord.startRecording();

                while (isMonitoringStarted) {
                    int bufferReadResult = audioRecord.read(buffer, 0,bufferSize);

                    //for (int i = 0; i < buffer.length; i++) {
                    for (int i = 0; i < bufferReadResult; i++) {

                        prgrssbarNoiseStatus.setProgress(buffer[i]);
                        lastlevel = buffer[i];
                        //txtvwProgress.setText(""+lastlevel);
                        //System.out.println(lastlevel);
                        if (lastlevel > lowSensitivityLevel && lastlevel < mediumSensitivityLevel){
                            dos.write(buffer[i]);
                            publishProgress();
                            break;
                        } 
                        if (lastlevel > mediumSensitivityLevel && lastlevel < highSensitivityLevel) {
                            dos.write(buffer[i]);
                            publishProgress();
                            break;
                        }
                        if (lastlevel > highSensitivityLevel) {
                            dos.write(buffer[i]);
                            publishProgress();
                            break;
                        }

                    }
                    for (int i = 0; i < bufferReadResult; i++) {
                        dosTemp.writeShort(buffer[i]);

                    }
                    //publishProgress(new Integer(r));
                }
                audioRecord.stop();
                if (isMonitoringStarted) {

                }
                dos.close();
                dosTemp.close();
            } catch (Throwable t) {
                t.getStackTrace();
                t.printStackTrace();
                Log.e("AudioRecord", "Recording Failed");
            }
            return null;
        }

        protected void onProgressUpdate(Void... progress) {

            if (lastlevel > lowSensitivityLevel && lastlevel < mediumSensitivityLevel) {
                //islowFired = false;
                System.out.println("low");
                if (!islowFired) {
                    islowFired = true;
                    lowTimer = new LowActionTimer(2000, 1000);
                    lowTimer.start();
                }

                //Toast.makeText(HomeActivity.this, "Call will fire!!", Toast.LENGTH_SHORT).show();
            }
            if (lastlevel > mediumSensitivityLevel && lastlevel < highSensitivityLevel) {
                //if (islowFired) {
                lowTimer.cancel();
                mediumTimer = new MediumActionTimer(2000, 1000);
                mediumTimer.start();
                //}
                //Toast.makeText(HomeActivity.this, "email will fire!!", Toast.LENGTH_SHORT).show();
                System.out.println("medium");
            }
            if (lastlevel > highSensitivityLevel) {
                //Toast.makeText(HomeActivity.this, "notification will fire!!", Toast.LENGTH_SHORT).show();
                System.out.println("call will fire!!");
            }
}
        protected void onPostExecute(Void result) {

        }
    }

提前致谢。

4

0 回答 0