음성 받아 드려서 어느 정도의 소리 이상일 경우 이벤트를 발생 하려고 구글링 해서 본 소스 입니다.
I am trying to record a pcm sound file and play it back. When I play it back, it sounds slow and takes longer than it did to record. I'm not sure if the error is in the record or play code. Any ideas what the problem is?
I largely copied code from this example: http://emeadev.blogspot.com/2009/09/raw-audio-manipulation-in-android.html
Here is the record code (isRecording flag is set by stop button in 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); // Get the length of the audio stored in the file (16 bit so 2 bytes per short) // and create a short array to store the recorded audio. int musicLength = (int)(file.length()/2); short[] music = new short[musicLength]; try { // Create a DataInputStream to read the audio data back from the saved file. InputStream is = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(is); DataInputStream dis = new DataInputStream(bis); // Read the file into the music array. int i = 0; while (dis.available() > 0) { music[i] = dis.readShort(); i++; } // Close the input streams. dis.close(); // Create a new AudioTrack object using the same parameters as the AudioRecord // object used to create the file. AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 8000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, musicLength, AudioTrack.MODE_STREAM); // Start playback audioTrack.play(); // Write the music buffer to the AudioTrack object audioTrack.write(music, 0, musicLength); |
buffer[i] 를 log 찍어 보시면 소리 db 따라 값이 출력 됩니다.
출처 - http://efreedom.com/Question/1-3604419/Android-Audio-Record-Playback-Corrupt
'Program > Android' 카테고리의 다른 글
(안드로이드 게임 점프점프 다운)JumpJump [Android Game] 추천 (0) | 2011.02.24 |
---|---|
memory leak 회피하기 (0) | 2010.11.12 |
안드로이드 얼굴 인식 (0) | 2010.10.08 |
안드로이드 단말(기기) 모델(기종) 정보 뽑아 오기 (0) | 2010.08.27 |
[Android] BitmapDrawable과 Bitmap (3) | 2010.05.20 |