Запись с использованием AudioRecord.Сгенерированный файл не воспроизводится - PullRequest
0 голосов
/ 28 января 2019

Я использую 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();
    }
}

Файл не воспроизводится.

...