I am currently doing some research in my company for a voip application.
We want to record the microphone sound of the device in amr, encrypt it and send data to our server, from which other streams transmit and decrypt it. For some tests, I am currently using MediaRecorder to record sound in amr. Since I want to continuously transmit the current sound, I cannot use the file to store data. So I created a channel and passed it as a file descriptor to MediaRecorder-Reference.
MediaRecorder mediaRecorder = createMediaRecorder(); //make a pipe containing a read and write parcelFd try { ParcelFileDescriptor[] fdPair = ParcelFileDescriptor.createPipe(); //get a handle to your read and write fd objects ParcelFileDescriptor readFd = fdPair[0]; ParcelFileDescriptor writeFd = fdPair[1]; //set the 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()); FileOutputStream writer = new FileOutputStream(writeFd.getFileDescriptor()); //now to fill up a buffer with data, only do a simple read byte[] buffer = new byte[BUFSIZE]; mediaRecorder.prepare(); //by starting the mediarecorder, it will write into the writeFD mediaRecorder.start(); Log.i(LOG_TAG, "Start Recording"); while(_isRecording){ int length = 0; while((length = reader.read(buffer)) > 0){ writer.write(buffer, 0, length); _audioListener.onAudioRecorded(buffer, 0, length); } } Log.i(LOG_TAG, "Stop Recording"); writer.flush(); writer.close(); reader.close(); tearDownMediaRecorder(mediaRecorder); } catch (IOException e) { e.printStackTrace(); }
The AudioListener will send the read bytes to my Activity, where I hold an instance of MediaPlayer that needs to read the data and play it back. I know that it doesn’t make much difference to hear what I just said, but it’s just for testing how I can read the contents of the pipe in MediaPlayer. So this is what my activity does.
ParcelFileDescriptor[] fdPair = ParcelFileDescriptor.createPipe(); _readFd = fdPair[0]; // _writer = new FileOutputStream(_writeFd.getFileDescriptor()); _reader = new FileInputStream(_readFd.getFileDescriptor()); _mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); _mediaPlayer.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { Log.i(LOG_TAG, "Media-Player Completed."); } }); _mediaPlayer.setOnErrorListener(new OnErrorListener() { @Override public boolean onError(MediaPlayer mp, int what, int extra) { Log.i(LOG_TAG, "Error-What: " + what); return false; } }); _mediaPlayer.setDataSource(_readFd.getFileDescriptor()); Log.i(LOG_TAG, "Set Source of MediaPlayer"); _mediaPlayer.prepare(); _mediaPlayer.start(); } System.arraycopy(buffer, 0, mAudioTxBuffer, 0, bytesRead);
//_writer.write(mAudioTxBuffer); _reader.read (mAudioTxBuffer);
I am currently getting the following exception from MediaPlayer if I want to play back the bytes received:
09-18 10:38:10.785: W/System.err(12136): java.io.IOException: setDataSourceFD failed.: status=0x80000000
Currently I do not know if I understand something wrong or I have a mistake in the code.
Thanks for your help.