How to check in browser if the headphones just got plugged out? [duplicate] Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How to detect whether a headphone is inserted or removed in JavaScript?Detecting when head phones are plugged inDetect if headphones are plugged in or not via C#Android: Checking if headphones are plugged inHow do I check if an object has a property in JavaScript?How to check empty/undefined/null string in JavaScript?How do I check if an element is hidden in jQuery?How do I check if an array includes an object in JavaScript?How to check if a string “StartsWith” another string?How do you check if a variable is an array in JavaScript?Detect rotation of Android phone in the browser with JavaScriptHow to check whether a string contains a substring in JavaScript?How to check for “undefined” in JavaScript?How to check if an object is an array?
Can 'non' with gerundive mean both lack of obligation and negative obligation?
Marquee sign letters
Converting a text document with special format to Pandas DataFrame
What's the connection between Mr. Nancy and fried chicken?
Is "ein Herz wie das meine" an antiquated or colloquial use of the possesive pronoun?
When does Bran Stark remember Jamie pushing him?
Raising a bilingual kid. When should we introduce the majority language?
Why did Israel vote against lifting the American embargo on Cuba?
What could prevent concentrated local exploration?
Why do C and C++ allow the expression (int) + 4*5?
Why do people think Winterfell crypts is the safest place for women, children & old people?
What is the ongoing value of the Kanban board to the developers as opposed to management
Who's this lady in the war room?
How is an IPA symbol that lacks a name (e.g. ɲ) called?
Does using the inspiration rules for character defects tend to encourage players to display MGS?
Can I take recommendation from someone I met at a conference?
Can a Knight grant Knighthood to another?
Why aren't these two solutions equivalent? Combinatorics problem
Is there a verb for listening stealthily?
Why isn't everyone flabbergasted about Bran's "gift"?
IC on Digikey is 5x more expensive than board containing same IC on Alibaba: How?
How to ask rejected full-time candidates to apply to teach individual courses?
false 'Security alert' from Google - every login generates mails from 'no-reply@accounts.google.com'
How to charge percentage of transaction cost?
How to check in browser if the headphones just got plugged out? [duplicate]
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How to detect whether a headphone is inserted or removed in JavaScript?Detecting when head phones are plugged inDetect if headphones are plugged in or not via C#Android: Checking if headphones are plugged inHow do I check if an object has a property in JavaScript?How to check empty/undefined/null string in JavaScript?How do I check if an element is hidden in jQuery?How do I check if an array includes an object in JavaScript?How to check if a string “StartsWith” another string?How do you check if a variable is an array in JavaScript?Detect rotation of Android phone in the browser with JavaScriptHow to check whether a string contains a substring in JavaScript?How to check for “undefined” in JavaScript?How to check if an object is an array?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
This question already has an answer here:
How to detect whether a headphone is inserted or removed in JavaScript?
2 answers
I want to pause the video if the audio device changes. e.g. if the headphones get plugged out. Is there an event for that?
I went through some other questions but they are not for browser
- Detect if headphones are plugged in or not via C#
- Detecting when head phones are plugged in
- Android: Checking if headphones are plugged in
javascript events
marked as duplicate by ADyson, Ouroborus, Community♦ Mar 11 at 5:10
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How to detect whether a headphone is inserted or removed in JavaScript?
2 answers
I want to pause the video if the audio device changes. e.g. if the headphones get plugged out. Is there an event for that?
I went through some other questions but they are not for browser
- Detect if headphones are plugged in or not via C#
- Detecting when head phones are plugged in
- Android: Checking if headphones are plugged in
javascript events
marked as duplicate by ADyson, Ouroborus, Community♦ Mar 11 at 5:10
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
I assume there has to be an event for it since youtube does this. +1
– Caleb Goodman
Mar 9 at 2:25
developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API should cover what's possible within a modern browser in terms of audio support
– ADyson
Mar 9 at 2:29
add a comment |
This question already has an answer here:
How to detect whether a headphone is inserted or removed in JavaScript?
2 answers
I want to pause the video if the audio device changes. e.g. if the headphones get plugged out. Is there an event for that?
I went through some other questions but they are not for browser
- Detect if headphones are plugged in or not via C#
- Detecting when head phones are plugged in
- Android: Checking if headphones are plugged in
javascript events
This question already has an answer here:
How to detect whether a headphone is inserted or removed in JavaScript?
2 answers
I want to pause the video if the audio device changes. e.g. if the headphones get plugged out. Is there an event for that?
I went through some other questions but they are not for browser
- Detect if headphones are plugged in or not via C#
- Detecting when head phones are plugged in
- Android: Checking if headphones are plugged in
This question already has an answer here:
How to detect whether a headphone is inserted or removed in JavaScript?
2 answers
javascript events
javascript events
asked Mar 9 at 2:21
Souradeep NandaSouradeep Nanda
1,09411625
1,09411625
marked as duplicate by ADyson, Ouroborus, Community♦ Mar 11 at 5:10
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by ADyson, Ouroborus, Community♦ Mar 11 at 5:10
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
I assume there has to be an event for it since youtube does this. +1
– Caleb Goodman
Mar 9 at 2:25
developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API should cover what's possible within a modern browser in terms of audio support
– ADyson
Mar 9 at 2:29
add a comment |
I assume there has to be an event for it since youtube does this. +1
– Caleb Goodman
Mar 9 at 2:25
developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API should cover what's possible within a modern browser in terms of audio support
– ADyson
Mar 9 at 2:29
I assume there has to be an event for it since youtube does this. +1
– Caleb Goodman
Mar 9 at 2:25
I assume there has to be an event for it since youtube does this. +1
– Caleb Goodman
Mar 9 at 2:25
developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API should cover what's possible within a modern browser in terms of audio support
– ADyson
Mar 9 at 2:29
developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API should cover what's possible within a modern browser in terms of audio support
– ADyson
Mar 9 at 2:29
add a comment |
1 Answer
1
active
oldest
votes
This isn't perfect and it might be a little hacky since it relies on the browser exposing the label attribute of the MediaDevice and then trying to use the string "head"to guess if the user has a headphone device connected, but you could also just watch the number of connected audio devices and pause whenever it changes or something depending on your usecase.
Almost all of the logic was adapted from the MDN page for navigator.mediaDevices.ondevicechange.
NOTE: This code does not work properly when executed from a file:// URL and must be accessed via an http:// or https:// URL for the browser to actually let you access the label property of the MediaDevice. Also, depending on your browser, you may need to use the hack featured in the first iteration of this answer which will ask the user for microphone access and grant you access to the label property.
// let's assume we don't have any headphones connected
let headphonesConnected = false;
const updateDevices = () =>
navigator.mediaDevices.enumerateDevices()
.then(function(devices)
// check to see if we have any device with "head" in its name connected
// like "headset" or "headphones"
headphonesConnected = devices
.filter(device => /audiow+/.test(device.kind))
.find(device => device.label.toLowerCase().includes('head'))
;
)
;
;
updateDevices();
// add an ondevicechange event listener so we can tell when
// an input device is connected and disconnected
navigator.mediaDevices.ondevicechange = updateDevices;
NOTE: Testing has been spotty with this new, simpler version, but it eliminates the microphone access requirement. Some browsers also take longer to trigger the ondevicechange handler than others.
In a better world I would be totally outraged that browsers have the unmitigated gall to make this possible, but in this world, it just never seems to stop. I already feel so utterly helpless against the existence of APIs like this and at this point I've no energy left to get outraged.
– Boann
Mar 9 at 21:48
@Boann I don't see the big deal in knowing if the user plugs or unplugs headphones, but maybe I'm not imaginative enough. The immediate usecase I could see would be to pause audio, like many mobile devices do, when the user's headphones are unplugged. I would actually prefer a much cleaner and less hacky implementation where you've just got anonAudioDestinationChangeoronAudioDeviceConnected/onAudioDeviceDisconnectedevent you can listen for.
– Eric Allen
Mar 10 at 16:46
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This isn't perfect and it might be a little hacky since it relies on the browser exposing the label attribute of the MediaDevice and then trying to use the string "head"to guess if the user has a headphone device connected, but you could also just watch the number of connected audio devices and pause whenever it changes or something depending on your usecase.
Almost all of the logic was adapted from the MDN page for navigator.mediaDevices.ondevicechange.
NOTE: This code does not work properly when executed from a file:// URL and must be accessed via an http:// or https:// URL for the browser to actually let you access the label property of the MediaDevice. Also, depending on your browser, you may need to use the hack featured in the first iteration of this answer which will ask the user for microphone access and grant you access to the label property.
// let's assume we don't have any headphones connected
let headphonesConnected = false;
const updateDevices = () =>
navigator.mediaDevices.enumerateDevices()
.then(function(devices)
// check to see if we have any device with "head" in its name connected
// like "headset" or "headphones"
headphonesConnected = devices
.filter(device => /audiow+/.test(device.kind))
.find(device => device.label.toLowerCase().includes('head'))
;
)
;
;
updateDevices();
// add an ondevicechange event listener so we can tell when
// an input device is connected and disconnected
navigator.mediaDevices.ondevicechange = updateDevices;
NOTE: Testing has been spotty with this new, simpler version, but it eliminates the microphone access requirement. Some browsers also take longer to trigger the ondevicechange handler than others.
In a better world I would be totally outraged that browsers have the unmitigated gall to make this possible, but in this world, it just never seems to stop. I already feel so utterly helpless against the existence of APIs like this and at this point I've no energy left to get outraged.
– Boann
Mar 9 at 21:48
@Boann I don't see the big deal in knowing if the user plugs or unplugs headphones, but maybe I'm not imaginative enough. The immediate usecase I could see would be to pause audio, like many mobile devices do, when the user's headphones are unplugged. I would actually prefer a much cleaner and less hacky implementation where you've just got anonAudioDestinationChangeoronAudioDeviceConnected/onAudioDeviceDisconnectedevent you can listen for.
– Eric Allen
Mar 10 at 16:46
add a comment |
This isn't perfect and it might be a little hacky since it relies on the browser exposing the label attribute of the MediaDevice and then trying to use the string "head"to guess if the user has a headphone device connected, but you could also just watch the number of connected audio devices and pause whenever it changes or something depending on your usecase.
Almost all of the logic was adapted from the MDN page for navigator.mediaDevices.ondevicechange.
NOTE: This code does not work properly when executed from a file:// URL and must be accessed via an http:// or https:// URL for the browser to actually let you access the label property of the MediaDevice. Also, depending on your browser, you may need to use the hack featured in the first iteration of this answer which will ask the user for microphone access and grant you access to the label property.
// let's assume we don't have any headphones connected
let headphonesConnected = false;
const updateDevices = () =>
navigator.mediaDevices.enumerateDevices()
.then(function(devices)
// check to see if we have any device with "head" in its name connected
// like "headset" or "headphones"
headphonesConnected = devices
.filter(device => /audiow+/.test(device.kind))
.find(device => device.label.toLowerCase().includes('head'))
;
)
;
;
updateDevices();
// add an ondevicechange event listener so we can tell when
// an input device is connected and disconnected
navigator.mediaDevices.ondevicechange = updateDevices;
NOTE: Testing has been spotty with this new, simpler version, but it eliminates the microphone access requirement. Some browsers also take longer to trigger the ondevicechange handler than others.
In a better world I would be totally outraged that browsers have the unmitigated gall to make this possible, but in this world, it just never seems to stop. I already feel so utterly helpless against the existence of APIs like this and at this point I've no energy left to get outraged.
– Boann
Mar 9 at 21:48
@Boann I don't see the big deal in knowing if the user plugs or unplugs headphones, but maybe I'm not imaginative enough. The immediate usecase I could see would be to pause audio, like many mobile devices do, when the user's headphones are unplugged. I would actually prefer a much cleaner and less hacky implementation where you've just got anonAudioDestinationChangeoronAudioDeviceConnected/onAudioDeviceDisconnectedevent you can listen for.
– Eric Allen
Mar 10 at 16:46
add a comment |
This isn't perfect and it might be a little hacky since it relies on the browser exposing the label attribute of the MediaDevice and then trying to use the string "head"to guess if the user has a headphone device connected, but you could also just watch the number of connected audio devices and pause whenever it changes or something depending on your usecase.
Almost all of the logic was adapted from the MDN page for navigator.mediaDevices.ondevicechange.
NOTE: This code does not work properly when executed from a file:// URL and must be accessed via an http:// or https:// URL for the browser to actually let you access the label property of the MediaDevice. Also, depending on your browser, you may need to use the hack featured in the first iteration of this answer which will ask the user for microphone access and grant you access to the label property.
// let's assume we don't have any headphones connected
let headphonesConnected = false;
const updateDevices = () =>
navigator.mediaDevices.enumerateDevices()
.then(function(devices)
// check to see if we have any device with "head" in its name connected
// like "headset" or "headphones"
headphonesConnected = devices
.filter(device => /audiow+/.test(device.kind))
.find(device => device.label.toLowerCase().includes('head'))
;
)
;
;
updateDevices();
// add an ondevicechange event listener so we can tell when
// an input device is connected and disconnected
navigator.mediaDevices.ondevicechange = updateDevices;
NOTE: Testing has been spotty with this new, simpler version, but it eliminates the microphone access requirement. Some browsers also take longer to trigger the ondevicechange handler than others.
This isn't perfect and it might be a little hacky since it relies on the browser exposing the label attribute of the MediaDevice and then trying to use the string "head"to guess if the user has a headphone device connected, but you could also just watch the number of connected audio devices and pause whenever it changes or something depending on your usecase.
Almost all of the logic was adapted from the MDN page for navigator.mediaDevices.ondevicechange.
NOTE: This code does not work properly when executed from a file:// URL and must be accessed via an http:// or https:// URL for the browser to actually let you access the label property of the MediaDevice. Also, depending on your browser, you may need to use the hack featured in the first iteration of this answer which will ask the user for microphone access and grant you access to the label property.
// let's assume we don't have any headphones connected
let headphonesConnected = false;
const updateDevices = () =>
navigator.mediaDevices.enumerateDevices()
.then(function(devices)
// check to see if we have any device with "head" in its name connected
// like "headset" or "headphones"
headphonesConnected = devices
.filter(device => /audiow+/.test(device.kind))
.find(device => device.label.toLowerCase().includes('head'))
;
)
;
;
updateDevices();
// add an ondevicechange event listener so we can tell when
// an input device is connected and disconnected
navigator.mediaDevices.ondevicechange = updateDevices;
NOTE: Testing has been spotty with this new, simpler version, but it eliminates the microphone access requirement. Some browsers also take longer to trigger the ondevicechange handler than others.
edited Mar 10 at 16:43
answered Mar 9 at 3:55
Eric AllenEric Allen
31829
31829
In a better world I would be totally outraged that browsers have the unmitigated gall to make this possible, but in this world, it just never seems to stop. I already feel so utterly helpless against the existence of APIs like this and at this point I've no energy left to get outraged.
– Boann
Mar 9 at 21:48
@Boann I don't see the big deal in knowing if the user plugs or unplugs headphones, but maybe I'm not imaginative enough. The immediate usecase I could see would be to pause audio, like many mobile devices do, when the user's headphones are unplugged. I would actually prefer a much cleaner and less hacky implementation where you've just got anonAudioDestinationChangeoronAudioDeviceConnected/onAudioDeviceDisconnectedevent you can listen for.
– Eric Allen
Mar 10 at 16:46
add a comment |
In a better world I would be totally outraged that browsers have the unmitigated gall to make this possible, but in this world, it just never seems to stop. I already feel so utterly helpless against the existence of APIs like this and at this point I've no energy left to get outraged.
– Boann
Mar 9 at 21:48
@Boann I don't see the big deal in knowing if the user plugs or unplugs headphones, but maybe I'm not imaginative enough. The immediate usecase I could see would be to pause audio, like many mobile devices do, when the user's headphones are unplugged. I would actually prefer a much cleaner and less hacky implementation where you've just got anonAudioDestinationChangeoronAudioDeviceConnected/onAudioDeviceDisconnectedevent you can listen for.
– Eric Allen
Mar 10 at 16:46
In a better world I would be totally outraged that browsers have the unmitigated gall to make this possible, but in this world, it just never seems to stop. I already feel so utterly helpless against the existence of APIs like this and at this point I've no energy left to get outraged.
– Boann
Mar 9 at 21:48
In a better world I would be totally outraged that browsers have the unmitigated gall to make this possible, but in this world, it just never seems to stop. I already feel so utterly helpless against the existence of APIs like this and at this point I've no energy left to get outraged.
– Boann
Mar 9 at 21:48
@Boann I don't see the big deal in knowing if the user plugs or unplugs headphones, but maybe I'm not imaginative enough. The immediate usecase I could see would be to pause audio, like many mobile devices do, when the user's headphones are unplugged. I would actually prefer a much cleaner and less hacky implementation where you've just got an
onAudioDestinationChange or onAudioDeviceConnected/onAudioDeviceDisconnected event you can listen for.– Eric Allen
Mar 10 at 16:46
@Boann I don't see the big deal in knowing if the user plugs or unplugs headphones, but maybe I'm not imaginative enough. The immediate usecase I could see would be to pause audio, like many mobile devices do, when the user's headphones are unplugged. I would actually prefer a much cleaner and less hacky implementation where you've just got an
onAudioDestinationChange or onAudioDeviceConnected/onAudioDeviceDisconnected event you can listen for.– Eric Allen
Mar 10 at 16:46
add a comment |
I assume there has to be an event for it since youtube does this. +1
– Caleb Goodman
Mar 9 at 2:25
developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API should cover what's possible within a modern browser in terms of audio support
– ADyson
Mar 9 at 2:29