Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recording audio not to file on Android

Tags:

java

android

I want android.media.MediaRecorder. to record audio not into file, but into same variable, for example char[ ] or byte[ ] or some other datta buffer structure. I want to send it to the remote server via Wi-Fi, can android.media.MediaRecorder provide this functionality?

like image 893
mmmaaak Avatar asked Jan 21 '13 11:01

mmmaaak


People also ask

Can I record just audio on my Android phone?

Swipe down from the top of your screen to see the quick settings tiles and tap the screen recorder button. A floating bubble will appear with a record and microphone button. If the latter is crossed out, you're recording internal audio, and if it's not, you get sound straight from your phone's mic.

How do I record audio secretly on Android?

How to record conversation secretly. To record sound secretly on your Android device, install the secret voice recorder app from the Google Play Store. Now, whenever you need to record audio secretly, just press the power button thrice within 2 seconds to start recording.


1 Answers

What you can do here is utilize the ParcelFileDescriptor class.

//make a pipe containing a read and a write parcelfd
ParcelFileDescriptor[] fdPair = ParcelFileDescriptor.createPipe();

//get a handle to your read and write fd objects.
ParcelFileDescriptor readFD = fdPair[0];
ParcelFileDescriptor writeFD = fdPair[1];

//next set your mediaRecorder instance to output to the write side of this pipe.
mediaRecorder.setOutputFile(writeFD.getFileDescriptor());

//next create an input stream to read from the read side of the pipe.
FileInputStream reader = new FileInputStream(readFD.getFileDescriptor());

//now to fill up a buffer with data, we just do a simple read
byte[] buffer = new byte[4096];//or w/e buffer size you want

//fill up your buffer with data from the stream
reader.read(buffer);// may want to do this in a separate thread

and now you have a buffer full of audio data

alternatively, you may want to write data directly to a socket from the recorder. this can also be achieved with the ParcelFileDescriptor class.

//create a socket connection to another device
Socket socket = new Socket("123.123.123.123",65535);//or w/e socket address you are using

//wrap the socket with a parcel so you can get at its underlying File descriptor
ParcelFileDescriptor socketWrapper = ParcelFileDescriptor.fromSocket(socket);

//set your mediaRecorder instance to write to this file descriptor
mediaRecorder.setOutputFile(socketWrapper.getFileDescriptor());

now any time your media recorder has data to write it will automatically write it over the socket

like image 127
S E Avatar answered Oct 02 '22 08:10

S E