A little analysis of Android framework audio management Audiomanager

Source: Internet
Author: User
Tags dtmf tones

In the near future to match some third-party applications to the system, but the audio part of the logic is still a bug. So focus on the next part of the audio management Code, write part of the current understanding of the part.

First of all, we must first explain why the current work, the cause is that the system has to call the sound of the application, so at the same time to run another need to use the sound of the application needs to have a priority to constrain the volume of these sounds and sequencing. Therefore, this part of the audio management needs to be logically handled, so that it can be coordinated to emit sound rather than produce sound overlap and confusion.

Review the framework's code, the SDK documentation, and finally locate the source in the following path

/frameworks/base/media/java/android/media/audiomanager.java
/frameworks/base/media/java/android/media/audioservice.java

Because the first contact a lot of just learn the framework when the next did not carefully read the source code, this time need to look carefully.

Data learned that in Audiomanager, the previous version of Android2.2 is not a mechanism for multiple programs to compete for audio resources at the same time, so it is possible that before that version the sound management should be limited to the volume size of the adjustment, and there is no competition exclusive priority of the sub-level.

So start with a simple sound adjustment to analyze.

Let's look at the definitions in some of the code:

    /** the audio stream for phone calls */public static final int stream_voice_call = Audiosystem.stream_voice_call;    /** the audio stream for system sounds */public static final int stream_system = Audiosystem.stream_system;    /** the audio stream for the phone ring */public static final int stream_ring = audiosystem.stream_ring;    /** the audio stream for music playback */public static final int stream_music = Audiosystem.stream_music;    /** the audio stream for alarms */public static final int stream_alarm = Audiosystem.stream_alarm; /** the audio stream for notifications */public static final int stream_notification = Audiosystem.stream_notification    ; /** @hide the audio stream for phone calls when connected to Bluetooth */public static final int Stream_bluetooth_sco    = Audiosystem.stream_bluetooth_sco;  /** @hide the audio stream for enforced system sounds in certain countries (e.g camera in Japan) */public static final int Stream_system_enforced = audiosystem.stream_system_enforced;    /** the audio stream for DTMF Tones */public static final int STREAM_DTMF = AUDIOSYSTEM.STREAM_DTMF;    /** @hide the audio stream for text to Speech (TTS) */public static final int stream_tts = Audiosystem.stream_tts; /** number of audio streams */
And look at the code that adjusts the volume.

    Private Audiomanager Maudiomanager;    Maudiomanager = (Audiomanager) mcontext.getsystemservice (context.audio_service);    Maudiomanager.adjuststreamvolume (Audiomanager.stream_music, Audiomanager.adjust_raise, AudioManager.FX_FOCUS_ NAVIGATION_UP);
The above part of the code is to adjust the volume of music part of the code, the effect is to make a sound increase in step one, and at this time the call will appear the system volume bar changes, this operation will be analyzed later.

Now focus on function adjuststreamvolume (int streamtype,int direction,int flags)

Parameter streamtype: This part of the parameter is the first definition, there is stream_voice_call--call, Stream_system--system volume, stream_ring--ringtones

Stream_music--Music, stream_alarm--Alarm clock, stream_notification--notice, Stream_bluetooth_sco--Bluetooth call,

The latter three seem to be infrequently used, not very clear.

Parameter direction: This part of the parameter definition has adjust_raise--increase, adjust_lower--decrease, adjust_same--the same, for adjust_same this parameter, can achieve a small purpose is After the call, the system volume bar can be ejected without changing the volume.

Parameter flags: This section is a flag bit, optional. The code is used to focus on the shift of the flag bit, specifically what special purpose follow-up.

The other part of the sound adjustment function has setstreamvolume (int streamtype, int index, int flags), this function is different from the previous function is the last function is adjusted by stepping, this function can directly assign a value to the second parameter, Specifies the volume value to set. However, it is necessary to determine the range of parameters to avoid exceeding the range of valid values.

Other related functions:

Getmastermaxvolume ()--Gets the maximum system master Volume

Getmastervolume ()--Get the current volume value of the system

Getlastaudiblemastervolume ()--Get the effective volume before the system is muted

GetMode ()--Get current audio mode, such as Mode_invalid--invalid, mode_current--current, normal--normal, RINGTONE--ringtones, Mode_in_call--Pick up the phone, Mode_in_communication--Call.

Getringmode ()--Get the ring way, such as ringer_mode_silent-mute, ringer_mode_vibrate--vibration, ringer_mode_normal--Normal, ringer_mode_max-- Normal

Getstreamvolume (int streamtype)--Gets the volume value in the current mode

Getstreammaxvolume (int streamtype)--Gets the current maximum ringtone volume value

The above section is a partial resolution of volume adjustment in Audiomanager. Subsequent continuation of the function parsing concerning the adjustment involving another part.

A little analysis of Android framework audio management Audiomanager

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.