Android sound focus ---- adjust the volume of Music from Music back to Luncher

Source: Internet
Author: User
Tags dtmf tones

Android sound focus ---- adjust the volume of Music from Music back to Luncher

Sound types are defined in the AudioSystem. java file.

 

/* The default audio stream */public static final int STREAM_DEFAULT = -1;/* The audio stream for phone calls */public static final int STREAM_VOICE_CALL = 0;/* The audio stream for system sounds */public static final int STREAM_SYSTEM = 1;/* The audio stream for the phone ring and message alerts */public static final int STREAM_RING = 2;/* The audio stream for music playback */public static final int STREAM_MUSIC = 3;/* The audio stream for alarms */public static final int STREAM_ALARM = 4;/* The audio stream for notifications */public static final int STREAM_NOTIFICATION = 5;/* @hide The audio stream for phone calls when connected on bluetooth */public static final int STREAM_BLUETOOTH_SCO = 6;/* @hide The audio stream for enforced system sounds in certain countries (e.g camera in Japan) */public static final int STREAM_SYSTEM_ENFORCED = 7;/* @hide The audio stream for DTMF tones */public static final int STREAM_DTMF = 8;/* @hide The audio stream for text to speech (TTS) */public static final int STREAM_TTS = 9;
Under normal circumstances, STREAM_MUSIC is used to adjust the volume on the audio and video interfaces, and STREAM_VOICE_CALL is used to return to Luncher, but StreamOverride is delayed. sDelayMs, which can be adjusted immediately after the audio and video interfaces are exited. The size of STREAM_MUSIC is changed ,:

 

 

 /**     * For code clarity for getActiveStreamType(int)     * @param delay_ms max time since last STREAM_MUSIC activity to consider     * @return true if STREAM_MUSIC is active in streams handled by AudioFlinger now or     *     in the last "delay_ms" ms.     */    private boolean isAfMusicActiveRecently(int delay_ms) {        return AudioSystem.isStreamActive(AudioSystem.STREAM_MUSIC, delay_ms)                || AudioSystem.isStreamActiveRemotely(AudioSystem.STREAM_MUSIC, delay_ms);    }    private int getActiveStreamType(int suggestedStreamType) {        switch (mPlatformType) {        case PLATFORM_VOICE:            if (isInCommunication()) {                if (AudioSystem.getForceUse(AudioSystem.FOR_COMMUNICATION)                        == AudioSystem.FORCE_BT_SCO) {                    // Log.v(TAG, "getActiveStreamType: Forcing STREAM_BLUETOOTH_SCO...");                    return AudioSystem.STREAM_BLUETOOTH_SCO;                } else {                    // Log.v(TAG, "getActiveStreamType: Forcing STREAM_VOICE_CALL...");                    return AudioSystem.STREAM_VOICE_CALL;                }            } else if (suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) {                if (isAfMusicActiveRecently(StreamOverride.sDelayMs)) {                    if (DEBUG_VOL)                        Log.v(TAG, "getActiveStreamType: Forcing STREAM_MUSIC stream active");                    return AudioSystem.STREAM_MUSIC;                    } else {                        if (DEBUG_VOL)                            Log.v(TAG, "getActiveStreamType: Forcing STREAM_RING b/c default");                        return AudioSystem.STREAM_RING;                }            } else if (isAfMusicActiveRecently(0)) {                if (DEBUG_VOL)                    Log.v(TAG, "getActiveStreamType: Forcing STREAM_MUSIC stream active");                return AudioSystem.STREAM_MUSIC;            }            break;        case PLATFORM_TELEVISION:            if (suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) {                    // TV always defaults to STREAM_MUSIC                    return AudioSystem.STREAM_MUSIC;            }            break;        default:            if (isInCommunication()) {                if (AudioSystem.getForceUse(AudioSystem.FOR_COMMUNICATION)                        == AudioSystem.FORCE_BT_SCO) {                    if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_BLUETOOTH_SCO");                    return AudioSystem.STREAM_BLUETOOTH_SCO;                } else {                    if (DEBUG_VOL)  Log.v(TAG, "getActiveStreamType: Forcing STREAM_VOICE_CALL");                    return AudioSystem.STREAM_VOICE_CALL;                }            } else if (AudioSystem.isStreamActive(AudioSystem.STREAM_NOTIFICATION,                    StreamOverride.sDelayMs) ||                    AudioSystem.isStreamActive(AudioSystem.STREAM_RING,                            StreamOverride.sDelayMs)) {                if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_NOTIFICATION");                return AudioSystem.STREAM_NOTIFICATION;            } else if (suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) {                if (isAfMusicActiveRecently(StreamOverride.sDelayMs)) {                    if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: forcing STREAM_MUSIC");                    return AudioSystem.STREAM_MUSIC;                } else {                    if (DEBUG_VOL) Log.v(TAG,                            "getActiveStreamType: using STREAM_NOTIFICATION as default");                    return AudioSystem.STREAM_NOTIFICATION;                }            }            break;        }        if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Returning suggested type "                + suggestedStreamType);        return suggestedStreamType;    }
Change isAfMusicActiveRecently (StreamOverride. sDelayMs) to isAfMusicActiveRecently (0) or a small value.


 

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.