音频播放就像快进

时间:2015-11-03 09:53:20

标签: android audio wav

我正在创建一个录制音频的应用程序。为此我正在使用AudioRecord类。我正在将录制的pcm文件转换为WAV格式。问题是,当我播放录制的音频时,声音播放就像它的快速转发。我做了一些研究,但仍然没有成功。请帮助我 代码

/*Recording Audio*/
                /*Sampling rate*/

    private static final String AUDIO_RECORDER_FOLDER = "Active Audio";
    private static final String AUDIO_RECORDER_TEMP_FILE = "record_temp.raw";

    private static final int SAMPLING_RATE = 44100;

    private int mBufferSize;
    private short[] mAudioBuffer;
    private int BufferElements2Rec = 1024; // want to play 2048 (2K) since 2 bytes we use only 1024
    private int BytesPerElement = 2; // 2 bytes in 16bit format

    private AudioRecord audioRecord;
    private boolean isRecording = false;
    private RecordingThread recordingThread; 

主题

/*Recording stuff*/
        // Compute the minimum required audio buffer size and allocate the buffer.
        mBufferSize = AudioRecord.getMinBufferSize(SAMPLING_RATE, AudioFormat.CHANNEL_IN_MONO,
                AudioFormat.ENCODING_PCM_16BIT);
        mAudioBuffer = new short[mBufferSize / 2];

        recordingThread = new RecordingThread();/*Recording stuff*/
        // Compute the minimum required audio buffer size and allocate the buffer.
        mBufferSize = AudioRecord.getMinBufferSize(SAMPLING_RATE, AudioFormat.CHANNEL_IN_MONO,
                AudioFormat.ENCODING_PCM_16BIT);

        recordingThread = new RecordingThread();

 public void run() {

            if (getValidSampleRates()) {
                audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLING_RATE,
                        AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, BufferElements2Rec * BytesPerElement);

                if (audioRecord.getState() == AudioRecord.STATE_INITIALIZED) {
                    audioRecord.startRecording();
                } else {

                    if (Constants.isLoggingEnable) {
                        Logger.logError(TAG, "Error occured");

                    }

                }


            } else {

                Toast.makeText(context, "Sampling rate not supported", Toast.LENGTH_SHORT).show();
            }

            synchronized (this) {

                if (pause) {
                    pause = false;
                    try {
                        wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {


                    if (isRecording) {

                        writeAudioDataToFile();

                    }

                }
            }

            if (isRecording) {
                writeAudioDataToFile();

            }


        }

        public synchronized void pause() {
            pause = true;
        }

        public synchronized void go() {
            this.notify();
        }
    }

将数据写入文件

private void writeAudioDataToFile() {
        // Write the output audio in byte

        String fileName = getTempFilename();
        short sData[] = new short[mBufferSize];


        FileOutputStream os = null;
        try {
            os = new FileOutputStream(fileName);
        } catch (FileNotFoundException e) {
            if (Constants.isLoggingEnable) {
                Logger.logError(TAG, e.getLocalizedMessage());

            }

        }

        while (isRecording) {
            // gets the voice output from microphone to byte format
            audioRecord.read(sData, 0, mBufferSize);
            mWaveformView.updateAudioData(sData);

            if (Constants.isLoggingEnable) {
                Logger.logError(TAG, "Recording is working");

            }


            try {
                // // writes the data to file from buffer
                // // stores the voice buffer
                byte bData[] = short2byte(sData);
                os.write(bData);
            } catch (IOException e) {
                if (Constants.isLoggingEnable) {
                    Logger.logError(TAG, e.getLocalizedMessage());

                }
            }
        }
        try {
            os.close();
        } catch (IOException e) {
            if (Constants.isLoggingEnable) {
                Logger.logError(TAG, e.getLocalizedMessage());

            }
        }

//        copyWaveFile(getTempFilename(), getFilename());
//        deleteTempFile();
    }

private void deleteTempFile() {
    File file = new File(getTempFilename());

    file.delete();
}

将PCM格式转换为WAV数据

