简体   繁体   中英

How to save audio of AudioRecord in a file?

Background: I am making an android application by which I would capture the live audio of user and then transmit it to his PC via Socket. I am using AudioRecord for live recording, and AudioTrack for listening the Audio. I am able to capture the live audio and then play it at same time.

Problem: I want to save the voice of user that I am capturing live, in a file which is Playable. I tried saving the output in a file with a.mp3 and.pcm extension, but these files are not playable. How I can save the output genereated by AudioRecord Object in a file that can be Playable.
Below is my Code for Capturing and Playing Live Audio:

 AudioRecord recorder;
AudioTrack audioTrack;

short[] buffer;
int sampleRate = AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_MUSIC);//48000
int channelConfig = AudioFormat.CHANNEL_IN_MONO;
int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
int bufferSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);//3840

DataOutputStream dos;

                        recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
                                sampleRate,
                                channelConfig ,
                                AudioFormat.ENCODING_PCM_16BIT,
                                bufferSize );

                        recorder.startRecording();

                        //For playing audio
                        audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC
                                , sampleRate
                                , AudioFormat.CHANNEL_OUT_MONO
                                , AudioFormat.ENCODING_PCM_16BIT
                                , bufferSize
                                , AudioTrack.MODE_STREAM);

                        audioTrack.setPlaybackRate(sampleRate);
                        audioTrack.play();

                        buffer = new short[bufferSize/4];
                        audioPlaying =true;
                     

                        try {
                            dataOutputStream = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(getRecordingFilePath())));

                        } catch (Exception e) {
                            e.printStackTrace();
                            Log.d(TAG,"Exception >>"+e.toString());

                        }

                        while (audioPlaying){
                            //reading audio from buffer
                            int bufferReadResult =recorder.read(buffer, 0, bufferSize/4);

                            //playing that audio simultaneously
                            audioTrack.write(buffer, 0, bufferSize/4);

                            //Saving file (Unable to Play it)
                            try {
                                     for (int i = 0; i < bufferReadResult; i++ )
                                     {
                                         dataOutputStream.writeShort(buffer[i]);
                                     }
                                     
                                Log.d(TAG,"Writing file");

                            } catch (Exception e) {
                                Log.d(TAG,"183 Eception>>"+e.toString());
                                e.printStackTrace();
                            }
                        }


private String getRecordingFilePath()
    {
        Log.d(TAG, "Creating File for Audio");
        //I am using android 11

        ContextWrapper contextWrapper= new ContextWrapper(getApplicationContext());
        File musicDirectory = contextWrapper.getExternalFilesDir(Environment.DIRECTORY_MUSIC);
        File file = new File(musicDirectory,"testAudioFile.pcm");
        String path = file.getPath();
        Log.d(TAG, "File created at path:" + path);

        return path;
    }

After some research, I found a solution. Not perfect, but somehow helped me. If anyone knows better answer then then the below code, Post it.

private void playFileAudio() {
    byte[] byteData = null;
    File file = null;
    file = new File(getRecordingFilePath());
    byteData = new byte[(int) file.length()];
    FileInputStream fileInputStream = null;
    try {
        fileInputStream = new FileInputStream( file );
        fileInputStream.read( byteData );
        fileInputStream.close();
        Log.d(TAG,"File Readed");

    } catch (Exception e) {
        Log.d(TAG,"Exception>>:"+e.toString());
        e.printStackTrace();
    }

    if (audioTrack!=null) {
        audioTrack.play();
        Log.d(TAG, "File is being played");
        audioTrack.write(byteData, 0, byteData.length);
        audioTrack.stop();
        audioTrack.release();
    }
    else{
        Log.d(TAG, "audio track is not initialised ");

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM