android.media.AudioManager#setBluetoothScoOn ( )源码实例Demo

下面列出了android.media.AudioManager#setBluetoothScoOn ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: bcm-android   文件: BluetoothStateManager.java
public void setWantsConnection(boolean enabled) {
    synchronized (LOCK) {
        AudioManager audioManager = AppUtil.INSTANCE.getAudioManager(context);

        this.wantsConnection = enabled;

        if (wantsConnection && isBluetoothAvailable() && scoConnection == ScoConnection.DISCONNECTED) {
            audioManager.startBluetoothSco();
            scoConnection = ScoConnection.IN_PROGRESS;
        } else if (!wantsConnection && scoConnection == ScoConnection.CONNECTED) {
            audioManager.stopBluetoothSco();
            audioManager.setBluetoothScoOn(false);
            scoConnection = ScoConnection.DISCONNECTED;
        } else if (!wantsConnection && scoConnection == ScoConnection.IN_PROGRESS) {
            audioManager.stopBluetoothSco();
            scoConnection = ScoConnection.DISCONNECTED;
        }
    }
}
 
源代码2 项目: bcm-android   文件: SignalAudioManager.java
public void stop(boolean playDisconnected) {
  AudioManager audioManager = AppUtil.INSTANCE.getAudioManager(context);

  incomingRinger.stop();
  outgoingRinger.stop();

  if (playDisconnected) {
    soundPool.play(disconnectedSoundId, 1.0f, 1.0f, 0, 0, 1.0f);
  }

  if (audioManager.isBluetoothScoOn()) {
    audioManager.setBluetoothScoOn(false);
    audioManager.stopBluetoothSco();
  }

  audioManager.setSpeakerphoneOn(false);
  audioManager.setMicrophoneMute(false);
  audioManager.setMode(AudioManager.MODE_NORMAL);
  audioManager.abandonAudioFocus(null);
}
 
源代码3 项目: sealrtc-android   文件: CallActivity.java
@Override
public void onNotifySCOAudioStateChange(int scoAudioState) {
    switch (scoAudioState) {
        case AudioManager.SCO_AUDIO_STATE_CONNECTED:
            AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            if (am != null) {
                am.setBluetoothScoOn(true);
            }
            break;
        case AudioManager.SCO_AUDIO_STATE_DISCONNECTED:
            Log.d("onNotifyHeadsetState",
                "onNotifySCOAudioStateChange: " + headsetPlugReceiver.isBluetoothConnected());
            if (headsetPlugReceiver.isBluetoothConnected()) {
                startBluetoothSco();
            }
            break;
    }
}
 
源代码4 项目: AssistantBySDK   文件: AssistantService.java
@Override
public void execute(AudioManager audioManager) {
    if (voiceMediator.isBlueToothHeadSet()) {
        if (!voiceMediator.isSuportA2DP()) {
            if (audioManager.getMode() != AudioManager.MODE_NORMAL) {
                Log.e(TAG, "playInChannel>>setMode(AudioManager.MODE_NORMAL)");
                audioManager.setMode(AudioManager.MODE_NORMAL);
            }
            if (audioManager.isBluetoothScoOn()) {
                audioManager.setBluetoothScoOn(false);
                audioManager.stopBluetoothSco();
            }
        } else {
            if (!audioManager.isBluetoothA2dpOn()) {
                Log.e(TAG, "playInChannel>>setBluetoothA2dpOn(true)");
                audioManager.setBluetoothA2dpOn(true);
            }
        }
    }
}
 
源代码5 项目: BlueSound   文件: BluetoothManager.java
public boolean streamAudioStart(Context context) {
	try{
		AudioManager localAudioManager = (AudioManager) context
				.getSystemService(Context.AUDIO_SERVICE);
		if( localAudioManager == null ){ return false; }
		localAudioManager.setMode(0);
		localAudioManager.setBluetoothScoOn(true);
		localAudioManager.startBluetoothSco();
		localAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
		mAudioStatus = true;
	}catch( Exception ex){
		System.out.println("Exception on Start " + ex.getMessage() );
		mAudioStatus = false;
		return false;
	}

	return mAudioStatus;
}
 
源代码6 项目: mollyim-android   文件: BluetoothStateManager.java
@Override
public void onReceive(Context context, Intent intent) {
  if (intent == null) return;
  Log.i(TAG, "onReceive");

  synchronized (LOCK) {
    if (getScoChangeIntent().equals(intent.getAction())) {
      int status = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, AudioManager.SCO_AUDIO_STATE_ERROR);

      if (status == AudioManager.SCO_AUDIO_STATE_CONNECTED) {
        if (bluetoothHeadset != null) {
          List<BluetoothDevice> devices = bluetoothHeadset.getConnectedDevices();

          for (BluetoothDevice device : devices) {
            if (bluetoothHeadset.isAudioConnected(device)) {
              int deviceClass = device.getBluetoothClass().getDeviceClass();

              if (deviceClass == BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE ||
                  deviceClass == BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO ||
                  deviceClass == BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET)
              {
                scoConnection = ScoConnection.CONNECTED;

                if (wantsConnection) {
                  AudioManager audioManager = ServiceUtil.getAudioManager(context);
                  audioManager.setBluetoothScoOn(true);
                }
              }
            }
          }

        }
      }
    }
  }

  handleBluetoothStateChange();
}
 
源代码7 项目: Telegram   文件: MediaController.java
private void setUseFrontSpeaker(boolean value) {
    useFrontSpeaker = value;
    AudioManager audioManager = NotificationsController.audioManager;
    if (useFrontSpeaker) {
        audioManager.setBluetoothScoOn(false);
        audioManager.setSpeakerphoneOn(false);
    } else {
        audioManager.setSpeakerphoneOn(true);
    }
}
 
源代码8 项目: bcm-android   文件: BluetoothStateManager.java
@Override
public void onReceive(Context context, Intent intent) {
    if (intent == null) {
        return;
    }
    Log.w(TAG, "onReceive");

    synchronized (LOCK) {
        if (getScoChangeIntent().equals(intent.getAction())) {
            int status = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, AudioManager.SCO_AUDIO_STATE_ERROR);

            if (status == AudioManager.SCO_AUDIO_STATE_CONNECTED && bluetoothHeadset != null) {
                List<BluetoothDevice> devices = bluetoothHeadset.getConnectedDevices();

                for (BluetoothDevice device : devices) {
                    if (bluetoothHeadset.isAudioConnected(device)) {
                        int deviceClass = device.getBluetoothClass().getDeviceClass();

                        if (deviceClass == BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE ||
                                deviceClass == BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO ||
                                deviceClass == BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET) {
                            scoConnection = ScoConnection.CONNECTED;

                            if (wantsConnection) {
                                AudioManager audioManager = AppUtil.INSTANCE.getAudioManager(context);
                                audioManager.setBluetoothScoOn(true);
                            }
                        }
                    }
                }
            }
        }
    }

    handleBluetoothStateChange();
}
 
源代码9 项目: TelePlus-Android   文件: VoIPBaseService.java
protected void updateBluetoothHeadsetState(boolean connected){
	if(connected==isBtHeadsetConnected)
		return;
	if(BuildVars.LOGS_ENABLED)
		FileLog.d("updateBluetoothHeadsetState: "+connected);
	isBtHeadsetConnected=connected;
	final AudioManager am=(AudioManager)getSystemService(AUDIO_SERVICE);
	if(connected && !isRinging() && currentState!=0){
		if(bluetoothScoActive){
			if(BuildVars.LOGS_ENABLED)
				FileLog.d("SCO already active, setting audio routing");
			am.setSpeakerphoneOn(false);
			am.setBluetoothScoOn(true);
		}else{
			if(BuildVars.LOGS_ENABLED)
				FileLog.d("startBluetoothSco");
			needSwitchToBluetoothAfterScoActivates=true;
			// some devices ignore startBluetoothSco when called immediately after the headset is connected, so delay it
			AndroidUtilities.runOnUIThread(new Runnable(){
				@Override
				public void run(){
					try {
						am.startBluetoothSco();
					} catch (Throwable ignore) {

					}
				}
			}, 500);
		}
	}else{
		bluetoothScoActive=false;
	}
	for (StateListener l : stateListeners)
		l.onAudioSettingsChanged();
}
 
源代码10 项目: DevUtils   文件: AudioManagerUtils.java
/**
 * 设置是否使用蓝牙 SCO 耳机进行通讯
 * @param on {@code true} yes, {@code false} no
 * @return {@code true} success, {@code false} fail
 */
public static boolean setBluetoothScoOn(final boolean on) {
    AudioManager audioManager = AppUtils.getAudioManager();
    if (audioManager != null) {
        try {
            audioManager.setBluetoothScoOn(on);
            return true;
        } catch (Exception e) {
            LogPrintUtils.eTag(TAG, e, "setBluetoothScoOn");
        }
    }
    return false;
}
 
源代码11 项目: TelePlus-Android   文件: VoIPBaseService.java
protected void updateBluetoothHeadsetState(boolean connected){
	if(connected==isBtHeadsetConnected)
		return;
	if(BuildVars.LOGS_ENABLED)
		FileLog.d("updateBluetoothHeadsetState: "+connected);
	isBtHeadsetConnected=connected;
	final AudioManager am=(AudioManager)getSystemService(AUDIO_SERVICE);
	if(connected && !isRinging() && currentState!=0){
		if(bluetoothScoActive){
			if(BuildVars.LOGS_ENABLED)
				FileLog.d("SCO already active, setting audio routing");
			am.setSpeakerphoneOn(false);
			am.setBluetoothScoOn(true);
		}else{
			if(BuildVars.LOGS_ENABLED)
				FileLog.d("startBluetoothSco");
			needSwitchToBluetoothAfterScoActivates=true;
			// some devices ignore startBluetoothSco when called immediately after the headset is connected, so delay it
			AndroidUtilities.runOnUIThread(new Runnable(){
				@Override
				public void run(){
					try {
						am.startBluetoothSco();
					} catch (Throwable ignore) {

					}
				}
			}, 500);
		}
	}else{
		bluetoothScoActive=false;
	}
	for (StateListener l : stateListeners)
		l.onAudioSettingsChanged();
}
 
源代码12 项目: Telegram   文件: VoIPBaseService.java
protected void updateBluetoothHeadsetState(boolean connected){
	if(connected==isBtHeadsetConnected)
		return;
	if(BuildVars.LOGS_ENABLED)
		FileLog.d("updateBluetoothHeadsetState: "+connected);
	isBtHeadsetConnected=connected;
	final AudioManager am=(AudioManager)getSystemService(AUDIO_SERVICE);
	if(connected && !isRinging() && currentState!=0){
		if(bluetoothScoActive){
			if(BuildVars.LOGS_ENABLED)
				FileLog.d("SCO already active, setting audio routing");
			am.setSpeakerphoneOn(false);
			am.setBluetoothScoOn(true);
		}else{
			if(BuildVars.LOGS_ENABLED)
				FileLog.d("startBluetoothSco");
			needSwitchToBluetoothAfterScoActivates=true;
			// some devices ignore startBluetoothSco when called immediately after the headset is connected, so delay it
			AndroidUtilities.runOnUIThread(new Runnable(){
				@Override
				public void run(){
					try {
						am.startBluetoothSco();
					} catch (Throwable ignore) {

					}
				}
			}, 500);
		}
	}else{
		bluetoothScoActive=false;
	}
	for (StateListener l : stateListeners)
		l.onAudioSettingsChanged();
}
 
源代码13 项目: Telegram-FOSS   文件: MediaController.java
private void setUseFrontSpeaker(boolean value) {
    useFrontSpeaker = value;
    AudioManager audioManager = NotificationsController.audioManager;
    if (useFrontSpeaker) {
        audioManager.setBluetoothScoOn(false);
        audioManager.setSpeakerphoneOn(false);
    } else {
        audioManager.setSpeakerphoneOn(true);
    }
}
 
源代码14 项目: Telegram-FOSS   文件: VoIPBaseService.java
protected void configureDeviceForCall() {
	needPlayEndSound = true;
	AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
	if(!USE_CONNECTION_SERVICE){
		am.setMode(AudioManager.MODE_IN_COMMUNICATION);
		am.requestAudioFocus(this, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN);
		if(isBluetoothHeadsetConnected() && hasEarpiece()){
			switch(audioRouteToSet){
				case AUDIO_ROUTE_BLUETOOTH:
					if(!bluetoothScoActive){
						needSwitchToBluetoothAfterScoActivates=true;
						try {
							am.startBluetoothSco();
						} catch (Throwable ignore) {

						}
					}else{
						am.setBluetoothScoOn(true);
						am.setSpeakerphoneOn(false);
					}
					break;
				case AUDIO_ROUTE_EARPIECE:
					am.setBluetoothScoOn(false);
					am.setSpeakerphoneOn(false);
					break;
				case AUDIO_ROUTE_SPEAKER:
					am.setBluetoothScoOn(false);
					am.setSpeakerphoneOn(true);
					break;
			}
		}else if(isBluetoothHeadsetConnected()){
			am.setBluetoothScoOn(speakerphoneStateToSet);
		}else{
			am.setSpeakerphoneOn(speakerphoneStateToSet);
		}
	}/*else{
		if(isBluetoothHeadsetConnected() && hasEarpiece()){
			switch(audioRouteToSet){
				case AUDIO_ROUTE_BLUETOOTH:
					systemCallConnection.setAudioRoute(CallAudioState.ROUTE_BLUETOOTH);
					break;
				case AUDIO_ROUTE_EARPIECE:
					systemCallConnection.setAudioRoute(CallAudioState.ROUTE_WIRED_OR_EARPIECE);
					break;
				case AUDIO_ROUTE_SPEAKER:
					systemCallConnection.setAudioRoute(CallAudioState.ROUTE_SPEAKER);
					break;
			}
		}else{
			if(hasEarpiece())
				systemCallConnection.setAudioRoute(!speakerphoneStateToSet ? CallAudioState.ROUTE_WIRED_OR_EARPIECE : CallAudioState.ROUTE_SPEAKER);
			else
				systemCallConnection.setAudioRoute(!speakerphoneStateToSet ? CallAudioState.ROUTE_WIRED_OR_EARPIECE : CallAudioState.ROUTE_BLUETOOTH);
		}
	}*/
	updateOutputGainControlState();
	audioConfigured=true;

	SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
	Sensor proximity = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
	try{
		if(proximity!=null){
			proximityWakelock=((PowerManager) getSystemService(Context.POWER_SERVICE)).newWakeLock(PROXIMITY_SCREEN_OFF_WAKE_LOCK, "telegram-voip-prx");
			sm.registerListener(this, proximity, SensorManager.SENSOR_DELAY_NORMAL);
		}
	}catch(Exception x){
		if (BuildVars.LOGS_ENABLED) {
			FileLog.e("Error initializing proximity sensor", x);
		}
	}
}
 
源代码15 项目: Telegram   文件: VoIPBaseService.java
protected void configureDeviceForCall() {
	needPlayEndSound = true;
	AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
	if(!USE_CONNECTION_SERVICE){
		am.setMode(AudioManager.MODE_IN_COMMUNICATION);
		am.requestAudioFocus(this, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN);
		if(isBluetoothHeadsetConnected() && hasEarpiece()){
			switch(audioRouteToSet){
				case AUDIO_ROUTE_BLUETOOTH:
					if(!bluetoothScoActive){
						needSwitchToBluetoothAfterScoActivates=true;
						try {
							am.startBluetoothSco();
						} catch (Throwable ignore) {

						}
					}else{
						am.setBluetoothScoOn(true);
						am.setSpeakerphoneOn(false);
					}
					break;
				case AUDIO_ROUTE_EARPIECE:
					am.setBluetoothScoOn(false);
					am.setSpeakerphoneOn(false);
					break;
				case AUDIO_ROUTE_SPEAKER:
					am.setBluetoothScoOn(false);
					am.setSpeakerphoneOn(true);
					break;
			}
		}else if(isBluetoothHeadsetConnected()){
			am.setBluetoothScoOn(speakerphoneStateToSet);
		}else{
			am.setSpeakerphoneOn(speakerphoneStateToSet);
		}
	}/*else{
		if(isBluetoothHeadsetConnected() && hasEarpiece()){
			switch(audioRouteToSet){
				case AUDIO_ROUTE_BLUETOOTH:
					systemCallConnection.setAudioRoute(CallAudioState.ROUTE_BLUETOOTH);
					break;
				case AUDIO_ROUTE_EARPIECE:
					systemCallConnection.setAudioRoute(CallAudioState.ROUTE_WIRED_OR_EARPIECE);
					break;
				case AUDIO_ROUTE_SPEAKER:
					systemCallConnection.setAudioRoute(CallAudioState.ROUTE_SPEAKER);
					break;
			}
		}else{
			if(hasEarpiece())
				systemCallConnection.setAudioRoute(!speakerphoneStateToSet ? CallAudioState.ROUTE_WIRED_OR_EARPIECE : CallAudioState.ROUTE_SPEAKER);
			else
				systemCallConnection.setAudioRoute(!speakerphoneStateToSet ? CallAudioState.ROUTE_WIRED_OR_EARPIECE : CallAudioState.ROUTE_BLUETOOTH);
		}
	}*/
	updateOutputGainControlState();
	audioConfigured=true;

	SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
	Sensor proximity = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
	try{
		if(proximity!=null){
			proximityWakelock=((PowerManager) getSystemService(Context.POWER_SERVICE)).newWakeLock(PROXIMITY_SCREEN_OFF_WAKE_LOCK, "telegram-voip-prx");
			sm.registerListener(this, proximity, SensorManager.SENSOR_DELAY_NORMAL);
		}
	}catch(Exception x){
		if (BuildVars.LOGS_ENABLED) {
			FileLog.e("Error initializing proximity sensor", x);
		}
	}
}
 
源代码16 项目: TelePlus-Android   文件: VoIPBaseService.java
protected void configureDeviceForCall() {
	needPlayEndSound = true;
	AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
	if(!USE_CONNECTION_SERVICE){
		am.setMode(AudioManager.MODE_IN_COMMUNICATION);
		am.requestAudioFocus(this, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN);
		if(isBluetoothHeadsetConnected() && hasEarpiece()){
			switch(audioRouteToSet){
				case AUDIO_ROUTE_BLUETOOTH:
					if(!bluetoothScoActive){
						needSwitchToBluetoothAfterScoActivates=true;
						try {
							am.startBluetoothSco();
						} catch (Throwable ignore) {

						}
					}else{
						am.setBluetoothScoOn(true);
						am.setSpeakerphoneOn(false);
					}
					break;
				case AUDIO_ROUTE_EARPIECE:
					am.setBluetoothScoOn(false);
					am.setSpeakerphoneOn(false);
					break;
				case AUDIO_ROUTE_SPEAKER:
					am.setBluetoothScoOn(false);
					am.setSpeakerphoneOn(true);
					break;
			}
		}else if(isBluetoothHeadsetConnected()){
			am.setBluetoothScoOn(speakerphoneStateToSet);
		}else{
			am.setSpeakerphoneOn(speakerphoneStateToSet);
		}
	}/*else{
		if(isBluetoothHeadsetConnected() && hasEarpiece()){
			switch(audioRouteToSet){
				case AUDIO_ROUTE_BLUETOOTH:
					systemCallConnection.setAudioRoute(CallAudioState.ROUTE_BLUETOOTH);
					break;
				case AUDIO_ROUTE_EARPIECE:
					systemCallConnection.setAudioRoute(CallAudioState.ROUTE_WIRED_OR_EARPIECE);
					break;
				case AUDIO_ROUTE_SPEAKER:
					systemCallConnection.setAudioRoute(CallAudioState.ROUTE_SPEAKER);
					break;
			}
		}else{
			if(hasEarpiece())
				systemCallConnection.setAudioRoute(!speakerphoneStateToSet ? CallAudioState.ROUTE_WIRED_OR_EARPIECE : CallAudioState.ROUTE_SPEAKER);
			else
				systemCallConnection.setAudioRoute(!speakerphoneStateToSet ? CallAudioState.ROUTE_WIRED_OR_EARPIECE : CallAudioState.ROUTE_BLUETOOTH);
		}
	}*/
	updateOutputGainControlState();
	audioConfigured=true;

	SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
	Sensor proximity = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
	try{
		if(proximity!=null){
			proximityWakelock=((PowerManager) getSystemService(Context.POWER_SERVICE)).newWakeLock(PROXIMITY_SCREEN_OFF_WAKE_LOCK, "telegram-voip-prx");
			sm.registerListener(this, proximity, SensorManager.SENSOR_DELAY_NORMAL);
		}
	}catch(Exception x){
		if (BuildVars.LOGS_ENABLED) {
			FileLog.e("Error initializing proximity sensor", x);
		}
	}
}
 
