Kirill Boyarshinov
Kirill Boyarshinov

Reputation: 6173

What is the difference between AudioManager's stream types at low level?

There are several stream types in AudioManager. How did they differ at low level? Could it be that usage of e.g. AudioManager.STREAM_MUSIC blocks input microphone stream? Or something else?

Upvotes: 12

Views: 15549

Answers (3)

Meymann
Meymann

Reputation: 2532

As usual, whatever you Google won't document can (sometimes) be understood from the code

https://android.googlesource.com/platform/frameworks/base/+/00ccd5d026fcd0e4b9d27dc5a9ffa13ca0408449/media/java/android/media/AudioService.java

As for blocking, it's not really that streams block one another, it's just that modes block things. MODE_IN_COMMUNICATION blocks most of the streams.

Ducking: A common and "polite" behaviour when playing a long running stream (e.g. MUSIC) is to listen to an audio focus callback and lower the volume of your stream manually upon "can duck" event. The volume should return to its previous level when the focus returns to your stream.

The stream type affects the volume that streams contributes to the sum of all sounds at a given time:

   /** @hide Maximum volume index values for audio streams */
private int[] MAX_STREAM_VOLUME = new int[] {
    5,  // STREAM_VOICE_CALL
    7,  // STREAM_SYSTEM
    7,  // STREAM_RING
    15, // STREAM_MUSIC
    7,  // STREAM_ALARM
    7,  // STREAM_NOTIFICATION
    15, // STREAM_BLUETOOTH_SCO
    7,  // STREAM_SYSTEM_ENFORCED
    15, // STREAM_DTMF
    15  // STREAM_TTS

This array from AudioService.java shows the default maximum volume for a given stream. Other code:

    private void readPersistedSettings() {
    final ContentResolver cr = mContentResolver;
    mRingerMode = System.getInt(cr, System.MODE_RINGER, AudioManager.RINGER_MODE_NORMAL);
    mVibrateSetting = System.getInt(cr, System.VIBRATE_ON, 0);
    mRingerModeAffectedStreams = Settings.System.getInt(cr,
            Settings.System.MODE_RINGER_STREAMS_AFFECTED,
            ((1 << AudioSystem.STREAM_RING)|(1 << AudioSystem.STREAM_NOTIFICATION)|
             (1 << AudioSystem.STREAM_SYSTEM)|(1 << AudioSystem.STREAM_SYSTEM_ENFORCED)));
    mMuteAffectedStreams = System.getInt(cr,
            System.MUTE_STREAMS_AFFECTED,
            ((1 << AudioSystem.STREAM_MUSIC)|(1 << AudioSystem.STREAM_RING)|(1 << AudioSystem.STREAM_SYSTEM)));
    mNotificationsUseRingVolume = System.getInt(cr,
            Settings.System.NOTIFICATIONS_USE_RING_VOLUME, 1);

seems to indicate what streams are muted or reduced in volume when the phone starts 'ringing' or when a call is in progress.

Upvotes: 4

KeithYokoma
KeithYokoma

Reputation: 383

One most significant difference is a volume control.
As the document says, we can set a volume level or adjust a volume for each streams respectively.
And we can control "Audio Focus" that let other apps stop playback or lower the volumes so we can play a sound clearly on a specific stream, or the sound will not block the other high-prioritized sound like phone ring, navigation voice feedback or something like that.

With multiple apps potentially playing audio it's important to think about how they should interact. To avoid every music app playing at the same time, Android uses audio focus to moderate audio playback—only apps that hold the audio focus should play audio.

Other unmentioned thing is which audio device will produce a sound.
Using STREAM_MUSIC the sound will be produced through one audio device(phone speaker, earphone, bluetooth speaker or something else) connected to the phone.
Using STREAM_RING the sound will be produced through all audio device connected to the phone. This behaviour might be differed for each devices.

Upvotes: 12

acid_srvnn
acid_srvnn

Reputation: 693

STREAM_ALARM        The audio stream for alarms
STREAM_DTMF         The audio stream for DTMF Tones
STREAM_MUSIC        The audio stream for music playback
STREAM_NOTIFICATION The audio stream for notifications
STREAM_RING         The audio stream for the phone ring
STREAM_SYSTEM       The audio stream for system sounds
STREAM_VOICE_CALL   The audio stream for phone calls

from http://developer.android.com/reference/android/media/AudioManager.html

Upvotes: 2

Related Questions