Computer Laboratory

Course material 2010–11

Programming for Mobiles

Practical 3: Tricorder

worksheet PDF


  • Don't forget to request the relevant permissions in your Android manifest.xml file
  • Notes on audio recording [below]
  • Notes on recovering device orientation [below]

Audio recording

Use the AudioRecord class to record sound.
  1. You need to choose:
    1. Sample rate (number of audio samples to record per second)
    2. Channel configuration (stereo or mono) defined as constants in the AudioFormat class
    3. Sample encoding (number of bits per sample, etc.) defined as constants in AudioFormat
  2. Use AudioRecord.getMinBufferSize to determine if your chosen combination is valid. It returns -1 if its not supported by your hardware. You might want to use a bigger buffer than the one returned - it just tells you the smallest possible.
  3. Now you can create your AudioRecord instance
  4. Check that it is initialised properly and then call startRecording()
  5. You can now read from it just like you read from a InputStream
  6. Don't forget to call stop() and release() when you are done with the device

Device orientation

You should collect a sensor reading from the accelerometers (Sensor.TYPE_ACCELEROMETER) and from the magnetic field sensor (Sensor.TYPE_MAGNETIC_FIELD). Then pass these values to SensorManager.getRotationMatrix - this method combines the two sensor readings to give you a rotation matrix from the device coordinate system to the global coordinate system. You can now pass this rotation matrix to SensorManager.getOrientation which projects the matrix to compute azimuth, pitch and roll.