我正在为应用中的某些通知添加声音,如下所示:
Notification notification = ...
...
notification.sound = Uri.parse(...);
当通知声音播放时,即使耳机已插入手机,它也会通过扬声器播放,因此声音会在扬声器和耳机中播放。
如果插入耳机,是否可以仅通过耳机播放声音?
最佳答案
目前无法通过 耳机强制播放声音
请参阅此 google 问题跟踪器 Notifications are played through external speaker when headphones are plugged in 和讨论。
用于检查耳机是否已插入
注册接收器
ReceiveBroadcast receiver=new ReceiveBroadcast();
registerReceiver(receiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));
public void onReceive(Context context, Intent intent) {
if (intent.hasExtra("state")){
if (headsetConnected && intent.getIntExtra("state", 0) == 0){
headsetConnected = false;
} else if (!headsetConnected && intent.getIntExtra("state", 0) == 1){
headsetConnected = true;
}
}
}
关于android - 插入耳机时不要通过扬声器播放通知声音,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35289468/