Skip to content

Most visited

Recently visited

navigation

Audio

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;
    }

    @Override
    public void onStandbyChanged(boolean inStandby) {
        ...
    }

    @Override
    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
            .build();

    private PlaybackDriver mPlaybackDriver;

    @Override
    public void onCreate() {
        super.onCreate();

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

        // Generate a sane default playback buffer size
        int bufferSize = AudioTrack.getMinBufferSize(
            AUDIO_FORMAT_STEREO.getSampleRate(),
            AUDIO_FORMAT_STEREO.getChannelMask(),
            AUDIO_FORMAT_STEREO.getEncoding());
        // Register the new driver with the framework
        manager.registerAudioOutputDriver(mPlaybackDriver,
                AUDIO_FORMAT_STEREO,
                AudioDeviceInfo.TYPE_BUILTIN_SPEAKER,
                bufferSize);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

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

Hooray!

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.