下面列出了android.media.AudioManager#stopBluetoothSco ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void setWantsConnection(boolean enabled) {
synchronized (LOCK) {
AudioManager audioManager = ServiceUtil.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;
}
}
}
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;
}
}
}
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);
}
@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);
}
}
}
}
@Override
public void onReceive(Context context, Intent intent) {
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int audioState = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, AudioManager.SCO_AUDIO_STATE_ERROR);
switch (audioState) {
case AudioManager.SCO_AUDIO_STATE_CONNECTED:
mBluetoothScoOn = true;
mListener.onBluetoothScoConnected();
break;
case AudioManager.SCO_AUDIO_STATE_DISCONNECTED:
case AudioManager.SCO_AUDIO_STATE_ERROR:
am.stopBluetoothSco();
mBluetoothScoOn = false;
mListener.onBluetoothScoDisconnected();
break;
}
}
/**
* 停止蓝牙 SCO 音频连接
* @return {@code true} success, {@code false} fail
*/
public static boolean stopBluetoothSco() {
AudioManager audioManager = AppUtils.getAudioManager();
if (audioManager != null) {
try {
audioManager.stopBluetoothSco();
return true;
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "stopBluetoothSco");
}
}
return false;
}
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "blueConnectStateBroadcastReceiver action>>>>" + intent.getAction());
if (intent.getAction() == null)
return;
if (intent.getAction().equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
int blueconState = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, 0);
switch (blueconState) {
case BluetoothAdapter.STATE_CONNECTED:
Log.i(TAG, "blueConnectStateBroadcastReceiver>>>>STATE_CONNECTED");
voiceMediator.setBlueHeadSet(true);
isBlueToothHeadsetConnected();
break;
case BluetoothAdapter.STATE_CONNECTING:
Log.i(TAG, "blueConnectStateBroadcastReceiver>>>>STATE_CONNECTING");
break;
case BluetoothAdapter.STATE_DISCONNECTED:
Log.i(TAG, "blueConnectStateBroadcastReceiver>>>>STATE_DISCONNECTED");
voiceMediator.setBlueHeadSet(false);
voiceMediator.setSuportA2DP(false);
AudioManager mAudioManager_ = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mAudioManager_.setBluetoothScoOn(false);
mAudioManager_.stopBluetoothSco();
break;
case BluetoothAdapter.STATE_DISCONNECTING:
Log.i(TAG, "blueConnectStateBroadcastReceiver>>>>STATE_DISCONNECTING");
voiceMediator.setSuportA2DP(false);
break;
default:
break;
}
}
}
public void toggleBluetoothHeadset(boolean enabled) {
AudioManager audioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
if(enabled){
audioManager.startBluetoothSco();
} else {
audioManager.stopBluetoothSco();
}
}
public boolean streamAudioStop(Context context) {
mAudioStatus = false;
AudioManager localAudioManager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
if( localAudioManager == null ){ return false; }
localAudioManager.setBluetoothScoOn(false);
localAudioManager.stopBluetoothSco();
localAudioManager.setMode(AudioManager.MODE_NORMAL);
return true;
}
@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.didEndedCall);
}
});
if (controller != null && controllerStarted) {
lastKnownDuration = controller.getCallDuration();
updateStats();
StatsController.getInstance(currentAccount).incrementTotalCallsTime(getStatsNetworkType(), (int) (lastKnownDuration / 1000) % 5);
onControllerPreRelease();
controller.release();
controller = null;
}
cpuWakelock.release();
AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
if(!USE_CONNECTION_SERVICE){
if(isBtHeadsetConnected && !playingSound){
am.stopBluetoothSco();
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(systemCallConnection!=null && !playingSound){
systemCallConnection.destroy();
}
}
ConnectionsManager.getInstance(currentAccount).setAppPaused(true, false);
VoIPHelper.lastCallTime=System.currentTimeMillis();
}
@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();
}
}
@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.didEndedCall);
}
});
if (controller != null && controllerStarted) {
lastKnownDuration = controller.getCallDuration();
updateStats();
StatsController.getInstance(currentAccount).incrementTotalCallsTime(getStatsNetworkType(), (int) (lastKnownDuration / 1000) % 5);
onControllerPreRelease();
controller.release();
controller = null;
}
cpuWakelock.release();
AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
if(!USE_CONNECTION_SERVICE){
if(isBtHeadsetConnected && !playingSound){
am.stopBluetoothSco();
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(systemCallConnection!=null && !playingSound){
systemCallConnection.destroy();
}
}
ConnectionsManager.getInstance(currentAccount).setAppPaused(true, false);
VoIPHelper.lastCallTime=System.currentTimeMillis();
}
@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();
}
@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();
}