Hello,

Since my day 1 I´ve been facing problems with Android AudioRecord API.
After a lot of research and a lot more trial and error I´ve found my
way into the code and was able to write an asynctask for audio streams
recording which encapsules most of the audiorecord and asynctask
features as a lib, so the developers doesn´t need to worry about
finding the best audio parameters, work on stop and release, etc.
So far, everything was right.
Yesterday I started trying to process the audio I got from the
microfone input and my code was reacting bad. I revisited it many
times and decided then to check the audio I was getting from the mic.
Created in instance of AudioTrack and tried to play it back. The sound
was horrible, with lots of chopping, lots of distortion and giving me
even the sensation of low samplerate playback (a single word spoken
sometimes lasted for seconds on the earphone).
To check if it was actually my code I went for the internet and found
the code above. Tried it and got the same results. The only thing that
comes to my mind actually is that, it doesn´t matter what I do and the
configuration I try, I always get the buffer overflow message. Even if
I don´t actually do anything but read from AudioRecord and write to
AudioTrack (example above, and not in debug mode). If the emulator can
´t do it fast enought .... then I´m completly lost here.

Based on the output of my algorithm I think the audio streams I´m
reading are not right.

Have anyone faced this? does anyone know how to solve it? Could anyone
try the code (copy and paste) and check if it´s actually my computer
or my emulator?

package com.example.test;

import android.app.Activity;
import android.content.Context;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.os.Bundle;

public class test extends Activity {


    boolean isRecording; //currently not used
    AudioManager am;

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
       Record record = new Record();
       record.run();

   }

      public class Record extends Thread
      {
             // SoundPower sndPower = new SoundPower();

              static final int bufferSize = 200000;
              final short[] buffer = new short[bufferSize];
              short[] readBuffer = new short[bufferSize];

              public void run() {
                isRecording = true;
                android.os.Process.setThreadPriority
                (android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);

                int buffersize = AudioRecord.getMinBufferSize(8000,
                AudioFormat.CHANNEL_CONFIGURATION_MONO,
                AudioFormat.ENCODING_PCM_16BIT);
                buffersize = 2048;

                               AudioRecord arec = new
AudioRecord(MediaRecorder.AudioSource.MIC,
                                               8000,
 
AudioFormat.CHANNEL_CONFIGURATION_MONO,
 
AudioFormat.ENCODING_PCM_16BIT,
                                               buffersize);

                               AudioTrack atrack = new
AudioTrack(AudioManager.STREAM_VOICE_CALL,
                                               8000,
 
AudioFormat.CHANNEL_CONFIGURATION_MONO,
 
AudioFormat.ENCODING_PCM_16BIT,
                                               buffersize,
 
AudioTrack.MODE_STREAM);


                               atrack.setPlaybackRate(8000);

                               byte[] buffer = new byte[buffersize];
                               arec.startRecording();
                               atrack.play();

                               while(isRecording) {
                                       arec.read(buffer, 0,
buffersize);
                                       atrack.write(buffer, 0,
buffer.length);
                               }

                               arec.stop();
                               atrack.stop();
                               isRecording = false;
              }
      }
}


This code is not mine. I´ve just cut and replaced some values (as the
SampleRate) so it would work.

My specs:

AndroidOS 2.1, last emulator and package (downloaded one week ago).
Dell Vostro 1000 (onboard soundcard), windows XP SP3.

Thank you very much,
Gabriel Simões

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to