使用AudioRecord录音。生成的文件无法播放

时间:2019-01-28 11:03:08

标签: android audio-recording audiorecord android-audiorecord

我正在为我的应用程序使用Android Audio Record。但是使用此文件创建的文件无法播放。我尝试了所有可能的格式和采样率,但没有一个可以播放。 以下是代码:

我尝试了所有可能的输出流(DataOutputSteam,BufferedOutputStream)。所有可能的格式(.3gp,.amr,.wav)和采样率(8000、44100等)。

private static final int RECORDER_CHANNEL = AudioFormat.CHANNEL_IN_MONO;
private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
private static final int SAMPLE_RATE = 44100;

private boolean isRecordingP = false;

int minBufferSizeInBytes;

private void startRecordingP(String fileName) {

    Debug.d(TAG,"||| startRecordingP |||");

    minBufferSizeInBytes = AudioRecord.getMinBufferSize( SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT );

    Debug.d(TAG,"getMinBufferSize size result = "+minBufferSizeInBytes);
    audioRecord = new AudioRecord(MediaRecorder.AudioSource.VOICE_COMMUNICATION,
            SAMPLE_RATE, RECORDER_CHANNEL ,
            RECORDER_AUDIO_ENCODING, minBufferSizeInBytes * 2);

    audioRecord.startRecording();
    isRecordingP = true;
    mServiceHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            writeAudioDataToFile(fileName);
        }
    },0);
}

private void stopRecordingP() {
    // stops the recording activity
    Debug.d(TAG,"||| stopRecordingP |||");
    isRecordingP = false;
    if (null != audioRecord) {
        audioRecord.stop();
        audioRecord.release();
        audioRecord = null;
    }
}

private void writeAudioDataToFile(String filePath) {

    String recordingPathFolder = RecordingUtil.getRecordingPath();

    File dir = new File(recordingPathFolder);
    if (!dir.exists()) {
        dir.mkdirs();
    }

    File file = null;
    try {
        file = File.createTempFile("call_" + filePath + "_", ".wav", dir);
    } catch (IOException e) {
        e.printStackTrace();
    }

    int recBufferByteSize = minBufferSizeInBytes * 2;
    byte[] recBuffer = new byte[recBufferByteSize];

    FileOutputStream os = null;
    try {
        os = new FileOutputStream(file.getAbsolutePath());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    DataOutputStream dos = new DataOutputStream(os);

    while (isRecordingP) {

        Debug.d(TAG, "recording state "+audioRecord.getRecordingState()+" format "+audioRecord.getAudioFormat()
                +" channel count "+audioRecord.getChannelCount()+ " sample rate = "+audioRecord.getSampleRate());

        int bytesRecorded =  audioRecord.read(recBuffer, 0, minBufferSizeInBytes);

        if (bytesRecorded == AudioRecord.ERROR_INVALID_OPERATION || bytesRecorded == AudioRecord.ERROR_BAD_VALUE) {
            Debug.d(TAG, "error "+bytesRecorded);
            continue;
        }

        Debug.d(TAG,"writing to file "+bytesRecorded);
        try {
            if (bytesRecorded != 0 && bytesRecorded != -1) {
                dos.write(recBuffer, 0, bytesRecorded);
            } else {
                break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    Debug.d(TAG, "saved at : "+file.getAbsolutePath());

    try {
        os.flush();
        os.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

该文件无法播放。

0 个答案:

没有答案