2

I am recording voice using one tutorial and for recording i am using class AudioRecord as i want to show progressbar with some noise level. I am providing file in which the audio data will be stored. The size of file is being increasing as it gets audio data. But when i manually play that recorded file but it doesn't play anything. Can anyone tell me what i am missing?

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) {

        }
    }

Thanks in adavance.

Saurabh
  • 457
  • 2
  • 8
  • 26
  • 1
    How are you trying to play it? Most standalone players won't play pure PCM data, which is what this is. You need to add some sort of WAV header to it, or convert it to another format. – Geobits Feb 20 '13 at 14:31
  • by opening file from the sdcard. can u give me some hint to convert it to another format to store this recorded file? – Saurabh Feb 20 '13 at 14:35
  • If you want WAV(large filesize), you can just add a standard header to it. For anything else(MP3, OGG, etc), you might want to google up a third party library to do the job for you. – Geobits Feb 20 '13 at 14:36
  • can you please elaborate it more? for where should i have to add a standard header? – Saurabh Feb 20 '13 at 14:39
  • No, but [this guy](http://stackoverflow.com/a/4779722/752320) can. – Geobits Feb 20 '13 at 14:43

0 Answers0