Skip to content

Most visited

Recently visited



The Android audio subsystem enables apps to write audio data for playback and record audio from input sources. Using audio drivers, your apps can register new audio input/output devices connected over I2S or other Peripheral I/O interfaces. Apps can access your audio devices using the standard media framework APIs, such as AudioTrack and AudioRecord.

Implementing the driver

To define a new audio device, extend the AudioInputDriver or AudioOutputDriver class and implement the appropriate read() or write() method. The media framework calls these methods as needed to request recorded audio or supply data for playback.

Your driver should implement the onStandbyChanged() method if the audio hardware is able to react to Android going into and out of standby mode. This might include terminating an audio stream in progress or putting the hardware into a low power state.

The following example implements an AudioOutputDriver that sends audio from the framework to an I2sDevice for playback:

public class PlaybackDriver extends AudioOutputDriver {

    private I2sDevice mOutputDevice;

    public PlaybackDriver(I2sDevice output) {
        mOutputDevice = output;

    public void onStandbyChanged(boolean inStandby) {

    public int write(ByteBuffer buffer, int count) {
        try {
            return mOutputDevice.write(buffer, count);
        } catch (IOException e) {
            Log.w(TAG, "Unable to write audio buffer", e);
            return -1;

Registering a new device

Bind your audio device to the framework by registering it with the UserDriverManager. Supply the following parameters along with the device registration:

  1. Audio Format - Audio data parameters. Including sample rate, encoding, and channel count. Provided as an AudioFormat.
  2. Device Type - AudioDeviceInfo constant that best defines the device class.
  3. Buffer Size - Maximum amount of data the system can exchange with your audio device in one transaction. The buffer size should be an even multiple of the audio data defined by the provided format. If you are unsure of a good buffer size, use getMinBufferSize() as a starting point.

To detach the device from the framework, unregister the driver.

public class AudioDriverService extends Service {

    private static final AudioFormat AUDIO_FORMAT_STEREO =
            new AudioFormat.Builder()
            .setChannelMask(AudioFormat.CHANNEL_IN_STEREO) // 2 channels
            .setEncoding(AudioFormat.ENCODING_PCM_16BIT)   // 16-bit samples
            .setSampleRate(44100)                          // 44.1kHz

    private PlaybackDriver mPlaybackDriver;

    public void onCreate() {

        mPlaybackDriver = new PlaybackDriver();
        UserDriverManager manager = UserDriverManager.getManager();

        // Generate a sane default playback buffer size
        int bufferSize = AudioTrack.getMinBufferSize(
        // Register the new driver with the framework

    public void onDestroy() {

        UserDriverManager manager = UserDriverManager.getManager();
        // Unregister the driver when finished
This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields


Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.