I was wondering if there was any way to detect whether or not headphones are plugged in to a mobile device (in this case an iPhone) through Ionic. Our Ionic app plays sound, which sounds fine without headphones, but has some wonky results when headphones are plugged in.
If you start the app without headphones, then plug them in, the app constantly buzzes. If you start the app after plugging headphones in, it sounds fine, but then if you unplug the headphones, the sound will not play from the speakers, though the app thinks the sound is playing. It's all very weird. Any advice?
You can try this plug in : https://github.com/EddyVerbruggen/HeadsetDetection-PhoneGap-Plugin
If you want to check at the startup, you can place it inside $ionicPlatform.ready
like this:
window.plugins.headsetdetection.detect(function (detected) {
alert("Headphone " + detected)
})
Else if you want to check on any button click or on function you can make it like this:
HTML:
<button class="button button-stable" ng-click="checkHeadphone()">
JS:
$scope.checkHeadphone = function () {
window.plugins.headsetdetection.detect(function (detected) {
alert("Headphone " + detected)
})
}
So on click of the button it will check if headphone is detected
Note : please build/prepare the code for the platform after you install plugin if you still getting error of 'undefined'
This should work for iOS or Android
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.