下面列出了android.media.AudioManager#getMode ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onReceive(Context context, Intent intent) {
// Load preferences
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
// Check phone state
String phone_state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (phone_state.equals(TelephonyManager.EXTRA_STATE_RINGING) && prefs.getBoolean("enabled", false)) {
// Check for "second call" restriction
if (prefs.getBoolean("no_second_call", false)) {
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
if (am.getMode() == AudioManager.MODE_IN_CALL) {
return;
}
}
// Call a service, since this could take a few seconds
context.startService(new Intent(context, AutoAnswerIntentService.class));
}
}
@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);
}
}
}
}
public void startAudioRecording() {
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
if (am.getMode() == AudioManager.MODE_NORMAL) {
mediaRecorder = new MediaRecorder();
String fileName = UUID.randomUUID().toString().substring(0, 8) + ".m4a";
outputFilePath = new File(context.getFilesDir(), fileName);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
//maybe we can modify these in the future, or allow people to tweak them
mediaRecorder.setAudioChannels(1);
mediaRecorder.setAudioEncodingBitRate(22050);
mediaRecorder.setAudioSamplingRate(64000);
mediaRecorder.setOutputFile(outputFilePath.getAbsolutePath());
try {
isAudioRecording = true;
mediaRecorder.prepare();
mediaRecorder.start();
if (getVisualizerView() != null) {
startLevelListener();
}
} catch (Exception e) {
Log.e(LOG_TAG, "couldn't start audio", e);
}
}
}
/**
* 获取当前的音频模式
* <pre>
* 返回值有下述几种模式:
* MODE_NORMAL( 普通 )
* MODE_RINGTONE( 铃声 )
* MODE_IN_CALL( 打电话 )
* MODE_IN_COMMUNICATION( 通话 )
* </pre>
* @return 当前的音频模式
*/
public static int getMode() {
AudioManager audioManager = AppUtils.getAudioManager();
if (audioManager != null) {
try {
return audioManager.getMode();
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "getMode");
}
}
return AudioManager.MODE_NORMAL;
}
private void startBluetoothSco() {
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (am != null) {
if (am.getMode() != AudioManager.MODE_IN_COMMUNICATION) {
am.setMode(AudioManager.MODE_IN_COMMUNICATION);
}
am.setSpeakerphoneOn(false);
am.startBluetoothSco();
}
}
/**
* 设置默认的合成引擎
*
* @param type
*/
private void setDefaultSynthesizerParam(NetType type) {
if (isSpeaking()) {
Log.e(TAG, "播放当中切换播放引擎失败");
return;
}
synthesizer.setParameter(SpeechConstant.SPEED, Integer.toString(mVoiceConfig.getVolSpeed()));
synthesizer.setParameter(SpeechConstant.VOLUME, Integer.toString(mVoiceConfig.getVolume()));
synthesizer.setParameter(SpeechConstant.VOICE_NAME, mVoiceConfig.getVolName());
synthesizer.setParameter("rdn", "0");
synthesizer.setParameter(SpeechConstant.KEY_REQUEST_FOCUS, "0");
if (mediator.isBlueToothHeadSet()) {
synthesizer.setParameter(SpeechConstant.STREAM_TYPE, Integer.toString(AudioManager.STREAM_VOICE_CALL));
} else {
synthesizer.setParameter(SpeechConstant.STREAM_TYPE, Integer.toString(AudioManager.STREAM_MUSIC));
AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
if (am.getMode() != AudioManager.MODE_NORMAL) {
am.setMode(AudioManager.MODE_NORMAL);
}
}
//synthesizer.setParameter(SpeechConstant.ENGINE_TYPE, VoiceConfig.getSynEngineType());
/*if(type==NetType.NETWORK_TYPE_NONE||type==NetType.NETWORK_TYPE_2G){//网络关闭的状态下启动离线识别
Log.e(TAG, "离线。。。。。。。。。。。。。。。。。。。");
synthesizer.setParameter(SpeechConstant.SPEED, OFFLINE_ENGINE_SPPED);
synthesizer.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_LOCAL);
synthesizer.setParameter(SpeechConstant.VOICE_NAME, "xiaoyan");
isLocalEngine=true;
}
else{*/
Log.e(TAG, "在线。。。。。。。。。。。。。。。。。。。");
//synthesizer.setParameter(SpeechConstant.SPEED,"70");
synthesizer.setParameter(SpeechConstant.VOICE_NAME, mVoiceConfig.getVolName());
//synthesizer.setParameter(SpeechConstant.VOICE_NAME, ChatRobot.isInit()&&ChatRobot.getInstance().getMode()==ChatConstant.CHILDREN_MODE?"vinn":"vixq");
synthesizer.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);
//}
}
public static boolean isOngoingCall() {
try {
AudioManager manager = (AudioManager) xdrip.getAppContext().getSystemService(Context.AUDIO_SERVICE);
return (manager.getMode() == AudioManager.MODE_IN_CALL);
// possibly should have MODE_IN_COMMUNICATION as well
} catch (Exception e) {
return false;
}
}
public static boolean isOngoingCall() {
try {
AudioManager manager = (AudioManager) xdrip.getAppContext().getSystemService(Context.AUDIO_SERVICE);
return (manager.getMode() == AudioManager.MODE_IN_CALL);
// possibly should have MODE_IN_COMMUNICATION as well
} catch (Exception e) {
return false;
}
}
@SuppressWarnings("ConstantConditions")
private static boolean isOngoingCall() {
final AudioManager manager = (AudioManager) xdrip.getAppContext().getSystemService(Context.AUDIO_SERVICE);
try {
return (manager.getMode() == AudioManager.MODE_IN_CALL);
} catch (NullPointerException e) {
return false;
}
}
public static boolean isOngoingCall() {
try {
AudioManager manager = (AudioManager) xdrip.getAppContext().getSystemService(Context.AUDIO_SERVICE);
return (manager.getMode() == AudioManager.MODE_IN_CALL);
// possibly should have MODE_IN_COMMUNICATION as well
} catch (Exception e) {
return false;
}
}
public static boolean isOngoingCall() {
try {
AudioManager manager = (AudioManager) xdrip.getAppContext().getSystemService(Context.AUDIO_SERVICE);
return (manager.getMode() == AudioManager.MODE_IN_CALL);
// possibly should have MODE_IN_COMMUNICATION as well
} catch (Exception e) {
return false;
}
}
@SuppressWarnings("ConstantConditions")
private static boolean isOngoingCall() {
final AudioManager manager = (AudioManager) xdrip.getAppContext().getSystemService(Context.AUDIO_SERVICE);
try {
return (manager.getMode() == AudioManager.MODE_IN_CALL);
} catch (NullPointerException e) {
return false;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// assume that the user cancels the call if it is ringing
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (!canCallBeResumed()) {
if (null != mCall) {
mCall.hangup("");
}
} else {
saveCallView();
}
} else if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) || (keyCode == KeyEvent.KEYCODE_VOLUME_UP)) {
// this is a trick to reduce the ring volume :
// when the call is ringing, the AudioManager.Mode switch to MODE_IN_COMMUNICATION
// so the volume is the next call one whereas the user expects to reduce the ring volume.
if ((null != mCall) && mCall.getCallState().equals(IMXCall.CALL_STATE_RINGING)) {
AudioManager audioManager = (AudioManager) CallViewActivity.this.getSystemService(Context.AUDIO_SERVICE);
// IMXChrome call issue
if (audioManager.getMode() == AudioManager.MODE_IN_COMMUNICATION) {
int musicVol = audioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL) * audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) / audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, musicVol, 0);
}
}
}
return super.onKeyDown(keyCode, event);
}
public static int getSoundStreamType(Context context) {
// Get the audio manager
AudioManager audioManager = (AudioManager) context.getSystemService(context.AUDIO_SERVICE);
// In call?
if (audioManager.getMode() == AudioManager.MODE_IN_CALL) {
// Play in tiny speaker
// I think we disabled this because it's played in that speaker already by default
// return AudioManager.STREAM_VOICE_CALL;
}
// Return default stream type
return Sound.STREAM_TYPE;
}
void acquireMode(AudioManager audioManager) {
if (0 == naquire++) {
oldMode = audioManager.getMode();
audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
}
}
@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();
}
}
/**messagePop调用*/
public int getAudioMode(Context ctx) {
AudioManager audioManager = (AudioManager) ctx.getSystemService(Context.AUDIO_SERVICE);
return audioManager.getMode();
}
public static boolean isUserInCommunicationCall(Context context) {
AudioManager manager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
return manager.getMode() == AudioManager.MODE_IN_CALL || manager.getMode() == AudioManager.MODE_IN_COMMUNICATION;
}
private boolean isOngoingCall(){
AudioManager manager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
return (manager.getMode()==AudioManager.MODE_IN_CALL);
}
public void startAudioRecording ()
{
int permissionCheck = ContextCompat.checkSelfPermission(this,
Manifest.permission.RECORD_AUDIO);
if (permissionCheck ==PackageManager.PERMISSION_DENIED)
{
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.RECORD_AUDIO)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
Snackbar.make(mConvoView.getHistoryView(), R.string.grant_perms, Snackbar.LENGTH_LONG).show();
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.RECORD_AUDIO},
MY_PERMISSIONS_REQUEST_AUDIO);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
else {
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (am.getMode() == AudioManager.MODE_NORMAL) {
mMediaRecorder = new MediaRecorder();
String fileName = UUID.randomUUID().toString().substring(0, 8) + ".m4a";
mAudioFilePath = new File(getFilesDir(), fileName);
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
//maybe we can modify these in the future, or allow people to tweak them
mMediaRecorder.setAudioChannels(1);
mMediaRecorder.setAudioEncodingBitRate(22050);
mMediaRecorder.setAudioSamplingRate(64000);
mMediaRecorder.setOutputFile(mAudioFilePath.getAbsolutePath());
try {
mIsAudioRecording = true;
mMediaRecorder.prepare();
mMediaRecorder.start();
} catch (Exception e) {
Log.e(ImApp.LOG_TAG, "couldn't start audio", e);
}
}
}
}