当耳机插入android时,我只想从耳机发出声音。 (按警报音量)
AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_ALARM,alarmVolume,0);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mediaPlayer.prepare();
mediaPlayer.setLooping(false);
mediaPlayer.start();
答案 0 :(得分:0)
为此你需要听有线耳机状态。
private void registerWiredHeadsetIntentBroadcast() {
IntentFilter filter = new IntentFilter("android.intent.action.HEADSET_PLUG");
this.wiredHeadsetReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
int state = intent.getIntExtra("state", 0);
int microphone = intent.getIntExtra("microphone", 0);
boolean hasWiredHeadset = state == 1;
isWiredHeadsetPlugged=hasWiredHeadset;
switch (state) {
case 0:
//unplugged
audioManager.setWiredHeadsetOn(false);
audioManager.setSpeakerphoneOn(true);
break;
case 1:
//plugged
audioManager.setWiredHeadsetOn(true);
break;
default:
}
}
};
this.apprtcContext.registerReceiver(this.wiredHeadsetReceiver, filter);
}
将AudioStreamType分配给音频管理器,你很高兴。