O'Reilly logo

Programming Android by Zigurd Mednieks, G. Blake Meike, Masumi Nakamura, Laird Dornin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Recording Audio and Video

The standard class that supports recording is the MediaRecorder. Much like the MediaPlayer, it passes through various states during its life cycle. The states are as follows (for more details, view the state diagram provided by the Developers site, at http://developer.android.com/reference/android/media/MediaRecorder.html):

Initialize

The MediaRecorder class is instantiated.

Initialized

The MediaRecorder is ready to be used.

DataSource configured

The media source (where the output will be placed) is configured.

Prepared

The MediaRecorder is prepared to record.

Recording

Recording is underway.

Released

All resources are released.

To utilize the MediaRecorder, some permissions may need to be set in the manifest:

  • To enable video recording, enable RECORD_VIDEO and the CAMERA:

    <uses-permission android:name="android.permission.RECORD_VIDEO"/>
    <uses-permission android:name="android.permission.CAMERA"/>
  • To record audio, enable RECORD_AUDIO:

    <uses-permission android:name="android.permission.RECORD_AUDIO"/>

Audio Recording

There are three methods to record audio. The MediaRecorder is the standard method; using an Intent is the simplest method; and the AudioRecorder can be used to record directly from hardware buffers.

MediaRecorder audio recording

First, initialize the MediaRecorder. Then set the data source information (the audio input source, the output format, the encoding type, where the file is to be recorded to, etc.). Starting with version 8, you can set the bit rate and sampling ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required