I am trying to record a pcm sound file and play it. When I play it, it sounds slow and takes longer than it was for recording. I am not sure if there is an error in the recording or playback code. Any ideas what the problem is?
I pretty much copied the code from this example: http://emeadev.blogspot.com/2009/09/raw-audio-manipulation-in-android.html
Here is the recording code (the isRecording flag is set by the stop button in the gui thread).
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO); int sampleRateInHz = 8000;//8000 44100, 22050 and 11025 int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO; int audioFormat = AudioFormat.ENCODING_PCM_16BIT; File sd = Environment.getExternalStorageDirectory(); File file = new File(sd, "msg.wav"); if (file.exists()) file.delete(); try { file.createNewFile(); } catch (IOException e) { Log.e("create file:", e.toString()); } try { OutputStream os = new FileOutputStream(file); BufferedOutputStream bos = new BufferedOutputStream(os); DataOutputStream dos = new DataOutputStream(bos); int bufferSize = AudioRecord.getMinBufferSize(sampleRateInHz,channelConfig, audioFormat); short[] buffer = new short[bufferSize]; audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRateInHz,channelConfig, audioFormat,bufferSize); audioRecord.startRecording(); isRecording = true; while (isRecording) { int bufferReadResult = audioRecord.read(buffer, 0, bufferSize); for (int i = 0; i < bufferReadResult; i++) { dos.writeShort(buffer[i]); } } dos.close();
This is the play code.
File file = new File(SendAlert.voiceFile);
java android
Andrew Thomas
source share