下面列出了android.media.AudioManager#isSpeakerphoneOn ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void handleWiredHeadsetChange(Intent intent) {
Log.i(TAG, "handleWiredHeadsetChange...");
if ((activePeer != null) &&
(activePeer.getState() == CallState.CONNECTED ||
activePeer.getState() == CallState.DIALING ||
activePeer.getState() == CallState.RECEIVED_BUSY ||
activePeer.getState() == CallState.REMOTE_RINGING))
{
AudioManager audioManager = ServiceUtil.getAudioManager(this);
boolean present = intent.getBooleanExtra(EXTRA_AVAILABLE, false);
if (present && audioManager.isSpeakerphoneOn()) {
audioManager.setSpeakerphoneOn(false);
audioManager.setBluetoothScoOn(false);
} else if (!present && !audioManager.isSpeakerphoneOn() && !audioManager.isBluetoothScoOn() && localCameraState.isEnabled()) {
audioManager.setSpeakerphoneOn(true);
}
sendMessage(viewModelStateFor(activePeer), activePeer, localCameraState, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled, isRemoteVideoOffer);
}
}
private void handleWiredHeadsetChange(Intent intent) {
ALog.d(TAG, "handleWiredHeadsetChange...");
CallState currentState = this.callState.get();
if (currentState == CallState.STATE_CONNECTED ||
currentState == CallState.STATE_DIALING ||
currentState == CallState.STATE_REMOTE_RINGING) {
AudioManager audioManager = AppUtil.INSTANCE.getAudioManager(this);
boolean present = intent.getBooleanExtra(EXTRA_AVAILABLE, false);
if (present && audioManager.isSpeakerphoneOn()) {
audioManager.setSpeakerphoneOn(false);
audioManager.setBluetoothScoOn(false);
} else if (!present && !audioManager.isSpeakerphoneOn() && !audioManager.isBluetoothScoOn() && localCameraState.isEnabled()) {
audioManager.setSpeakerphoneOn(true);
}
if (recipient != null) {
sendMessage(viewModelStateFor(callState.get()), recipient, localCameraState, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled);
}
}
}
public int getCurrentAudioRoute(){
if(USE_CONNECTION_SERVICE){
if(systemCallConnection!=null && systemCallConnection.getCallAudioState()!=null){
switch(systemCallConnection.getCallAudioState().getRoute()){
case CallAudioState.ROUTE_BLUETOOTH:
return AUDIO_ROUTE_BLUETOOTH;
case CallAudioState.ROUTE_EARPIECE:
case CallAudioState.ROUTE_WIRED_HEADSET:
return AUDIO_ROUTE_EARPIECE;
case CallAudioState.ROUTE_SPEAKER:
return AUDIO_ROUTE_SPEAKER;
}
}
return audioRouteToSet;
}
if(audioConfigured){
AudioManager am=(AudioManager) getSystemService(AUDIO_SERVICE);
if(am.isBluetoothScoOn())
return AUDIO_ROUTE_BLUETOOTH;
else if(am.isSpeakerphoneOn())
return AUDIO_ROUTE_SPEAKER;
else
return AUDIO_ROUTE_EARPIECE;
}
return audioRouteToSet;
}
@SuppressLint("NewApi")
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
AudioManager am=(AudioManager) getSystemService(AUDIO_SERVICE);
if (isHeadsetPlugged || am.isSpeakerphoneOn() || (isBluetoothHeadsetConnected() && am.isBluetoothScoOn())) {
return;
}
boolean newIsNear = event.values[0] < Math.min(event.sensor.getMaximumRange(), 3);
if (newIsNear != isProximityNear) {
if (BuildVars.LOGS_ENABLED) {
FileLog.d("proximity " + newIsNear);
}
isProximityNear = newIsNear;
try{
if(isProximityNear){
proximityWakelock.acquire();
}else{
proximityWakelock.release(1); // this is non-public API before L
}
}catch(Exception x){
FileLog.e(x);
}
}
}
}
public int getCurrentAudioRoute(){
if(USE_CONNECTION_SERVICE){
if(systemCallConnection!=null && systemCallConnection.getCallAudioState()!=null){
switch(systemCallConnection.getCallAudioState().getRoute()){
case CallAudioState.ROUTE_BLUETOOTH:
return AUDIO_ROUTE_BLUETOOTH;
case CallAudioState.ROUTE_EARPIECE:
case CallAudioState.ROUTE_WIRED_HEADSET:
return AUDIO_ROUTE_EARPIECE;
case CallAudioState.ROUTE_SPEAKER:
return AUDIO_ROUTE_SPEAKER;
}
}
return audioRouteToSet;
}
if(audioConfigured){
AudioManager am=(AudioManager) getSystemService(AUDIO_SERVICE);
if(am.isBluetoothScoOn())
return AUDIO_ROUTE_BLUETOOTH;
else if(am.isSpeakerphoneOn())
return AUDIO_ROUTE_SPEAKER;
else
return AUDIO_ROUTE_EARPIECE;
}
return audioRouteToSet;
}
@SuppressLint("NewApi")
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
AudioManager am=(AudioManager) getSystemService(AUDIO_SERVICE);
if (isHeadsetPlugged || am.isSpeakerphoneOn() || (isBluetoothHeadsetConnected() && am.isBluetoothScoOn())) {
return;
}
boolean newIsNear = event.values[0] < Math.min(event.sensor.getMaximumRange(), 3);
if (newIsNear != isProximityNear) {
if (BuildVars.LOGS_ENABLED) {
FileLog.d("proximity " + newIsNear);
}
isProximityNear = newIsNear;
try{
if(isProximityNear){
proximityWakelock.acquire();
}else{
proximityWakelock.release(1); // this is non-public API before L
}
}catch(Exception x){
FileLog.e(x);
}
}
}
}
/**
* Sets the speaker phone mode.
*/
private void setSpeakerphoneOn(boolean on) {
AudioManager audioManager = (AudioManager) G.context.getSystemService(Context.AUDIO_SERVICE);
boolean wasOn = false;
if (audioManager != null) {
wasOn = audioManager.isSpeakerphoneOn();
}
if (wasOn == on) {
return;
}
if (audioManager != null) {
audioManager.setSpeakerphoneOn(on);
}
}
private void refreshSpeakerButton() {
if ((null != mCall) && mCall.getCallState().equals(IMXCall.CALL_STATE_CONNECTED)) {
mSpeakerSelectionView.setVisibility(View.VISIBLE);
AudioManager audioManager = (AudioManager) CallViewActivity.this.getSystemService(Context.AUDIO_SERVICE);
if (audioManager.isSpeakerphoneOn()) {
mSpeakerSelectionView.setImageResource(R.drawable.ic_material_call);
} else {
mSpeakerSelectionView.setImageResource(R.drawable.ic_material_volume);
}
CallViewActivity.this.setVolumeControlStream(audioManager.getMode());
} else {
mSpeakerSelectionView.setVisibility(View.GONE);
}
}
public int getCurrentAudioRoute(){
if(USE_CONNECTION_SERVICE){
if(systemCallConnection!=null && systemCallConnection.getCallAudioState()!=null){
switch(systemCallConnection.getCallAudioState().getRoute()){
case CallAudioState.ROUTE_BLUETOOTH:
return AUDIO_ROUTE_BLUETOOTH;
case CallAudioState.ROUTE_EARPIECE:
case CallAudioState.ROUTE_WIRED_HEADSET:
return AUDIO_ROUTE_EARPIECE;
case CallAudioState.ROUTE_SPEAKER:
return AUDIO_ROUTE_SPEAKER;
}
}
return audioRouteToSet;
}
if(audioConfigured){
AudioManager am=(AudioManager) getSystemService(AUDIO_SERVICE);
if(am.isBluetoothScoOn())
return AUDIO_ROUTE_BLUETOOTH;
else if(am.isSpeakerphoneOn())
return AUDIO_ROUTE_SPEAKER;
else
return AUDIO_ROUTE_EARPIECE;
}
return audioRouteToSet;
}
@SuppressLint("NewApi")
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
AudioManager am=(AudioManager) getSystemService(AUDIO_SERVICE);
if (isHeadsetPlugged || am.isSpeakerphoneOn() || (isBluetoothHeadsetConnected() && am.isBluetoothScoOn())) {
return;
}
boolean newIsNear = event.values[0] < Math.min(event.sensor.getMaximumRange(), 3);
if (newIsNear != isProximityNear) {
if (BuildVars.LOGS_ENABLED) {
FileLog.d("proximity " + newIsNear);
}
isProximityNear = newIsNear;
try{
if(isProximityNear){
proximityWakelock.acquire();
}else{
proximityWakelock.release(1); // this is non-public API before L
}
}catch(Exception x){
FileLog.e(x);
}
}
}
}
public int getCurrentAudioRoute(){
if(USE_CONNECTION_SERVICE){
if(systemCallConnection!=null && systemCallConnection.getCallAudioState()!=null){
switch(systemCallConnection.getCallAudioState().getRoute()){
case CallAudioState.ROUTE_BLUETOOTH:
return AUDIO_ROUTE_BLUETOOTH;
case CallAudioState.ROUTE_EARPIECE:
case CallAudioState.ROUTE_WIRED_HEADSET:
return AUDIO_ROUTE_EARPIECE;
case CallAudioState.ROUTE_SPEAKER:
return AUDIO_ROUTE_SPEAKER;
}
}
return audioRouteToSet;
}
if(audioConfigured){
AudioManager am=(AudioManager) getSystemService(AUDIO_SERVICE);
if(am.isBluetoothScoOn())
return AUDIO_ROUTE_BLUETOOTH;
else if(am.isSpeakerphoneOn())
return AUDIO_ROUTE_SPEAKER;
else
return AUDIO_ROUTE_EARPIECE;
}
return audioRouteToSet;
}
@SuppressLint("NewApi")
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
AudioManager am=(AudioManager) getSystemService(AUDIO_SERVICE);
if (isHeadsetPlugged || am.isSpeakerphoneOn() || (isBluetoothHeadsetConnected() && am.isBluetoothScoOn())) {
return;
}
boolean newIsNear = event.values[0] < Math.min(event.sensor.getMaximumRange(), 3);
if (newIsNear != isProximityNear) {
if (BuildVars.LOGS_ENABLED) {
FileLog.d("proximity " + newIsNear);
}
isProximityNear = newIsNear;
try{
if(isProximityNear){
proximityWakelock.acquire();
}else{
proximityWakelock.release(1); // this is non-public API before L
}
}catch(Exception x){
FileLog.e(x);
}
}
}
}
private LockManager.PhoneState getInCallPhoneState() {
AudioManager audioManager = ServiceUtil.getAudioManager(this);
if (audioManager.isSpeakerphoneOn() || audioManager.isBluetoothScoOn() || audioManager.isWiredHeadsetOn()) {
return LockManager.PhoneState.IN_HANDS_FREE_CALL;
} else {
return LockManager.PhoneState.IN_CALL;
}
}
public boolean isSpeakerphoneOn(){
if(USE_CONNECTION_SERVICE && systemCallConnection!=null && systemCallConnection.getCallAudioState()!=null){
int route=systemCallConnection.getCallAudioState().getRoute();
return hasEarpiece() ? route==CallAudioState.ROUTE_SPEAKER : route==CallAudioState.ROUTE_BLUETOOTH;
}else if(audioConfigured && !USE_CONNECTION_SERVICE){
AudioManager am=(AudioManager) getSystemService(AUDIO_SERVICE);
return hasEarpiece() ? am.isSpeakerphoneOn() : am.isBluetoothScoOn();
}
return speakerphoneStateToSet;
}
/**
* 判断是否打开扩音器 ( 扬声器 )
* @return {@code true} yes, {@code false} no
*/
public static boolean isSpeakerphoneOn() {
AudioManager audioManager = AppUtils.getAudioManager();
if (audioManager != null) {
try {
return audioManager.isSpeakerphoneOn();
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "isSpeakerphoneOn");
}
}
return false;
}
public boolean isSpeakerphoneOn(){
if(USE_CONNECTION_SERVICE && systemCallConnection!=null && systemCallConnection.getCallAudioState()!=null){
int route=systemCallConnection.getCallAudioState().getRoute();
return hasEarpiece() ? route==CallAudioState.ROUTE_SPEAKER : route==CallAudioState.ROUTE_BLUETOOTH;
}else if(audioConfigured && !USE_CONNECTION_SERVICE){
AudioManager am=(AudioManager) getSystemService(AUDIO_SERVICE);
return hasEarpiece() ? am.isSpeakerphoneOn() : am.isBluetoothScoOn();
}
return speakerphoneStateToSet;
}
public boolean isSpeakerphoneOn(){
if(USE_CONNECTION_SERVICE && systemCallConnection!=null && systemCallConnection.getCallAudioState()!=null){
int route=systemCallConnection.getCallAudioState().getRoute();
return hasEarpiece() ? route==CallAudioState.ROUTE_SPEAKER : route==CallAudioState.ROUTE_BLUETOOTH;
}else if(audioConfigured && !USE_CONNECTION_SERVICE){
AudioManager am=(AudioManager) getSystemService(AUDIO_SERVICE);
return hasEarpiece() ? am.isSpeakerphoneOn() : am.isBluetoothScoOn();
}
return speakerphoneStateToSet;
}
public boolean isSpeakerphoneOn(){
if(USE_CONNECTION_SERVICE && systemCallConnection!=null && systemCallConnection.getCallAudioState()!=null){
int route=systemCallConnection.getCallAudioState().getRoute();
return hasEarpiece() ? route==CallAudioState.ROUTE_SPEAKER : route==CallAudioState.ROUTE_BLUETOOTH;
}else if(audioConfigured && !USE_CONNECTION_SERVICE){
AudioManager am=(AudioManager) getSystemService(AUDIO_SERVICE);
return hasEarpiece() ? am.isSpeakerphoneOn() : am.isBluetoothScoOn();
}
return speakerphoneStateToSet;
}
private void handleSetEnableVideo(Intent intent) {
boolean enable = intent.getBooleanExtra(EXTRA_ENABLE, false);
AudioManager audioManager = ServiceUtil.getAudioManager(this);
if (activePeer == null) {
Log.w(TAG, "handleSetEnableVideo(): Ignoring for inactive call.");
return;
}
Log.i(TAG, "handleSetEnableVideo(): call_id: " + activePeer.getCallId());
if (activePeer.getState() != CallState.CONNECTED) {
enableVideoOnCreate = enable;
if (enableVideoOnCreate &&
!audioManager.isSpeakerphoneOn() &&
!audioManager.isBluetoothScoOn() &&
!audioManager.isWiredHeadsetOn())
{
audioManager.setSpeakerphoneOn(true);
}
return;
}
try {
callManager.setVideoEnable(enable);
} catch (CallException e) {
callFailure("setVideoEnable() failed: ", e);
return;
}
localCameraState = camera.getCameraState();
if (localCameraState.isEnabled()) {
lockManager.updatePhoneState(LockManager.PhoneState.IN_VIDEO);
} else {
lockManager.updatePhoneState(getInCallPhoneState());
}
if (localCameraState.isEnabled() &&
!audioManager.isSpeakerphoneOn() &&
!audioManager.isBluetoothScoOn() &&
!audioManager.isWiredHeadsetOn())
{
audioManager.setSpeakerphoneOn(true);
}
sendMessage(viewModelStateFor(activePeer), activePeer, localCameraState, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled, isRemoteVideoOffer);
}