11

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?

3 Answers 3

12

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.

1
  • @KeithYokoma. Is there any possibility of reducing stream volume by other stream. I observed STREAM_VOICE_CALL is reducing the volume of STREAM_MUSIC? Any guess how it is happening? Commented May 30, 2015 at 11:42
4

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.

4
  • Do you think a wrong mode in either audiotrack or audiorecord when using audiorecord and initializing audiotrack or another audiorecord will mute or silently disable the mic? Commented Jun 13, 2017 at 6:08
  • 1
    Sorry, haven't made enough experiments with the audio recording path. It is likely to be the same. Still, there are apps that record phone calls, there are apps that show sound level at any time, so I guess it's more flexible on microphone side. Microphone is input, so there is no real conflict, I suppose. The same samples just go to app A, B, or C. Maybe the volume and stuff but that's a minor problem.
    – Meymann
    Commented Jun 14, 2017 at 7:45
  • Ok! I understand and get the logic. Will take a look at a call recorder and investigate! Thank you for your time :) Commented Jun 15, 2017 at 20:48
  • I added some fragments of code to the answer above to show some of the effects of using different streams (from the code Meymann mentioned.
    – Ribo
    Commented Jul 14, 2020 at 22:55
2
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

2
  • 6
    It is pretty clear what purpose they serve. My question is about how they differ at low-level. It just can't be 10 identical streams. Commented Jul 9, 2014 at 14:51
  • 1
    It's clear that you can read the name of the identifier... What is the behaviour of each stream? Which is prior? Which is played in what audio mode? Which ducks when the what stream plays? Which is subject to some silencing mechanism?
    – Meymann
    Commented Oct 28, 2016 at 6:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.