2011年5月27日 星期五

AudioRecorder功能

AudioRecorder功能
AudioRecorder這個物件可以設定錄音的品質(頻率、取樣數、聲道)
但是錄出來的資料是原始音頻檔(PCM)
後續還要加Header將其轉換成可播放的格式,例如WAV
下面是使用AudioRecorder做錄音的範例
在範例中,AudioRecorder需要放在另一個線程(Thread)執行
    // 按鈕操作
    public void onClick(View v) {
        // Press start record
        if (v.getId() == R.id.recStart) {
        isRecording = true;
            thread = new Thread(new Runnable() {
                public void run() {
                    record();
                }
            });
            thread.start();

        }
        // Press stop record        else if (v.getId() == R.id.recStop) {
        isRecording = false;
        // 可以用"thread.join()"讓thread進入blocked狀態,目前直接在結束的地方寫"thread.stop();"
        // try {
        //     thread.join();
        // } catch (InterruptedExpection e) { }
        }
    }

    private void record() {
        pcmFile = new File(myDataPath.getAbsolutePath() + "/record.pcm");
        // Delete any previous recording.
        if (pcmFile.exists())
            pcmFile.delete();
        // Create the new file.
        try {
            pcmFile.createNewFile();
        } catch (IOException e) {
            throw new IllegalStateException("Failed to create " + pcmFile.toString());
        }
        // Start record pcm data        try {
            // Create a DataOuputStream to write the audio data into the saved file.            OutputStream os = new FileOutputStream(pcmFile);
            BufferedOutputStream bos = new BufferedOutputStream(os);
            DataOutputStream dos = new DataOutputStream(bos);
  
            // Create a new AudioRecord object to record the audio.
            int bufferSize =
            AudioRecord.getMinBufferSize(
                sampleRateInHz,
                AudioFormat.CHANNEL_IN_MONO,
                AudioFormat.ENCODING_PCM_16BIT);
  
            AudioRecord audioRecord =
                new AudioRecord(
                    MediaRecorder.AudioSource.MIC,
                    sampleRateInHz,
                    AudioFormat.CHANNEL_IN_MONO,
                    AudioFormat.ENCODING_PCM_16BIT,
                    bufferSize);
            byte[] buffer = new byte[bufferSize];
            audioRecord.startRecording();
            while (isRecording) {
                int bufferReadResult = audioRecord.read(buffer, 0, bufferSize);
                for (int i = 0; i < bufferReadResult; i++)
                    dos.write(buffer[i]);
            }
  
            // filler, data size need can %8 = 0            if (dos.size()%8  != 0) {
                int filler = 0;
                filler = 8 - (dos.size()%8);
                for (int i = 0 ; i < filler ; i++) {
                    dos.writeByte(0);
                }
            }
  
            // stop record and close the file.            audioRecord.stop();
            dos.close();
        } catch (Throwable t) { }
        // stop thread, this method may be not great, we can use the "thread.join( )" method
        thread.stop();
    }

沒有留言:

張貼留言