无法播放录制的音频文件

时间:2013-02-20 11:31:52

标签: android audio-recording real-time-updates

我正在使用一个教程录制语音并录制我正在使用课程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) {

        }
    }

谢谢你。

0 个答案:

没有答案