Android聲音焦點----從音樂回到Luncher調節音量顯示的是Music的音量
阿新 • • 發佈:2019-01-29
聲音的型別有:定義在AudioSystem.java檔案中
正常情況下在音、視訊介面調節音量調節的Stream是STREAM_MUSIC,回到Luncher調節的是STREAM_VOICE_CALL,但這個切換有點延遲StreamOverride.sDelayMs,導致退出音、視訊介面後馬上調節,改變的還是STREAM_MUSIC的大小,如圖所示:/* 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;
可以修改/frameworks/base/media/java/android/media/AudioService.java檔案
把isAfMusicActiveRecently(StreamOverride.sDelayMs) 改為: isAfMusicActiveRecently(0) 或一個小的值。/** * 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; }