现在的位置: 首页 > 综合 > 正文

android 合并录音文件

2018年02月10日 ⁄ 综合 ⁄ 共 1889字 ⁄ 字号 评论关闭

/**
     * @param audio files list
     */
    public void getInputCollection(List list) {
        long dateTaken = System.currentTimeMillis();
        String strTempFile = Long.toString(dateTaken);
        mRecordAudioFile = new File(mPath, strTempFile + ".amr");

        FileOutputStream fileOutputStream = null;

        if (!mRecordAudioFile.exists()) {
            try {
                mRecordAudioFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            fileOutputStream = new FileOutputStream(mRecordAudioFile);

        } catch (IOException e) {
            e.printStackTrace();
        }

        for (int i = 0; i < list.size(); i++) {
            File tempfile = new File((String) list.get(i));
            try {
                FileInputStream fileInputStream = new FileInputStream(tempfile);
                byte[] myByte = new byte[fileInputStream.available()];
                // file length
                int length = myByte.length;

                // first audio file
                if (i == 0) {
                    while (fileInputStream.read(myByte) != -1) {
                        fileOutputStream.write(myByte, 0, length);
                    }
                }

                // delete file head information
                else {
                    while (fileInputStream.read(myByte) != -1) {

                        fileOutputStream.write(myByte, 6, length - 6);
                    }
                }

                fileOutputStream.flush();
                fileInputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

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

    }

合并之前设置MediaRecorder的输出格式和encoder一定要指定对,否则合并后的录音文件有问题

            mMediaRecorder = new MediaRecorder();
            mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
            mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

抱歉!评论已关闭.