簡體   English   中英

如何將 AudioRecord 的音頻保存到文件中?

[英]How to save audio of AudioRecord in a file?

背景:我正在制作一個 android 應用程序,通過它我可以捕獲用戶的實時音頻,然后通過 Socket 將其傳輸到他的 PC。 我使用 AudioRecord 進行現場錄音,使用 AudioTrack 收聽音頻。 我能夠捕捉現場音頻,然后同時播放。

問題:我想將我正在實時捕獲的用戶聲音保存在一個可播放的文件中。 我嘗試將 output 保存在擴展名為 .mp3 和 .pcm 的文件中,但這些文件無法播放。 我如何將 AudioRecord Object 生成的 output 保存在可以播放的文件中。
以下是我捕獲和播放現場音頻的代碼:

 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;
    }

經過一些研究,我找到了解決方案。 不完美,但以某種方式幫助了我。 如果有人知道更好的答案,那么下面的代碼,發布它。

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 ");

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM