Androidプログラマへの道 〜 Moonlight 明日香 〜 - データストリームとして音声データを読み込む
録音データをリアルタイムで操作するような場合, データストリームとして音声データを読み込む必要がある.
データストリームとして読み込むには, オーディオレコード(AudioRecord)クラスを使う.

音声データの読み込み

  • Recorder02.java
    • AudioRecord#getMinBufferSizeメソッドに録音条件を設定し, バッファサイズを計算する.
    • AudioRecordコンストラクタに録音条件を設定し, AudioRecordを生成する.
    • AudioRecord#startRecordingメソッドで, 録音を開始する.
    • AudioRecord#readメソッドで, 音声データを読み込む.
    • AudioRecord#stopメソッドで, 録音を停止する.
    • AudioRecord#releaseメソッドで, オブジェクトを解放する.
package com.moonlight_aska.android.recorder02;

import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class Recorder02 extends Activity implements View.OnClickListener {
  final static int SAMPLING_RATE = 11025;
  AudioRecord audioRec = null;
  Button btn = null;
  boolean bIsRecording = false;
  int bufSize;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btn = (Button)findViewById(R.id.button_id);
    btn.setOnClickListener(this);
    // バッファサイズの計算
    bufSize = AudioRecord.getMinBufferSize(
           SAMPLING_RATE,
           AudioFormat.CHANNEL_CONFIGURATION_MONO,
           AudioFormat.ENCODING_PCM_16BIT) * 2;
    // AudioRecordの作成
    audioRec = new AudioRecord(
           MediaRecorder.AudioSource.MIC,
           SAMPLING_RATE,
           AudioFormat.CHANNEL_CONFIGURATION_MONO,
           AudioFormat.ENCODING_PCM_16BIT,
           bufSize);
  }

  @Override
  public void onClick(View v) {
    // TODO Auto-generated method stub
    if (v == btn) {
      if (bIsRecording) {
        btn.setText(R.string.start_label);
        bIsRecording = false;
      }
      else {
        // 録音開始
        Log.v("AudioRecord", "startRecording");
        audioRec.startRecording();
        bIsRecording = true;
        // 録音スレッド
        new Thread(new Runnable() {
          @Override
          public void run() {
            byte buf[] = new byte[bufSize];
            // TODO Auto-generated method stub
            while (bIsRecording) {
              // 録音データ読み込み
              audioRec.read(buf, 0, buf.length);
              Log.v("AudioRecord", "read " + buf.length + " bytes");
            }
            // 録音停止
            Log.v("AudioRecord", "stop");
            audioRec.stop();
          }
        }).start();
        btn.setText(R.string.stop_label);
      }
    }
  }

  @Override
  protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    audioRec.release();
  }
}
  • AndroidManifest.xml
    • 音声の録音の権限を追加する.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.moonlight_aska.android.recorder02"
  android:versionCode="1"
  android:versionName="1.0" >

  <uses-sdk android:minSdkVersion="8" />
  <uses-permission android:name="android.permission.RECORD_AUDIO"/>
  <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
      android:name=".Recorder02"
      android:label="@string/app_name" >
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
  </application>
</manifest>