I have found another way to record audio without using MediaRecorder,
but I'd like to hear feedback from an Android OS/audio expert.

I use the AudioRecord class, which records audio and allows you to
read the data into your own buffer.  You can also do something like:

int SAMPLE_RATE_IN_HERTZ = 8000;
int BUFFER_SIZE_IN_BYTES = 4096;
int CHUNK_SIZE = 320;
AudioRecord audioRecord = new AudioRecord
(MediaRecorder.AudioSource.MIC, SAMPLE_RATE_IN_HERTZ,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, BUFFER_SIZE_IN_BYTES);
audioRecord.startRecording();
while (true) { int bytesRead = audioRecord.read(buffer, 0,
CHUNK_SIZE); }

Similarly, the AudioTrack does playing via buffers.  So, you can do
playback by doing something like:

AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_SYSTEM,
SAMPLE_RATE_IN_HERTZ, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, BUFFER_SIZE_IN_BYTES,
AudioTrack.MODE_STREAM);
audioTrack.play();
int bytesWritten = audioTrack.write(buffer, 0, CHUNK_SIZE);

You can even record and play simultaneously, i.e. record audio into
your buffer via audioRecord.read(), then play that same data via
audioTrack.write().

Of course, I'm leaving out a lot of details such as proper looping,
handling buffer overlows, using non-zero offset values instead of hard-
coding 0, handling stops and restarts, etc.  But hopefully this gives
you an idea.

Note that as soon as you startRecording(), you must "immediately" and
"constantly" consume the audio data, otherwise you will lose the data
and get buffer overflows.  This can be mitigated with larger buffers
and chunks.  But beware if you are debugging and stepping through
code, because you will get buffer overflow errors, presumably because
there's a separate system thread that is still constantly recording
and filling its internal buffer, and that buffer will fill up quickly
while you're debugging your own thread.

Anyway, there's a non-MediaRecorder solution that you can play with...
but again, I have no idea if it's the preferred method, and I'd like
to hear comments from an Android OS/audio expert.

-- PJ



On Nov 9, 10:39 pm, Hunter  Peress <[email protected]> wrote:
> One more way of framing the Issue. Is there a way to access things
> with AID_SYSTEM,AID_AUDIO (aka /dev/msm_pcm_out) from an app?
>
> This capability...to record direct from the system. was available on
> iphone years ago. so, I see this as a pretty darn pressing issue. Not
> to mention that people have been asking for this capability for
> android for years now too.
>
> On Nov 9, 6:57 pm, Hunter  Peress <[email protected]> wrote:
>
>
>
> > Im trying to record audio directly from the system. Using the standard
> > API, you can only record from the MIC or phonecalls, ala:
> > MediaRecorder
>
> > Some ways I've tried is to read from /dev/audio and /dev/eac from my
> > app, but I think the sandbox of the app is preventing this.
>
> > Im thinking about using the NDK, but Im not sure if that would
> > overcome the sandbox aspect ?
>
> > Ideally, there is some solution outthere to access the audio sub-
> > system directly. Whether it be directly to alsa, to the AudioFlinger,
> > to /dev/eac
>
> > Anyone have ideas?
>
> > This file 
> > here:http://www.google.com/codesearch/p?hl=en&sa=N&cd=11&ct=rc#-EqHK0vgjLY...
>
> > apparently accesses /dev/audio, but when I try, it says it doesnt
> > exist.
>
> > Any clues would help,thanks.

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

Reply via email to