源代码17 项目: sealrtc-android   文件: CallActivity.java
@Override
public void onNotifyHeadsetState(boolean connected, int type) {
    try {
        if (connected) {
            HeadsetPlugReceiverState = true;
            if (type == 0) {
                startBluetoothSco();
            }
            if (null != btnMuteSpeaker) {
                btnMuteSpeaker.setBackgroundResource(R.drawable.img_capture_gray);
                btnMuteSpeaker.setSelected(false);
                btnMuteSpeaker.setEnabled(false);
                btnMuteSpeaker.setClickable(false);
                audioManager.onToggleSpeaker(false);
            }
        } else {
            if (type == 1 && BluetoothUtil.hasBluetoothA2dpConnected()) {
                return;
            }
            AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            if (am != null) {
                if (am.getMode() != AudioManager.MODE_IN_COMMUNICATION) {
                    am.setMode(AudioManager.MODE_IN_COMMUNICATION);
                }
                if (type == 0) {
                    am.stopBluetoothSco();
                    am.setBluetoothScoOn(false);
                    am.setSpeakerphoneOn(!muteSpeaker);
                } else {
                    RCRTCEngine.getInstance().enableSpeaker(!this.muteSpeaker);
                }
                audioManager.onToggleSpeaker(!muteSpeaker);
            }
            if (null != btnMuteSpeaker) {
                btnMuteSpeaker.setBackgroundResource(R.drawable.selector_checkbox_capture);
                btnMuteSpeaker.setSelected(false);
                btnMuteSpeaker.setEnabled(true);
                btnMuteSpeaker.setClickable(true);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
源代码18 项目: TelePlus-Android   文件: VoIPBaseService.java
protected void configureDeviceForCall() {
	needPlayEndSound = true;
	AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
	if(!USE_CONNECTION_SERVICE){
		am.setMode(AudioManager.MODE_IN_COMMUNICATION);
		am.requestAudioFocus(this, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN);
		if(isBluetoothHeadsetConnected() && hasEarpiece()){
			switch(audioRouteToSet){
				case AUDIO_ROUTE_BLUETOOTH:
					if(!bluetoothScoActive){
						needSwitchToBluetoothAfterScoActivates=true;
						try {
							am.startBluetoothSco();
						} catch (Throwable ignore) {

						}
					}else{
						am.setBluetoothScoOn(true);
						am.setSpeakerphoneOn(false);
					}
					break;
				case AUDIO_ROUTE_EARPIECE:
					am.setBluetoothScoOn(false);
					am.setSpeakerphoneOn(false);
					break;
				case AUDIO_ROUTE_SPEAKER:
					am.setBluetoothScoOn(false);
					am.setSpeakerphoneOn(true);
					break;
			}
		}else if(isBluetoothHeadsetConnected()){
			am.setBluetoothScoOn(speakerphoneStateToSet);
		}else{
			am.setSpeakerphoneOn(speakerphoneStateToSet);
		}
	}/*else{
		if(isBluetoothHeadsetConnected() && hasEarpiece()){
			switch(audioRouteToSet){
				case AUDIO_ROUTE_BLUETOOTH:
					systemCallConnection.setAudioRoute(CallAudioState.ROUTE_BLUETOOTH);
					break;
				case AUDIO_ROUTE_EARPIECE:
					systemCallConnection.setAudioRoute(CallAudioState.ROUTE_WIRED_OR_EARPIECE);
					break;
				case AUDIO_ROUTE_SPEAKER:
					systemCallConnection.setAudioRoute(CallAudioState.ROUTE_SPEAKER);
					break;
			}
		}else{
			if(hasEarpiece())
				systemCallConnection.setAudioRoute(!speakerphoneStateToSet ? CallAudioState.ROUTE_WIRED_OR_EARPIECE : CallAudioState.ROUTE_SPEAKER);
			else
				systemCallConnection.setAudioRoute(!speakerphoneStateToSet ? CallAudioState.ROUTE_WIRED_OR_EARPIECE : CallAudioState.ROUTE_BLUETOOTH);
		}
	}*/
	updateOutputGainControlState();
	audioConfigured=true;

	SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
	Sensor proximity = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
	try{
		if(proximity!=null){
			proximityWakelock=((PowerManager) getSystemService(Context.POWER_SERVICE)).newWakeLock(PROXIMITY_SCREEN_OFF_WAKE_LOCK, "telegram-voip-prx");
			sm.registerListener(this, proximity, SensorManager.SENSOR_DELAY_NORMAL);
		}
	}catch(Exception x){
		if (BuildVars.LOGS_ENABLED) {
			FileLog.e("Error initializing proximity sensor", x);
		}
	}
}
 
源代码19 项目: Telegram   文件: VoIPBaseService.java
@Override
public void onDestroy() {
	if (BuildVars.LOGS_ENABLED) {
		FileLog.d("=============== VoIPService STOPPING ===============");
	}
	stopForeground(true);
	stopRinging();
	NotificationCenter.getInstance(currentAccount).removeObserver(this, NotificationCenter.appDidLogout);
	SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
	Sensor proximity = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
	if (proximity != null) {
		sm.unregisterListener(this);
	}
	if (proximityWakelock != null && proximityWakelock.isHeld()) {
		proximityWakelock.release();
	}
	unregisterReceiver(receiver);
	if(timeoutRunnable!=null){
		AndroidUtilities.cancelRunOnUIThread(timeoutRunnable);
		timeoutRunnable=null;
	}
	super.onDestroy();
	sharedInstance = null;
	AndroidUtilities.runOnUIThread(new Runnable(){
		@Override
		public void run(){
			NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.didEndCall);
		}
	});
	if (tgVoip != null) {
		updateTrafficStats();
		StatsController.getInstance(currentAccount).incrementTotalCallsTime(getStatsNetworkType(), (int) (getCallDuration() / 1000) % 5);
		onTgVoipPreStop();
		onTgVoipStop(tgVoip.stop());
		prevTrafficStats = null;
		callStartTime = 0;
		tgVoip = null;
	}
	cpuWakelock.release();
	AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
	if(!USE_CONNECTION_SERVICE){
		if(isBtHeadsetConnected && !playingSound){
			am.stopBluetoothSco();
			am.setBluetoothScoOn(false);
			am.setSpeakerphoneOn(false);
		}
		try{
			am.setMode(AudioManager.MODE_NORMAL);
		}catch(SecurityException x){
			if(BuildVars.LOGS_ENABLED){
				FileLog.e("Error setting audio more to normal", x);
			}
		}
		am.abandonAudioFocus(this);
	}
	am.unregisterMediaButtonEventReceiver(new ComponentName(this, VoIPMediaButtonReceiver.class));
	if (haveAudioFocus)
		am.abandonAudioFocus(this);

	if (!playingSound)
		soundPool.release();

	if(USE_CONNECTION_SERVICE){
		if(!didDeleteConnectionServiceContact)
			ContactsController.getInstance(currentAccount).deleteConnectionServiceContact();
		if(systemCallConnection!=null && !playingSound){
			systemCallConnection.destroy();
		}
	}

	ConnectionsManager.getInstance(currentAccount).setAppPaused(true, false);
	VoIPHelper.lastCallTime=System.currentTimeMillis();
}
 
源代码20 项目: Telegram-FOSS   文件: VoIPBaseService.java
@Override
public void onDestroy() {
	if (BuildVars.LOGS_ENABLED) {
		FileLog.d("=============== VoIPService STOPPING ===============");
	}
	stopForeground(true);
	stopRinging();
	NotificationCenter.getInstance(currentAccount).removeObserver(this, NotificationCenter.appDidLogout);
	SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
	Sensor proximity = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
	if (proximity != null) {
		sm.unregisterListener(this);
	}
	if (proximityWakelock != null && proximityWakelock.isHeld()) {
		proximityWakelock.release();
	}
	unregisterReceiver(receiver);
	if(timeoutRunnable!=null){
		AndroidUtilities.cancelRunOnUIThread(timeoutRunnable);
		timeoutRunnable=null;
	}
	super.onDestroy();
	sharedInstance = null;
	AndroidUtilities.runOnUIThread(new Runnable(){
		@Override
		public void run(){
			NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.didEndCall);
		}
	});
	if (tgVoip != null) {
		updateTrafficStats();
		StatsController.getInstance(currentAccount).incrementTotalCallsTime(getStatsNetworkType(), (int) (getCallDuration() / 1000) % 5);
		onTgVoipPreStop();
		onTgVoipStop(tgVoip.stop());
		prevTrafficStats = null;
		callStartTime = 0;
		tgVoip = null;
	}
	cpuWakelock.release();
	AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
	if(!USE_CONNECTION_SERVICE){
		if(isBtHeadsetConnected && !playingSound){
			am.stopBluetoothSco();
			am.setBluetoothScoOn(false);
			am.setSpeakerphoneOn(false);
		}
		try{
			am.setMode(AudioManager.MODE_NORMAL);
		}catch(SecurityException x){
			if(BuildVars.LOGS_ENABLED){
				FileLog.e("Error setting audio more to normal", x);
			}
		}
		am.abandonAudioFocus(this);
	}
	am.unregisterMediaButtonEventReceiver(new ComponentName(this, VoIPMediaButtonReceiver.class));
	if (haveAudioFocus)
		am.abandonAudioFocus(this);

	if (!playingSound)
		soundPool.release();

	if(USE_CONNECTION_SERVICE){
		if(!didDeleteConnectionServiceContact)
			ContactsController.getInstance(currentAccount).deleteConnectionServiceContact();
		if(systemCallConnection!=null && !playingSound){
			systemCallConnection.destroy();
		}
	}

	ConnectionsManager.getInstance(currentAccount).setAppPaused(true, false);
	VoIPHelper.lastCallTime=System.currentTimeMillis();
}