private void WriteWaveFileHeader(
        FileOutputStream out, long totalAudioLen,
        long totalDataLen, long longSampleRate, int channels,
        long byteRate) throws IOException {

    byte[] header = new byte[44];

    header[0] = 'R'; // RIFF/WAVE header
    header[1] = 'I';
    header[2] = 'F';
    header[3] = 'F';
    header[4] = (byte) (totalDataLen & 0xff);
    header[5] = (byte) ((totalDataLen >> 8) & 0xff);
    header[6] = (byte) ((totalDataLen >> 16) & 0xff);
    header[7] = (byte) ((totalDataLen >> 24) & 0xff);
    header[8] = 'W';
    header[9] = 'A';
    header[10] = 'V';
    header[11] = 'E';
    header[12] = 'f'; // 'fmt ' chunk
    header[13] = 'm';
    header[14] = 't';
    header[15] = ' ';
    header[16] = 16; // 4 bytes: size of 'fmt ' chunk
    header[17] = 0;
    header[18] = 0;
    header[19] = 0;
    header[20] = 1; // format = 1
    header[21] = 0;
    header[22] = (byte) channels;
    header[23] = 0;
    header[24] = (byte) (longSampleRate & 0xff);
    header[25] = (byte) ((longSampleRate >> 8) & 0xff);
    header[26] = (byte) ((longSampleRate >> 16) & 0xff);
    header[27] = (byte) ((longSampleRate >> 24) & 0xff);
    header[28] = (byte) (byteRate & 0xff);
    header[29] = (byte) ((byteRate >> 8) & 0xff);
    header[30] = (byte) ((byteRate >> 16) & 0xff);
    header[31] = (byte) ((byteRate >> 24) & 0xff);
    header[32] = (byte) (2 * 16 / 8); // block align
    header[33] = 0;
    header[34] = 16; // bits per sample
    header[35] = 0;
    header[36] = 'd';
    header[37] = 'a';
    header[38] = 't';
    header[39] = 'a';
    header[40] = (byte) (totalAudioLen & 0xff);
    header[41] = (byte) ((totalAudioLen >> 8) & 0xff);
    header[42] = (byte) ((totalAudioLen >> 16) & 0xff);
    header[43] = (byte) ((totalAudioLen >> 24) & 0xff);

    out.write(header, 0, 44);
}

private void copyWaveFile(String inFilename, String outFilename) {
    FileInputStream in = null;
    FileOutputStream out = null;
    long totalAudioLen = 0;
    long totalDataLen = totalAudioLen + 36;
    long longSampleRate = SAMPLING_RATE;
    int channels = 2;
    long byteRate = 16 * SAMPLING_RATE * channels / 8;

    byte[] data = new byte[mBufferSize];

    try {
        in = new FileInputStream(inFilename);
        out = new FileOutputStream(outFilename);
        totalAudioLen = in.getChannel().size();
        totalDataLen = totalAudioLen + 36;


        WriteWaveFileHeader(out, totalAudioLen, totalDataLen,
                longSampleRate, channels, byteRate);

        while (in.read(data) != -1) {
            out.write(data);
        }

        in.close();
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
private String getFilename() {
    String filepath = Environment.getExternalStorageDirectory().getPath();
    File file = new File(filepath, AUDIO_RECORDER_FOLDER);

    if (!file.exists()) {
        file.mkdirs();
    }

    return (file.getAbsolutePath() + "/audio.wav");
}

private String getTempFilename() {
    String filepath = Environment.getExternalStorageDirectory().getPath();
    File file = new File(filepath, AUDIO_RECORDER_FOLDER);

    if (!file.exists()) {
        file.mkdirs();
    }

    File tempFile = new File(filepath, AUDIO_RECORDER_TEMP_FILE);

    if (tempFile.exists())
        tempFile.delete();

    return (file.getAbsolutePath() + "/" + AUDIO_RECORDER_TEMP_FILE);
}

我出错的地方?请帮帮我。 谢谢:)

0 个答案:

没有答案