Android音頻開發(3):使用AudioRecord實現錄音的暫停和恢復

Android 音頻開發 目錄

  1. Android音頻開發(1):音頻相關知識
  2. Android音頻開發(2):使用AudioRecord錄製pcm格式音頻
  3. Android音頻開發(3):使用AudioRecord實現錄音的暫停和恢復
  4. Android音頻開發(4):PCM轉WAV格式音頻
  5. Android音頻開發(5):Mp3的錄製 - 編譯Lame源碼
  6. Android音頻開發(6):Mp3的錄製 - 使用Lame實時錄製MP3格式音頻
  7. Android音頻開發(7):音樂可視化-FFT頻譜圖

項目地址

https://github.com/zhaolewei/ZlwAudioRecorder


上一篇主要寫了AudioRecord實現音頻錄製的開始和停止,AudioRecord並沒有暫停和恢復播放功能的API,所以需要手動實現

一、解決辦法

思路很簡單,現在可以實現音頻的文件錄製和停止,並生成pcm文件,那麼暫停時將這次文件先保存下來,恢復播放後開始新一輪的錄製,那麼最後會生成多個pcm音頻,再將這些pcm文件進行合併,這樣就實現了暫停/恢復的功能了。

二、實現

  • 實現的重點在於如何控制錄音的狀態

/**
 * @author zhaolewei on 2018/7/10.
 */
public class RecordHelper {
    private volatile RecordState state = RecordState.IDLE;
    private AudioRecordThread audioRecordThread;

    private File recordFile = null;
    private File tmpFile = null;
    private List<File> files = new ArrayList<>();

    public void start(String filePath, RecordConfig config) {
        this.currentConfig = config;
        if (state != RecordState.IDLE) {
            Logger.e(TAG, "狀態異常當前狀態: %s", state.name());
            return;
        }
        recordFile = new File(filePath);
        String tempFilePath = getTempFilePath();
        Logger.i(TAG, "tmpPCM File: %s", tempFilePath);
        tmpFile = new File(tempFilePath);
        audioRecordThread = new AudioRecordThread();
        audioRecordThread.start();
    }

    public void stop() {
        if (state == RecordState.IDLE) {
            Logger.e(TAG, "狀態異常當前狀態: %s", state.name());
            return;
        }

        //若在暫停中直接停止,則直接合並文件即可
        if (state == RecordState.PAUSE) {
            makeFile();
            state = RecordState.IDLE;
        } else {
            state = RecordState.STOP;
        }
    }

    public void pause() {
        if (state != RecordState.RECORDING) {
            Logger.e(TAG, "狀態異常當前狀態: %s", state.name());
            return;
        }
        state = RecordState.PAUSE;
    }

    public void resume() {
        if (state != RecordState.PAUSE) {
            Logger.e(TAG, "狀態異常當前狀態: %s", state.name());
            return;
        }
        String tempFilePath = getTempFilePath();
        Logger.i(TAG, "tmpPCM File: %s", tempFilePath);
        tmpFile = new File(tempFilePath);
        audioRecordThread = new AudioRecordThread();
        audioRecordThread.start();
    }

    private class AudioRecordThread extends Thread {
        private AudioRecord audioRecord;
        private int bufferSize;

        AudioRecordThread() {
            bufferSize = AudioRecord.getMinBufferSize(currentConfig.getFrequency(),
                    currentConfig.getChannel(), currentConfig.getEncoding()) * RECORD_AUDIO_BUFFER_TIMES;
            audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, currentConfig.getFrequency(),
                    currentConfig.getChannel(), currentConfig.getEncoding(), bufferSize);
        }

        @Override
        public void run() {
            super.run();
            state = RecordState.RECORDING;
            notifyState();
            Logger.d(TAG, "開始錄製");
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(tmpFile);
                audioRecord.startRecording();
                byte[] byteBuffer = new byte[bufferSize];

                while (state == RecordState.RECORDING) {
                        int end = audioRecord.read(byteBuffer, 0, byteBuffer.length);
                    fos.write(byteBuffer, 0, end);
                    fos.flush();
                }
                audioRecord.stop();
                //1. 將本次錄音的文件暫存下來,用於合併
                files.add(tmpFile);
                //2. 再此判斷終止循環的狀態是暫停還是停止,並做相應處理
                if (state == RecordState.STOP) {
                    makeFile();
                } else {
                    Logger.i(TAG, "暫停!");
                }
            } catch (Exception e) {
                Logger.e(e, TAG, e.getMessage());
            } finally {
                try {
                    if (fos != null) {
                        fos.close();
                    }
                } catch (IOException e) {
                    Logger.e(e, TAG, e.getMessage());
                }
            }
            if (state != RecordState.PAUSE) {
                state = RecordState.IDLE;
                notifyState();
                Logger.d(TAG, "錄音結束");
            }
        }
    }

    private void makeFile() {
        //合併文件
        boolean mergeSuccess = mergePcmFiles(recordFile, files);

        //TODO:轉換wav
        Logger.i(TAG, "錄音完成! path: %s ; 大小:%s", recordFile.getAbsoluteFile(), recordFile.length());
    }

    /**
     * 合併Pcm文件
     *
     * @param recordFile 輸出文件
     * @param files      多個文件源
     * @return 是否成功
     */
    private boolean mergePcmFiles(File recordFile, List<File> files) {
        if (recordFile == null || files == null || files.size() <= 0) {
            return false;
        }

        FileOutputStream fos = null;
        BufferedOutputStream outputStream = null;
        byte[] buffer = new byte[1024];
        try {
            fos = new FileOutputStream(recordFile);
            outputStream = new BufferedOutputStream(fos);

            for (int i = 0; i < files.size(); i++) {
                BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(files.get(i)));
                int readCount;
                while ((readCount = inputStream.read(buffer)) > 0) {
                    outputStream.write(buffer, 0, readCount);
                }
                inputStream.close();
            }
        } catch (Exception e) {
            Logger.e(e, TAG, e.getMessage());
            return false;
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //3. 合併後記得刪除緩存文件並清除list
        for (int i = 0; i < files.size(); i++) {
            files.get(i).delete();
        }
        files.clear();
        return true;
    }

}

其他

  • 在此後如若需要添加錄音狀態回調,記得使用Handler做好線程切換。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章