android音量键调节听筒音量的大小

您所在的位置:网站首页 s9音量键无效 android音量键调节听筒音量的大小

android音量键调节听筒音量的大小

2023-08-16 18:01| 来源: 网络整理| 查看: 265

android音量键调节听筒音量的大小

最近发现微信的语音功能可以在听筒和喇叭间互相切换并且可以使用音量按键进行调节,之前在项目开发中只用到音频的多媒体类型播放音频文件但没用到听筒,所以就写了个demo使用听筒播放音频文件,用到的代码如下:

audioManager.setSpeakerphoneOn(false); audioManager.setMode(AudioManager.MODE_IN_CALL); mediaPlayer = new MediaPlayer(); try { mediaPlayer.setDataSource(this, Uri.fromFile(new File(getExternalCacheDir(), "tada.ogg"))); } catch (IOException e) { e.printStackTrace(); } mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL); mediaPlayer.setLooping(true); try { mediaPlayer.prepare(); } catch (IOException e) { e.printStackTrace(); } mediaPlayer.start();

使用以上方式播放时,音量键调节始终调节的是“铃声和通知“的音量大小,对比打电话时可以调节听筒大小感觉很疑惑 ,追踪源码得知,处理音量按键的最终现实在AudioService.java文件中。 定位到函数

public void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags, String callingPackage) { if (DEBUG_VOL) Log.d(TAG, "adjustSuggestedStreamVolume() stream="+suggestedStreamType); int streamType; if (mVolumeControlStream != -1) { streamType = mVolumeControlStream; } else { streamType = getActiveStreamType(suggestedStreamType); } // Play sounds on STREAM_RING only and if lock screen is not on. if ((streamType != STREAM_REMOTE_MUSIC) && (flags & AudioManager.FLAG_PLAY_SOUND) != 0 && ((mStreamVolumeAlias[streamType] != AudioSystem.STREAM_RING) || (mKeyguardManager != null && mKeyguardManager.isKeyguardLocked()))) { flags &= ~AudioManager.FLAG_PLAY_SOUND; } if (streamType == STREAM_REMOTE_MUSIC) { // don't play sounds for remote flags &= ~(AudioManager.FLAG_PLAY_SOUND|AudioManager.FLAG_FIXED_VOLUME); //if (DEBUG_VOL) Log.i(TAG, "Need to adjust remote volume: calling adjustRemoteVolume()"); mMediaFocusControl.adjustRemoteVolume(AudioSystem.STREAM_MUSIC, direction, flags); } else { adjustStreamVolume(streamType, direction, flags, callingPackage); } } private int getActiveStreamType(int suggestedStreamType) { if (mVoiceCapable) { 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(DEFAULT_STREAM_TYPE_OVERRIDE_DELAY_MS)) { if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_MUSIC stream active"); return AudioSystem.STREAM_MUSIC; } else if (mMediaFocusControl.checkUpdateRemoteStateIfActive(AudioSystem.STREAM_MUSIC)) { if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_REMOTE_MUSIC"); return STREAM_REMOTE_MUSIC; /// M: Add for FM volume adjust @ { } else if (isFmOn()) { if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_FM..."); return AudioSystem.STREAM_FM; /// @} } 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; /// M: Add for FM volume adjust @ { } else if (isFmOn()) { if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_FM..."); return AudioSystem.STREAM_FM; /// @} } else { if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Returning suggested type " + suggestedStreamType); return suggestedStreamType; } } else { 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, DEFAULT_STREAM_TYPE_OVERRIDE_DELAY_MS) || AudioSystem.isStreamActive(AudioSystem.STREAM_RING, DEFAULT_STREAM_TYPE_OVERRIDE_DELAY_MS)) { if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_NOTIFICATION"); return AudioSystem.STREAM_NOTIFICATION; } else if (suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { if (isAfMusicActiveRecently(DEFAULT_STREAM_TYPE_OVERRIDE_DELAY_MS)) { if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: forcing STREAM_MUSIC"); return AudioSystem.STREAM_MUSIC; } else if (mMediaFocusControl.checkUpdateRemoteStateIfActive(AudioSystem.STREAM_MUSIC)) { if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_REMOTE_MUSIC"); return STREAM_REMOTE_MUSIC; /// M: Add for FM volume adjust @ { } else if (isFmOn()) { if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_FM..."); return AudioSystem.STREAM_FM; /// @} } else { if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: using STREAM_MUSIC as default"); return AudioSystem.STREAM_MUSIC; } /// M: Add for FM volume adjust @ { } else if (AudioSystem.isStreamActive(AudioSystem.STREAM_MUSIC, 0)) { if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_MUSIC stream active"); return AudioSystem.STREAM_MUSIC; /// M: Add for FM volume adjust @ { } else if (isFmOn()) { if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_FM..."); return AudioSystem.STREAM_FM; /// @} } else { if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Returning suggested type " + suggestedStreamType); return suggestedStreamType; } } } private boolean isInCommunication() { boolean isOffhook = false; if (mVoiceCapable) { try { ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone")); if (phone != null) isOffhook = phone.isOffhook(); } catch (RemoteException e) { Log.w(TAG, "Couldn't connect to phone service", e); } } return (isOffhook || getMode() == AudioManager.MODE_IN_COMMUNICATION); }

解释下以上代码 音量按键经过事件分发处理最后会调用adjustSuggestedStreamVolume方法,之后在 getActiveStreamType方法中鉴定出最终的stream type ,在该方法中 会作出一个判断,isInCommunication() 中的返回 (isOffhook || getMode() == AudioManager.MODE_IN_COMMUNICATION); 这个两个条件只需满足其中之一便可返回true ,从而使getActiveStreamType方法返回

return AudioSystem.STREAM_VOICE_CALL;

而不是默认的

Log.v(TAG, "getActiveStreamType: Forcing STREAM_RING b/c default"); return AudioSystem.STREAM_RING;

因此只需将

audioManager.setMode(AudioManager.MODE_IN_CALL);

换成

audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);

音量按键就可以调节听筒音量大小了



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3